grunt-contrib-jst/tasks/jst.js

95 lines
3.0 KiB
JavaScript
Raw Normal View History

2012-09-11 02:12:23 +08:00
/*
* grunt-contrib-jst
* http://gruntjs.com/
*
2014-03-01 08:55:55 +08:00
* Copyright (c) 2014 Tim Branyen, contributors
2012-09-11 02:12:23 +08:00
* Licensed under the MIT license.
*/
2012-10-19 07:02:45 +08:00
'use strict';
2012-09-11 02:12:23 +08:00
module.exports = function(grunt) {
2013-01-01 04:06:55 +08:00
var _ = require('lodash');
2012-09-12 23:34:53 +08:00
// filename conversion for templates
var defaultProcessName = function(name) { return name; };
2012-09-11 02:12:23 +08:00
2012-10-19 07:02:45 +08:00
grunt.registerMultiTask('jst', 'Compile underscore templates to JST file', function() {
var lf = grunt.util.linefeed;
2012-10-19 07:02:45 +08:00
var helpers = require('grunt-lib-contrib').init(grunt);
var options = this.options({
namespace: 'JST',
2012-12-29 21:18:21 +08:00
templateSettings: {},
processContent: function (src) { return src; },
separator: lf + lf
2012-10-19 07:02:45 +08:00
});
2012-09-11 02:12:23 +08:00
// assign filename transformation functions
var processName = options.processName || defaultProcessName;
var nsInfo;
if (options.namespace !== false) {
nsInfo = helpers.getNamespaceDeclaration(options.namespace);
}
2012-09-11 02:12:23 +08:00
this.files.forEach(function(f) {
var output = f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
})
.map(function(filepath) {
var src = options.processContent(grunt.file.read(filepath));
var compiled, filename;
2012-11-14 06:12:23 +08:00
try {
compiled = _.template(src, false, options.templateSettings).source;
} catch (e) {
grunt.log.error(e);
2013-07-15 09:38:36 +08:00
grunt.fail.warn('JST "' + filepath + '" failed to compile.');
}
2012-11-14 06:12:23 +08:00
if (options.prettify) {
compiled = compiled.replace(new RegExp('\n', 'g'), '');
}
filename = processName(filepath);
2012-11-14 06:12:23 +08:00
2013-03-07 01:09:18 +08:00
if (options.amd && options.namespace === false) {
return 'return ' + compiled;
}
return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';';
});
if (output.length < 1) {
grunt.log.warn('Destination not written because compiled files were empty.');
} else {
if (options.namespace !== false) {
output.unshift(nsInfo.declaration);
}
2013-03-07 01:09:18 +08:00
if (options.amd) {
if (options.prettify) {
output.forEach(function(line, index) {
output[index] = " " + line;
});
}
output.unshift("define(function(){");
if (options.namespace !== false) {
// Namespace has not been explicitly set to false; the AMD
// wrapper will return the object containing the template.
output.push(" return " + nsInfo.namespace + ";");
}
output.push("});");
}
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
grunt.log.writeln('File "' + f.dest + '" created.');
}
});
2012-11-14 06:12:23 +08:00
2012-09-11 02:12:23 +08:00
});
};