compile.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * https://opensource.org/licenses/MIT
  11. */
  12. /* eslint-disable strict */
  13. /* eslint-disable no-console */
  14. ;(function() {
  15. 'use strict'
  16. var path = require('path')
  17. var tmpl = require(path.join(__dirname, 'tmpl.js'))
  18. var fs = require('fs')
  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*\\(|') +
  26. '\\s*\\('
  27. )
  28. // A list to store the function bodies:
  29. var list = []
  30. var code
  31. // Extend the Templating engine with a print method for the generated functions:
  32. tmpl.print = function(str) {
  33. // Only add helper functions if they are used inside of the template:
  34. var helper = helperRegexp.test(str) ? tmpl.helper : ''
  35. var body = str.replace(tmpl.regexp, tmpl.func)
  36. if (helper || /_e\s*\(/.test(body)) {
  37. helper = '_e=tmpl.encode' + helper + ','
  38. }
  39. return (
  40. 'function(' +
  41. tmpl.arg +
  42. ',tmpl){' +
  43. ('var ' + helper + "_s='" + body + "';return _s;")
  44. .split("_s+='';")
  45. .join('') +
  46. '}'
  47. )
  48. }
  49. // Loop through the command line arguments:
  50. process.argv.forEach(function(file, index) {
  51. var listLength = list.length
  52. var stats
  53. var content
  54. var result
  55. var id
  56. // Skip the first two arguments, which are "node" and the script:
  57. if (index > 1) {
  58. stats = fs.statSync(file)
  59. if (!stats.isFile()) {
  60. console.error(file + ' is not a file.')
  61. return
  62. }
  63. content = fs.readFileSync(file, 'utf8')
  64. // eslint-disable-next-line no-constant-condition
  65. while (true) {
  66. // Find templates in script tags:
  67. result = regexp.exec(content)
  68. if (!result) {
  69. break
  70. }
  71. id = result[2] || result[4]
  72. list.push("'" + id + "':" + tmpl.print(result[5]))
  73. }
  74. if (listLength === list.length) {
  75. // No template script tags found, use the complete content:
  76. id = path.basename(file, path.extname(file))
  77. list.push("'" + id + "':" + tmpl.print(content))
  78. }
  79. }
  80. })
  81. if (!list.length) {
  82. console.error('Missing input file.')
  83. return
  84. }
  85. // Combine the generated functions as cache of the minimal runtime:
  86. code = runtime.replace('{}', '{' + list.join(',') + '}')
  87. // Print the resulting code to the console output:
  88. console.log(code)
  89. })()