compile.js 2.8 KB

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