1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1× 1× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 3× 5× 5× 1× 1× 1× 1× 1× 1× 1× 1× 1× 6× 6× 6× 6× 6× 4× 3× 3× 3× 5× 5× 3× 3× 5× 5× 5× 5× 5× 5× 5× 5× 1× | 'use strict'; const LRU = require('lru-cache'); const path = require('path'); class JTS { constructor(config) { this.config = config || {}; Eif (this.config.cache !== false) { this.config.cache = this.config.cache || {}; this.config.cache.max = this.config.cache.max || 500, this.config.cache.maxAge = this.config.cache.maxAge || 1000 * 60 * 5 this.cache = LRU(this.config.cache); } this.defaultLayout = this.config.defaultLayout || false; this.layouts = this.config.layouts || './'; this.templatePath = './'; this.partialsUsed = []; this.apply = this.apply.bind(this); this.read = this.read.bind(this); this.checkLayout = this.checkLayout.bind(this); this.compile = this.compile.bind(this); this.compileLayout = this.compileLayout.bind(this); this.render = this.render.bind(this); } // Webpack plugin // -- // Allows for JTS to be used in your build process with Webpack. Simply // configure the plugin with a `from` and a `to` value representing the // template source and final HTML output: // ``` // plugins: [ // new JTS({ from: 'src/template.jts', to: 'index.html' }) // ] // ``` apply(compiler) { compiler.plugin('emit', (compilation, callback) => { this.config.cache = false; // Determine the location of the template and add to webpack watch. var source = path.resolve(this.config.from); if (compilation.fileDependencies.indexOf(source) < 0) { compilation.fileDependencies.push(source); } // Render the template. this.render(source, this.config.vars || {}, (err, source) => { if (err) return console.error(err); compilation.assets[this.config.to] = { source: () => source, size: () => source.length }; // Add each partial that was used in the template to webpack watch. this.partialsUsed.forEach(partial => { if (compilation.fileDependencies.indexOf(partial) < 0) { compilation.fileDependencies.push(partial); } }); callback(); }); }); } templateScope() { var engine = this; return { customLayout: false, s: function(text) { return String(text) .replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/'/g, ''') .replace(/"/g, '"'); }, each: function(array, callback) { Iif (!array || !Array.isArray(array)) return ''; return array.map(callback).join(''); }, layout: function(template) { this.customLayout = template; return ''; }, partial: function(template, variables) { var partialEngine = new JTS(); template = path.resolve(engine.templatePath, template); Eif (engine.partialsUsed.indexOf(template) < 0) { engine.partialsUsed.push(template); } variables = variables || this.variables; return partialEngine.compile(partialEngine.read(template), variables); }, variables: {} }; } read(filePath) { var cachedFile; this.templatePath = require('path').dirname(filePath); Eif (this.config.cache !== false) { cachedFile = this.cache.get(filePath); if (cachedFile) return cachedFile; } var template = require('fs').readFileSync(filePath, 'utf8'); Eif (this.config.cache !== false && !cachedFile) { this.cache.set(filePath, template); } return template; } render(filePath, options, cb) { var template = this.read(filePath); var compiled = this.compile(template, options); if (!cb) return compiled; return cb(null, compiled); } compile(template, variables) { var params = [], props = []; for (var variable in variables) { props.push(variable); params.push(variables[variable]); } var scope = this.templateScope(); scope.variables = variables; scope.customLayout = variables && variables.layout; params.unshift(scope); this.compiled = eval(`((_jts${props.length > 0 ? `,${props.join(',')}` : ''}) => ` + '`' + template + '`)'); var final = this.compiled.apply(scope, params); Eif (scope.customLayout === 'none' || (!scope.customLayout && !this.config.defaultLayout)) { return final; } var layout = scope.customLayout ? scope.customLayout : this.config.defaultLayout; return this.compileLayout(layout, final, template, variables); } compileLayout(layout, body, template, variables) { layout = this.checkLayout(layout); if (layout === false) return body; layout = this.read(layout); variables.body = body; variables.layout = 'none'; return this.compile(layout, variables); } checkLayout(layout) { if (!layout) return false; if (layout.indexOf('.jts') === -1) layout += '.jts'; var path = require('path'), fs = require('fs'); var templatePath = path.resolve(this.templatePath, layout); try { fs.accessSync(templatePath); return templatePath; } catch(e) { try { templatePath = path.resolve(this.layouts, layout); fs.accessSync(templatePath); return templatePath; } catch(e) { return false; } } } layout(template) { this.layout = template; return; } } module.exports = JTS; |