precompiler.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. define(['exports', 'neo-async', 'fs', './handlebars', 'path', 'source-map'], function (exports, _neoAsync, _fs, _handlebars, _path, _sourceMap) {
  2. /* eslint-disable no-console */
  3. 'use strict';
  4. // istanbul ignore next
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  6. var _Async = _interopRequireDefault(_neoAsync);
  7. var _fs2 = _interopRequireDefault(_fs);
  8. module.exports.loadTemplates = function (opts, callback) {
  9. loadStrings(opts, function (err, strings) {
  10. if (err) {
  11. callback(err);
  12. } else {
  13. loadFiles(opts, function (err, files) {
  14. if (err) {
  15. callback(err);
  16. } else {
  17. opts.templates = strings.concat(files);
  18. callback(undefined, opts);
  19. }
  20. });
  21. }
  22. });
  23. };
  24. function loadStrings(opts, callback) {
  25. var strings = arrayCast(opts.string),
  26. names = arrayCast(opts.name);
  27. if (names.length !== strings.length && strings.length > 1) {
  28. return callback(new _handlebars.Exception('Number of names did not match the number of string inputs'));
  29. }
  30. _Async['default'].map(strings, function (string, callback) {
  31. if (string !== '-') {
  32. callback(undefined, string);
  33. } else {
  34. (function () {
  35. // Load from stdin
  36. var buffer = '';
  37. process.stdin.setEncoding('utf8');
  38. process.stdin.on('data', function (chunk) {
  39. buffer += chunk;
  40. });
  41. process.stdin.on('end', function () {
  42. callback(undefined, buffer);
  43. });
  44. })();
  45. }
  46. }, function (err, strings) {
  47. strings = strings.map(function (string, index) {
  48. return {
  49. name: names[index],
  50. path: names[index],
  51. source: string
  52. };
  53. });
  54. callback(err, strings);
  55. });
  56. }
  57. function loadFiles(opts, callback) {
  58. // Build file extension pattern
  59. var extension = (opts.extension || 'handlebars').replace(/[\\^$*+?.():=!|{}\-[\]]/g, function (arg) {
  60. return '\\' + arg;
  61. });
  62. extension = new RegExp('\\.' + extension + '$');
  63. var ret = [],
  64. queue = (opts.files || []).map(function (template) {
  65. return { template: template, root: opts.root };
  66. });
  67. _Async['default'].whilst(function () {
  68. return queue.length;
  69. }, function (callback) {
  70. var _queue$shift = queue.shift();
  71. var path = _queue$shift.template;
  72. var root = _queue$shift.root;
  73. _fs2['default'].stat(path, function (err, stat) {
  74. if (err) {
  75. return callback(new _handlebars.Exception('Unable to open template file "' + path + '"'));
  76. }
  77. if (stat.isDirectory()) {
  78. opts.hasDirectory = true;
  79. _fs2['default'].readdir(path, function (err, children) {
  80. /* istanbul ignore next : Race condition that being too lazy to test */
  81. if (err) {
  82. return callback(err);
  83. }
  84. children.forEach(function (file) {
  85. var childPath = path + '/' + file;
  86. if (extension.test(childPath) || _fs2['default'].statSync(childPath).isDirectory()) {
  87. queue.push({ template: childPath, root: root || path });
  88. }
  89. });
  90. callback();
  91. });
  92. } else {
  93. _fs2['default'].readFile(path, 'utf8', function (err, data) {
  94. /* istanbul ignore next : Race condition that being too lazy to test */
  95. if (err) {
  96. return callback(err);
  97. }
  98. if (opts.bom && data.indexOf('') === 0) {
  99. data = data.substring(1);
  100. }
  101. // Clean the template name
  102. var name = path;
  103. if (!root) {
  104. name = _path.basename(name);
  105. } else if (name.indexOf(root) === 0) {
  106. name = name.substring(root.length + 1);
  107. }
  108. name = name.replace(extension, '');
  109. ret.push({
  110. path: path,
  111. name: name,
  112. source: data
  113. });
  114. callback();
  115. });
  116. }
  117. });
  118. }, function (err) {
  119. if (err) {
  120. callback(err);
  121. } else {
  122. callback(undefined, ret);
  123. }
  124. });
  125. }
  126. module.exports.cli = function (opts) {
  127. if (opts.version) {
  128. console.log(_handlebars.VERSION);
  129. return;
  130. }
  131. if (!opts.templates.length && !opts.hasDirectory) {
  132. throw new _handlebars.Exception('Must define at least one template or directory.');
  133. }
  134. if (opts.simple && opts.min) {
  135. throw new _handlebars.Exception('Unable to minimize simple output');
  136. }
  137. var multiple = opts.templates.length !== 1 || opts.hasDirectory;
  138. if (opts.simple && multiple) {
  139. throw new _handlebars.Exception('Unable to output multiple templates in simple mode');
  140. }
  141. // Force simple mode if we have only one template and it's unnamed.
  142. if (!opts.amd && !opts.commonjs && opts.templates.length === 1 && !opts.templates[0].name) {
  143. opts.simple = true;
  144. }
  145. // Convert the known list into a hash
  146. var known = {};
  147. if (opts.known && !Array.isArray(opts.known)) {
  148. opts.known = [opts.known];
  149. }
  150. if (opts.known) {
  151. for (var i = 0, len = opts.known.length; i < len; i++) {
  152. known[opts.known[i]] = true;
  153. }
  154. }
  155. var objectName = opts.partial ? 'Handlebars.partials' : 'templates';
  156. var output = new _sourceMap.SourceNode();
  157. if (!opts.simple) {
  158. if (opts.amd) {
  159. output.add('define([\'' + opts.handlebarPath + 'handlebars.runtime\'], function(Handlebars) {\n Handlebars = Handlebars["default"];');
  160. } else if (opts.commonjs) {
  161. output.add('var Handlebars = require("' + opts.commonjs + '");');
  162. } else {
  163. output.add('(function() {\n');
  164. }
  165. output.add(' var template = Handlebars.template, templates = ');
  166. if (opts.namespace) {
  167. output.add(opts.namespace);
  168. output.add(' = ');
  169. output.add(opts.namespace);
  170. output.add(' || ');
  171. }
  172. output.add('{};\n');
  173. }
  174. opts.templates.forEach(function (template) {
  175. var options = {
  176. knownHelpers: known,
  177. knownHelpersOnly: opts.o
  178. };
  179. if (opts.map) {
  180. options.srcName = template.path;
  181. }
  182. if (opts.data) {
  183. options.data = true;
  184. }
  185. var precompiled = _handlebars.precompile(template.source, options);
  186. // If we are generating a source map, we have to reconstruct the SourceNode object
  187. if (opts.map) {
  188. var consumer = new _sourceMap.SourceMapConsumer(precompiled.map);
  189. precompiled = _sourceMap.SourceNode.fromStringWithSourceMap(precompiled.code, consumer);
  190. }
  191. if (opts.simple) {
  192. output.add([precompiled, '\n']);
  193. } else {
  194. if (!template.name) {
  195. throw new _handlebars.Exception('Name missing for template');
  196. }
  197. if (opts.amd && !multiple) {
  198. output.add('return ');
  199. }
  200. output.add([objectName, '[\'', template.name, '\'] = template(', precompiled, ');\n']);
  201. }
  202. });
  203. // Output the content
  204. if (!opts.simple) {
  205. if (opts.amd) {
  206. if (multiple) {
  207. output.add(['return ', objectName, ';\n']);
  208. }
  209. output.add('});');
  210. } else if (!opts.commonjs) {
  211. output.add('})();');
  212. }
  213. }
  214. if (opts.map) {
  215. output.add('\n//# sourceMappingURL=' + opts.map + '\n');
  216. }
  217. output = output.toStringWithSourceMap();
  218. output.map = output.map + '';
  219. if (opts.min) {
  220. output = minify(output, opts.map);
  221. }
  222. if (opts.map) {
  223. _fs2['default'].writeFileSync(opts.map, output.map, 'utf8');
  224. }
  225. output = output.code;
  226. if (opts.output) {
  227. _fs2['default'].writeFileSync(opts.output, output, 'utf8');
  228. } else {
  229. console.log(output);
  230. }
  231. };
  232. function arrayCast(value) {
  233. value = value != null ? value : [];
  234. if (!Array.isArray(value)) {
  235. value = [value];
  236. }
  237. return value;
  238. }
  239. /**
  240. * Run uglify to minify the compiled template, if uglify exists in the dependencies.
  241. *
  242. * We are using `require` instead of `import` here, because es6-modules do not allow
  243. * dynamic imports and uglify-js is an optional dependency. Since we are inside NodeJS here, this
  244. * should not be a problem.
  245. *
  246. * @param {string} output the compiled template
  247. * @param {string} sourceMapFile the file to write the source map to.
  248. */
  249. function minify(output, sourceMapFile) {
  250. try {
  251. // Try to resolve uglify-js in order to see if it does exist
  252. require.resolve('uglify-js');
  253. } catch (e) {
  254. if (e.code !== 'MODULE_NOT_FOUND') {
  255. // Something else seems to be wrong
  256. throw e;
  257. }
  258. // it does not exist!
  259. console.error('Code minimization is disabled due to missing uglify-js dependency');
  260. return output;
  261. }
  262. return require('uglify-js').minify(output.code, {
  263. sourceMap: {
  264. content: output.map,
  265. url: sourceMapFile
  266. }
  267. });
  268. }
  269. });
  270. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL2xpYi9wcmVjb21waWxlci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7QUFRQSxRQUFNLENBQUMsT0FBTyxDQUFDLGFBQWEsR0FBRyxVQUFTLElBQUksRUFBRSxRQUFRLEVBQUU7QUFDdEQsZUFBVyxDQUFDLElBQUksRUFBRSxVQUFTLEdBQUcsRUFBRSxPQUFPLEVBQUU7QUFDdkMsVUFBSSxHQUFHLEVBQUU7QUFDUCxnQkFBUSxDQUFDLEdBQUcsQ0FBQyxDQUFDO09BQ2YsTUFBTTtBQUNMLGlCQUFTLENBQUMsSUFBSSxFQUFFLFVBQVMsR0FBRyxFQUFFLEtBQUssRUFBRTtBQUNuQyxjQUFJLEdBQUcsRUFBRTtBQUNQLG9CQUFRLENBQUMsR0FBRyxDQUFDLENBQUM7V0FDZixNQUFNO0FBQ0wsZ0JBQUksQ0FBQyxTQUFTLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsQ0FBQztBQUN2QyxvQkFBUSxDQUFDLFNBQVMsRUFBRSxJQUFJLENBQUMsQ0FBQztXQUMzQjtTQUNGLENBQUMsQ0FBQztPQUNKO0tBQ0YsQ0FBQyxDQUFDO0dBQ0osQ0FBQzs7QUFFRixXQUFTLFdBQVcsQ0FBQyxJQUFJLEVBQUUsUUFBUSxFQUFFO0FBQ25DLFFBQUksT0FBTyxHQUFHLFNBQVMsQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDO1FBQ2hDLEtBQUssR0FBRyxTQUFTLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFDOztBQUVqQyxRQUFJLEtBQUssQ0FBQyxNQUFNLEtBQUssT0FBTyxDQUFDLE1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b24gdGhhdCBiZWluZyB0b28gbGF6eSB0byB0ZXN0ICovXG4gICAgICAgICAgaWYgKGVycikge1xuICAgICAgICAgICAgcmV0dXJuIGNhbGxiYWNrKGVycik7XG4gICAgICAgICAgfVxuXG4gICAgICAgICAgaWYgKG9wdHMuYm9tICYmIGRhdGEuaW5kZXhPZignXFx1RkVGRicpID09PSAwKSB7XG4gICAgICAgICAgICBkYXRhID0gZGF0YS5zdWJzdHJpbmcoMSk7XG4gICAgICAgICAgfVxuXG4gICAgICAgICAgLy8gQ2xlYW4gdGhlIHRlbXBsYXRlIG5hbWVcbiAgICAgICAgICBsZXQgbmFtZSA9IHBhdGg7XG4gICAgICAgICAgaWYgKCFyb290KSB7XG4gICAgICAgICAgICBuYW1lID0gYmFzZW5hbWUobmFtZSk7XG4gICAgICAgICAgfSBlbHNlIGlmIChuYW1lLmluZGV4T2Yocm9vdCkgPT09IDApIHtcbiAgICAgICAgICAgIG5hbWUgPSBuYW1lLnN1YnN0cmluZyhyb290Lmxlbmd0aCArIDEpO1xuICAgICAgICAgIH1cbiAgICAgICAgICBuYW1lID0gbmFtZS5yZXBsYWNlKGV4dGVuc2lvbiwgJycpO1xuXG4gICAgICAgICAgcmV0LnB1c2goe1xuICAgICAgICAgICAgcGF0aDogcGF0aCxcbiAgICAgICAgICAgIG5hbWU6IG5hbWUsXG4gICAgICAgICAgICBzb3VyY2U6IGRhdGFcbiAgICAgICAgICB9KTtcblxuICAgICAgICAgIGNhbGxiYWNrKCk7XG4gICAgICAgIH0pO1xuICAgICAgfVxuICAgIH0pO1xuICB9LFxuICBmdW5jdGlvbihlcnIpIHtcbiAgICBpZiAoZXJyKSB7XG4gICAgICBjYWxsYmFjayhlcnIpO1xuICAgIH0gZWxzZSB7XG4gICAgICBjYWxsYmFjayh1bmRlZmluZWQsIHJldCk7XG4gICAgfVxuICB9KTtcbn1cblxubW9kdWxlLmV4cG9ydHMuY2xpID0gZnVuY3Rpb24ob3B0cykge1xuICBpZiAob3B0cy52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