compile.js 2.7 KB

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