add processName function - closes issue gh-1
This commit is contained in:
parent
deaf7c937b
commit
8699b506cf
12
README.md
12
README.md
@ -39,6 +39,18 @@ This controls how this task (and its helpers) operate and should contain key:val
|
|||||||
|
|
||||||
The namespace in which the resulting JST templates are assigned to.
|
The namespace in which the resulting JST templates are assigned to.
|
||||||
|
|
||||||
|
##### processName ```function```
|
||||||
|
|
||||||
|
This option accepts a function which takes one argument (the template filepath) and returns a string which will be used as the key for the precompiled template object. The example below stores all templates on the default JST namespace in capital letters.
|
||||||
|
|
||||||
|
``` javascript
|
||||||
|
options: {
|
||||||
|
processName: function(filename) {
|
||||||
|
return filename.toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
##### templateSettings ```object```
|
##### templateSettings ```object```
|
||||||
|
|
||||||
The settings passed to underscore when compiling templates.
|
The settings passed to underscore when compiling templates.
|
||||||
|
53
tasks/jst.js
53
tasks/jst.js
@ -14,51 +14,50 @@ module.exports = function(grunt) {
|
|||||||
|
|
||||||
var escapeQuote = function(name) { return name.replace("'","\\'"); };
|
var escapeQuote = function(name) { return name.replace("'","\\'"); };
|
||||||
|
|
||||||
var jst = function(source, filepath, namespace, templateSettings) {
|
// filename conversion for templates
|
||||||
try {
|
var defaultProcessName = function(name) { return name; };
|
||||||
return namespace + "['" + escapeQuote(filepath) + "'] = " + _.template(source, false, templateSettings).source + ";";
|
|
||||||
} catch (e) {
|
|
||||||
grunt.log.error(e);
|
|
||||||
grunt.fail.warn("JST failed to compile.");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
grunt.registerMultiTask("jst", "Compile underscore templates to JST file", function() {
|
grunt.registerMultiTask("jst", "Compile underscore templates to JST file", function() {
|
||||||
|
|
||||||
var helpers = require("grunt-contrib-lib").init(grunt);
|
var helpers = require("grunt-contrib-lib").init(grunt);
|
||||||
var options = helpers.options(this, {namespace: "JST", templateSettings: {}});
|
var options = helpers.options(this, {namespace: "JST", templateSettings: {}});
|
||||||
|
|
||||||
|
// assign filename transformation functions
|
||||||
|
var processName = options.processName || defaultProcessName;
|
||||||
|
|
||||||
grunt.verbose.writeflags(options, "Options");
|
grunt.verbose.writeflags(options, "Options");
|
||||||
|
|
||||||
// TODO: ditch this when grunt v0.4 is released
|
// TODO: ditch this when grunt v0.4 is released
|
||||||
this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target);
|
this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target);
|
||||||
|
|
||||||
var srcFiles;
|
var compiled, srcFiles, src, filename;
|
||||||
var taskOutput;
|
var output = [];
|
||||||
var sourceCode;
|
var namespace = "this['" + options.namespace + "']";
|
||||||
var sourceCompiled;
|
|
||||||
|
|
||||||
var helperNamespace = "this['" + options.namespace + "']";
|
this.files.forEach(function(files) {
|
||||||
|
srcFiles = grunt.file.expandFiles(files.src);
|
||||||
|
srcFiles.forEach(function(file) {
|
||||||
|
src = grunt.file.read(file);
|
||||||
|
|
||||||
this.files.forEach(function(file) {
|
try {
|
||||||
srcFiles = grunt.file.expandFiles(file.src);
|
compiled = _.template(src, false, options.templateSettings).source;
|
||||||
|
} catch (e) {
|
||||||
|
grunt.log.error(e);
|
||||||
|
grunt.fail.warn("JST failed to compile.");
|
||||||
|
}
|
||||||
|
|
||||||
taskOutput = [];
|
filename = escapeQuote(processName(file));
|
||||||
taskOutput.push(helperNamespace + " = " + helperNamespace + " || {};");
|
output.push(namespace+"['"+filename+"'] = "+compiled+";");
|
||||||
|
|
||||||
srcFiles.forEach(function(srcFile) {
|
|
||||||
sourceCode = grunt.file.read(srcFile);
|
|
||||||
|
|
||||||
sourceCompiled = jst(sourceCode, srcFile, helperNamespace, options.templateSettings);
|
|
||||||
|
|
||||||
taskOutput.push(sourceCompiled);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (taskOutput.length > 0) {
|
if(output.length > 0) {
|
||||||
grunt.file.write(file.dest, taskOutput.join("\n\n"));
|
output.unshift(namespace + " = " + namespace + " || {};");
|
||||||
grunt.log.writeln("File '" + file.dest + "' created.");
|
grunt.file.write(files.dest, output.join("\n\n"));
|
||||||
|
grunt.log.writeln("File '" + files.dest + "' created.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user