/
jst.js
61 lines (45 loc) · 1.82 KB
/
jst.js
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
/*
* grunt-contrib-jst
* http://gruntjs.com/
*
* Copyright (c) 2012 Tim Branyen, contributors
* Licensed under the MIT license.
*/
module.exports = function(grunt) {
"use strict";
var _ = require("underscore");
var helpers = require('grunt-lib-contrib').init(grunt);
// filename conversion for templates
var defaultProcessName = function(name) { return name; };
grunt.registerMultiTask("jst", "Compile underscore templates to JST file", function() {
var helpers = require("grunt-lib-contrib").init(grunt);
var options = helpers.options(this, {namespace: "JST", templateSettings: {}});
// assign filename transformation functions
var processName = options.processName || defaultProcessName;
grunt.verbose.writeflags(options, "Options");
// TODO: ditch this when grunt v0.4 is released
this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target);
var compiled, srcFiles, src, filename;
var output = [];
var nsInfo = helpers.getNamespaceDeclaration(options.namespace);
this.files.forEach(function(files) {
srcFiles = grunt.file.expandFiles(files.src);
srcFiles.forEach(function(file) {
src = grunt.file.read(file);
try {
compiled = _.template(src, false, options.templateSettings).source;
} catch (e) {
grunt.log.error(e);
grunt.fail.warn("JST failed to compile.");
}
filename = processName(file);
output.push(nsInfo.namespace+"["+JSON.stringify(filename)+"] = "+compiled+";");
});
if(output.length > 0) {
output.unshift(nsInfo.declaration);
grunt.file.write(files.dest, output.join("\n\n"));
grunt.log.writeln("File '" + files.dest + "' created.");
}
});
});
};