|
10 | 10 | * http://www.opensource.org/licenses/MIT |
11 | 11 | */ |
12 | 12 |
|
13 | | -/*jslint nomen: true, stupid: true */ |
14 | 13 | /*global require, __dirname, process, console */ |
15 | 14 |
|
16 | | -(function () { |
17 | | - "use strict"; |
18 | | - var tmpl = require("./tmpl.js").tmpl, |
19 | | - fs = require("fs"), |
20 | | - path = require("path"), |
21 | | - uglifyJS = require("uglify-js"), |
22 | | - // Retrieve the content of the minimal runtime: |
23 | | - runtime = fs.readFileSync(__dirname + "/runtime.js", "utf8"), |
24 | | - // A regular expression to parse templates from script tags in a HTML page: |
25 | | - regexp = /<script( id="([\w\-]+)")? type="text\/x-tmpl"( id="([\w\-]+)")?>([\s\S]+?)<\/script>/gi, |
26 | | - // A regular expression to match the helper function names: |
27 | | - helperRegexp = new RegExp( |
28 | | - tmpl.helper.match(/\w+(?=\s*=\s*function\s*\()/g).join("\\s*\\(|") + "\\s*\\(" |
29 | | - ), |
30 | | - // A list to store the function bodies: |
31 | | - list = [], |
32 | | - code; |
33 | | - // Extend the Templating engine with a print method for the generated functions: |
34 | | - tmpl.print = function (str) { |
35 | | - // Only add helper functions if they are used inside of the template: |
36 | | - var helper = helperRegexp.test(str) ? tmpl.helper : "", |
37 | | - body = str.replace(tmpl.regexp, tmpl.func); |
38 | | - if (helper || (/_e\s*\(/.test(body))) { |
39 | | - helper = "_e=tmpl.encode" + helper + ","; |
40 | | - } |
41 | | - return "function(" + tmpl.arg + ",tmpl){" + |
42 | | - ("var " + helper + "_s='" + body + "';return _s;") |
43 | | - .split("_s+='';").join("") + "}"; |
44 | | - }; |
45 | | - // Loop through the command line arguments: |
46 | | - process.argv.forEach(function (file, index) { |
47 | | - var listLength = list.length, |
48 | | - stats, |
49 | | - content, |
50 | | - result, |
51 | | - id; |
52 | | - // Skipt the first two arguments, which are "node" and the script: |
53 | | - if (index > 1) { |
54 | | - stats = fs.statSync(file); |
55 | | - if (!stats.isFile()) { |
56 | | - console.error(file + " is not a file."); |
57 | | - return; |
58 | | - } |
59 | | - content = fs.readFileSync(file, "utf8"); |
60 | | - while (true) { |
61 | | - // Find templates in script tags: |
62 | | - result = regexp.exec(content); |
63 | | - if (!result) { |
64 | | - break; |
65 | | - } |
66 | | - id = result[2] || result[4]; |
67 | | - list.push("'" + id + "':" + tmpl.print(result[5])); |
68 | | - } |
69 | | - if (listLength === list.length) { |
70 | | - // No template script tags found, use the complete content: |
71 | | - id = path.basename(file, path.extname(file)); |
72 | | - list.push("'" + id + "':" + tmpl.print(content)); |
73 | | - } |
| 15 | +;(function () { |
| 16 | + 'use strict' |
| 17 | + var path = require('path') |
| 18 | + var tmpl = require(path.join(__dirname, 'tmpl.js')) |
| 19 | + var fs = require('fs') |
| 20 | + var uglifyJS = require('uglify-js') |
| 21 | + // Retrieve the content of the minimal runtime: |
| 22 | + var runtime = fs.readFileSync(path.join(__dirname, 'runtime.js'), 'utf8') |
| 23 | + // A regular expression to parse templates from script tags in a HTML page: |
| 24 | + var regexp = /<script( id="([\w\-]+)")? type="text\/x-tmpl"( id="([\w\-]+)")?>([\s\S]+?)<\/script>/gi |
| 25 | + // A regular expression to match the helper function names: |
| 26 | + var helperRegexp = new RegExp( |
| 27 | + tmpl.helper.match(/\w+(?=\s*=\s*function\s*\()/g).join('\\s*\\(|') + '\\s*\\(' |
| 28 | + ) |
| 29 | + // A list to store the function bodies: |
| 30 | + var list = [] |
| 31 | + var code |
| 32 | + // Extend the Templating engine with a print method for the generated functions: |
| 33 | + tmpl.print = function (str) { |
| 34 | + // Only add helper functions if they are used inside of the template: |
| 35 | + var helper = helperRegexp.test(str) ? tmpl.helper : '' |
| 36 | + var body = str.replace(tmpl.regexp, tmpl.func) |
| 37 | + if (helper || (/_e\s*\(/.test(body))) { |
| 38 | + helper = '_e=tmpl.encode' + helper + ',' |
| 39 | + } |
| 40 | + return 'function(' + tmpl.arg + ',tmpl){' + |
| 41 | + ('var ' + helper + "_s='" + body + "';return _s;") |
| 42 | + .split("_s+='';").join('') + '}' |
| 43 | + } |
| 44 | + // Loop through the command line arguments: |
| 45 | + process.argv.forEach(function (file, index) { |
| 46 | + var listLength = list.length |
| 47 | + var stats |
| 48 | + var content |
| 49 | + var result |
| 50 | + var id |
| 51 | + // Skipt the first two arguments, which are "node" and the script: |
| 52 | + if (index > 1) { |
| 53 | + stats = fs.statSync(file) |
| 54 | + if (!stats.isFile()) { |
| 55 | + console.error(file + ' is not a file.') |
| 56 | + return |
| 57 | + } |
| 58 | + content = fs.readFileSync(file, 'utf8') |
| 59 | + while (true) { |
| 60 | + // Find templates in script tags: |
| 61 | + result = regexp.exec(content) |
| 62 | + if (!result) { |
| 63 | + break |
74 | 64 | } |
75 | | - }); |
76 | | - if (!list.length) { |
77 | | - console.error("Missing input file."); |
78 | | - return; |
| 65 | + id = result[2] || result[4] |
| 66 | + list.push("'" + id + "':" + tmpl.print(result[5])) |
| 67 | + } |
| 68 | + if (listLength === list.length) { |
| 69 | + // No template script tags found, use the complete content: |
| 70 | + id = path.basename(file, path.extname(file)) |
| 71 | + list.push("'" + id + "':" + tmpl.print(content)) |
| 72 | + } |
79 | 73 | } |
80 | | - // Combine the generated functions as cache of the minimal runtime: |
81 | | - code = runtime.replace("{}", "{" + list.join(",") + "}"); |
82 | | - // Generate the minified code and print it to the console output: |
83 | | - console.log(uglifyJS.minify(code, {fromString: true}).code); |
84 | | -}()); |
| 74 | + }) |
| 75 | + if (!list.length) { |
| 76 | + console.error('Missing input file.') |
| 77 | + return |
| 78 | + } |
| 79 | + // Combine the generated functions as cache of the minimal runtime: |
| 80 | + code = runtime.replace('{}', '{' + list.join(',') + '}') |
| 81 | + // Generate the minified code and print it to the console output: |
| 82 | + console.log(uglifyJS.minify(code, {fromString: true}).code) |
| 83 | +}()) |
0 commit comments