Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

140 řádky
3.5 KiB

  1. #!/usr/bin/env node
  2. var optimist = require('optimist')
  3. .usage('Precompile handlebar templates.\nUsage: $0 template...', {
  4. 'f': {
  5. 'type': 'string',
  6. 'description': 'Output File',
  7. 'alias': 'output'
  8. },
  9. 'k': {
  10. 'type': 'string',
  11. 'description': 'Known helpers',
  12. 'alias': 'known'
  13. },
  14. 'o': {
  15. 'type': 'boolean',
  16. 'description': 'Known helpers only',
  17. 'alias': 'knownOnly'
  18. },
  19. 'm': {
  20. 'type': 'boolean',
  21. 'description': 'Minimize output',
  22. 'alias': 'min'
  23. },
  24. 's': {
  25. 'type': 'boolean',
  26. 'description': 'Output template function only.',
  27. 'alias': 'simple'
  28. },
  29. 'r': {
  30. 'type': 'string',
  31. 'description': 'Template root. Base value that will be stripped from template names.',
  32. 'alias': 'root'
  33. }
  34. })
  35. .check(function(argv) {
  36. var template = [0];
  37. if (!argv._.length) {
  38. throw 'Must define at least one template or directory.';
  39. }
  40. argv._.forEach(function(template) {
  41. try {
  42. fs.statSync(template);
  43. } catch (err) {
  44. throw 'Unable to open template file "' + template + '"';
  45. }
  46. });
  47. })
  48. .check(function(argv) {
  49. if (argv.simple && argv.min) {
  50. throw 'Unable to minimze simple output';
  51. }
  52. if (argv.simple && (argv._.length !== 1 || fs.statSync(argv._[0]).isDirectory())) {
  53. throw 'Unable to output multiple templates in simple mode';
  54. }
  55. });
  56. var fs = require('fs'),
  57. handlebars = require('../lib/handlebars'),
  58. basename = require('path').basename,
  59. uglify = require('uglify-js');
  60. var argv = optimist.argv,
  61. template = argv._[0];
  62. // Convert the known list into a hash
  63. var known = {};
  64. if (argv.known && !Array.isArray(argv.known)) {
  65. argv.known = [argv.known];
  66. }
  67. if (argv.known) {
  68. for (var i = 0, len = argv.known.length; i < len; i++) {
  69. known[argv.known[i]] = true;
  70. }
  71. }
  72. var output = [];
  73. if (!argv.simple) {
  74. output.push('(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n');
  75. }
  76. function processTemplate(template, root) {
  77. var path = template,
  78. stat = fs.statSync(path);
  79. if (stat.isDirectory()) {
  80. fs.readdirSync(template).map(function(file) {
  81. var path = template + '/' + file;
  82. if (/\.handlebars$/.test(path) || fs.statSync(path).isDirectory()) {
  83. processTemplate(path, root || template);
  84. }
  85. });
  86. } else {
  87. var data = fs.readFileSync(path, 'utf8');
  88. var options = {
  89. knownHelpers: known,
  90. knownHelpersOnly: argv.o
  91. };
  92. // Clean the template name
  93. if (!root) {
  94. template = basename(template);
  95. } else if (template.indexOf(root) === 0) {
  96. template = template.substring(root.length+1);
  97. }
  98. template = template.replace(/\.handlebars$/, '');
  99. if (argv.simple) {
  100. output.push(handlebars.precompile(data, options) + '\n');
  101. } else {
  102. output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
  103. }
  104. }
  105. }
  106. argv._.forEach(function(template) {
  107. processTemplate(template, argv.root);
  108. });
  109. // Output the content
  110. if (!argv.simple) {
  111. output.push('})();');
  112. }
  113. output = output.join('');
  114. if (argv.min) {
  115. var ast = uglify.parser.parse(output);
  116. ast = uglify.uglify.ast_mangle(ast);
  117. ast = uglify.uglify.ast_squeeze(ast);
  118. output = uglify.uglify.gen_code(ast);
  119. }
  120. if (argv.output) {
  121. fs.writeFileSync(argv.output, output, 'utf8');
  122. } else {
  123. console.log(output);
  124. }