Blame view

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