compile.js 2.7 KB

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