2012-09-11 02:12:23 +08:00
|
|
|
/*
|
|
|
|
* grunt-contrib-jst
|
|
|
|
* http://gruntjs.com/
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tim Branyen, contributors
|
|
|
|
* Licensed under the MIT license.
|
|
|
|
* https://github.com/gruntjs/grunt-contrib-jst/blob/master/LICENSE-MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function(grunt) {
|
|
|
|
"use strict";
|
|
|
|
|
2012-09-11 19:56:17 +08:00
|
|
|
var _ = require("underscore");
|
2012-09-15 07:31:54 +08:00
|
|
|
var helpers = require('grunt-contrib-lib').init(grunt);
|
2012-09-12 23:34:53 +08:00
|
|
|
|
2012-09-12 23:52:39 +08:00
|
|
|
// filename conversion for templates
|
|
|
|
var defaultProcessName = function(name) { return name; };
|
2012-09-11 02:12:23 +08:00
|
|
|
|
|
|
|
grunt.registerMultiTask("jst", "Compile underscore templates to JST file", function() {
|
2012-09-11 06:32:35 +08:00
|
|
|
|
|
|
|
var helpers = require("grunt-contrib-lib").init(grunt);
|
2012-09-11 02:12:23 +08:00
|
|
|
var options = helpers.options(this, {namespace: "JST", templateSettings: {}});
|
|
|
|
|
2012-09-12 23:52:39 +08:00
|
|
|
// assign filename transformation functions
|
|
|
|
var processName = options.processName || defaultProcessName;
|
|
|
|
|
2012-09-11 02:12:23 +08:00
|
|
|
grunt.verbose.writeflags(options, "Options");
|
|
|
|
|
|
|
|
// TODO: ditch this when grunt v0.4 is released
|
|
|
|
this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target);
|
|
|
|
|
2012-09-12 23:52:39 +08:00
|
|
|
var compiled, srcFiles, src, filename;
|
|
|
|
var output = [];
|
2012-09-15 07:31:54 +08:00
|
|
|
var nsInfo = helpers.getNamespaceDeclaration(options.namespace);
|
2012-09-11 02:12:23 +08:00
|
|
|
|
2012-09-12 23:52:39 +08:00
|
|
|
this.files.forEach(function(files) {
|
|
|
|
srcFiles = grunt.file.expandFiles(files.src);
|
|
|
|
srcFiles.forEach(function(file) {
|
|
|
|
src = grunt.file.read(file);
|
2012-09-11 02:12:23 +08:00
|
|
|
|
2012-09-12 23:52:39 +08:00
|
|
|
try {
|
|
|
|
compiled = _.template(src, false, options.templateSettings).source;
|
|
|
|
} catch (e) {
|
|
|
|
grunt.log.error(e);
|
|
|
|
grunt.fail.warn("JST failed to compile.");
|
|
|
|
}
|
2012-09-11 02:12:23 +08:00
|
|
|
|
2012-09-15 07:31:54 +08:00
|
|
|
filename = processName(file);
|
|
|
|
output.push(nsInfo.namespace+"["+JSON.stringify(filename)+"] = "+compiled+";");
|
2012-09-11 02:12:23 +08:00
|
|
|
});
|
|
|
|
|
2012-09-12 23:52:39 +08:00
|
|
|
if(output.length > 0) {
|
2012-09-15 07:31:54 +08:00
|
|
|
output.unshift(nsInfo.declaration);
|
2012-09-12 23:52:39 +08:00
|
|
|
grunt.file.write(files.dest, output.join("\n\n"));
|
|
|
|
grunt.log.writeln("File '" + files.dest + "' created.");
|
2012-09-11 02:12:23 +08:00
|
|
|
}
|
|
|
|
});
|
2012-09-12 23:52:39 +08:00
|
|
|
|
|
|
|
|
2012-09-11 02:12:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|