precompiler.js 31 KB

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