2012-09-11 02:12:23 +08:00
|
|
|
/*
|
|
|
|
* grunt-contrib-jst
|
|
|
|
* http://gruntjs.com/
|
|
|
|
*
|
2015-01-02 03:30:53 +08:00
|
|
|
* Copyright (c) 2015 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';
|
2014-03-01 10:19:13 +08:00
|
|
|
var _ = require('lodash');
|
|
|
|
var chalk = require('chalk');
|
2012-10-19 07:02:45 +08:00
|
|
|
|
2012-09-11 02:12:23 +08:00
|
|
|
module.exports = function(grunt) {
|
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
|
|
|
|
2012-10-19 07:02:45 +08:00
|
|
|
grunt.registerMultiTask('jst', 'Compile underscore templates to JST file', function() {
|
2013-01-11 05:08:29 +08:00
|
|
|
var lf = grunt.util.linefeed;
|
2014-07-12 01:17:51 +08:00
|
|
|
var lib = require('./lib/jst');
|
2012-10-19 07:02:45 +08:00
|
|
|
var options = this.options({
|
|
|
|
namespace: 'JST',
|
2012-12-29 21:18:21 +08:00
|
|
|
templateSettings: {},
|
2013-01-11 05:08:29 +08:00
|
|
|
processContent: function (src) { return src; },
|
|
|
|
separator: lf + lf
|
2012-10-19 07:02:45 +08:00
|
|
|
});
|
2012-09-11 02:12:23 +08:00
|
|
|
|
2012-09-12 23:52:39 +08:00
|
|
|
// assign filename transformation functions
|
|
|
|
var processName = options.processName || defaultProcessName;
|
|
|
|
|
2013-03-06 19:44:15 +08:00
|
|
|
var nsInfo;
|
|
|
|
if (options.namespace !== false) {
|
2014-07-12 01:17:51 +08:00
|
|
|
nsInfo = lib.getNamespaceDeclaration(options.namespace);
|
2013-03-06 19:44:15 +08:00
|
|
|
}
|
2012-09-11 02:12:23 +08:00
|
|
|
|
2013-01-10 06:48:55 +08:00
|
|
|
this.files.forEach(function(f) {
|
2013-01-11 05:08:29 +08:00
|
|
|
var output = f.src.filter(function(filepath) {
|
|
|
|
// Warn on and remove invalid source files (if nonull was set).
|
|
|
|
if (!grunt.file.exists(filepath)) {
|
2014-03-01 10:19:13 +08:00
|
|
|
grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found.');
|
2013-01-11 05:08:29 +08:00
|
|
|
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
|
|
|
|
2013-01-10 06:48:55 +08:00
|
|
|
try {
|
|
|
|
compiled = _.template(src, false, options.templateSettings).source;
|
|
|
|
} catch (e) {
|
|
|
|
grunt.log.error(e);
|
2014-03-01 10:19:13 +08:00
|
|
|
grunt.fail.warn('JST ' + chalk.cyan(filepath) + ' failed to compile.');
|
2013-01-10 06:48:55 +08:00
|
|
|
}
|
2012-11-14 06:12:23 +08:00
|
|
|
|
2013-01-10 06:48:55 +08:00
|
|
|
if (options.prettify) {
|
2014-03-01 10:17:38 +08:00
|
|
|
compiled = compiled.replace(/\n/g, '');
|
2013-01-10 06:48:55 +08:00
|
|
|
}
|
2013-01-11 05:08:29 +08:00
|
|
|
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) {
|
2013-03-06 19:44:15 +08:00
|
|
|
return 'return ' + compiled;
|
|
|
|
}
|
2013-01-10 06:48:55 +08:00
|
|
|
return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';';
|
|
|
|
});
|
2012-09-12 23:52:39 +08:00
|
|
|
|
2013-01-11 05:08:29 +08:00
|
|
|
if (output.length < 1) {
|
|
|
|
grunt.log.warn('Destination not written because compiled files were empty.');
|
|
|
|
} else {
|
2013-03-06 19:44:15 +08:00
|
|
|
if (options.namespace !== false) {
|
|
|
|
output.unshift(nsInfo.declaration);
|
|
|
|
}
|
2013-03-07 01:09:18 +08:00
|
|
|
if (options.amd) {
|
2013-01-10 06:48:55 +08:00
|
|
|
if (options.prettify) {
|
|
|
|
output.forEach(function(line, index) {
|
|
|
|
output[index] = " " + line;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
output.unshift("define(function(){");
|
2013-03-06 19:44:15 +08:00
|
|
|
if (options.namespace !== false) {
|
|
|
|
// Namespace has not been explicitly set to false; the AMD
|
|
|
|
// wrapper will return the object containing the template.
|
2013-03-06 21:56:40 +08:00
|
|
|
output.push(" return " + nsInfo.namespace + ";");
|
2013-03-06 19:44:15 +08:00
|
|
|
}
|
|
|
|
output.push("});");
|
2012-11-27 02:02:20 +08:00
|
|
|
}
|
2013-01-11 05:08:29 +08:00
|
|
|
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
|
2014-03-01 10:19:13 +08:00
|
|
|
grunt.log.writeln('File ' + chalk.cyan(f.dest) + ' created.');
|
2012-11-27 02:02:20 +08:00
|
|
|
}
|
2013-01-10 06:48:55 +08:00
|
|
|
});
|
2012-11-14 06:12:23 +08:00
|
|
|
|
2012-09-11 02:12:23 +08:00
|
|
|
});
|
|
|
|
};
|