Tweak build option internals and add test template files.

Former-commit-id: ed5ec6ed7886a066c8c727de19dc0fe6548a276d
This commit is contained in:
John-David Dalton
2012-09-30 00:57:09 -07:00
parent 65ab5fdb34
commit 463b5c6e49
7 changed files with 70 additions and 32 deletions

View File

@@ -258,12 +258,12 @@
* each template file's basename. * each template file's basename.
* *
* @private * @private
* @param {String} pattern The file path pattern. * @param {String} [pattern='<cwd>/*.jst'] The file path pattern.
* @param {Object} options The options object. * @param {Object} [options=_.templateSettings] The options object.
* @returns {String} Returns the compiled source. * @returns {String} Returns the compiled source.
*/ */
function buildTemplate(pattern, options) { function buildTemplate(pattern, options) {
pattern || (pattern = './*.jst'); pattern || (pattern = path.join(cwd, '*.jst'));
options || (options = _.templateSettings); options || (options = _.templateSettings);
var directory = path.dirname(pattern); var directory = path.dirname(pattern);
@@ -289,9 +289,9 @@
); );
fs.readdirSync(directory).forEach(function(filename) { fs.readdirSync(directory).forEach(function(filename) {
var filepath = path.join(directory, filename); var filePath = path.join(directory, filename);
if (pattern.test(filename)) { if (pattern.test(filename)) {
var text = fs.readFileSync(filepath, 'utf8'), var text = fs.readFileSync(filePath, 'utf8'),
precompiled = getFunctionSource(_.template(text, null, options)), precompiled = getFunctionSource(_.template(text, null, options)),
prop = filename.replace(/\..*$/, ''); prop = filename.replace(/\..*$/, '');
@@ -809,9 +809,9 @@
/** /**
* Creates a debug and minified build, executing the `callback` for each. * Creates a debug and minified build, executing the `callback` for each.
* The `callback` is invoked with 2 arguments; (filepath, source) * The `callback` is invoked with 2 arguments; (filePath, source)
* *
* @param {Array} options The build options array. * @param {Array} [options=[]] The build options array.
* @param {Function} callback The function called per build. * @param {Function} callback The function called per build.
*/ */
function build(options, callback) { function build(options, callback) {
@@ -1429,7 +1429,8 @@
if (!isDebug) { if (!isDebug) {
workingName += '.min'; workingName += '.min';
minify(source, { minify(source, {
'silent': isSilent, 'isSilent': isSilent,
'isTemplate': isTemplate,
'workingName': workingName, 'workingName': workingName,
'onComplete': function(source) { 'onComplete': function(source) {
// correct overly aggressive Closure Compiler minification // correct overly aggressive Closure Compiler minification
@@ -1458,8 +1459,8 @@
} }
else { else {
// or invoked directly // or invoked directly
build(process.argv, function(source, filepath) { build(process.argv, function(source, filePath) {
filepath && fs.writeFileSync(filepath, source, 'utf8'); filePath && fs.writeFileSync(filePath, source, 'utf8');
}); });
} }
}()); }());

View File

@@ -9,7 +9,7 @@
spawn = require('child_process').spawn; spawn = require('child_process').spawn;
/** The directory that is the base of the repository */ /** The directory that is the base of the repository */
var basePath = path.join(__dirname, '../'); var basePath = fs.realpathSync(path.join(__dirname, '../'));
/** The directory where the Closure Compiler is located */ /** The directory where the Closure Compiler is located */
var closurePath = path.join(basePath, 'vendor', 'closure-compiler', 'compiler.jar'); var closurePath = path.join(basePath, 'vendor', 'closure-compiler', 'compiler.jar');
@@ -18,9 +18,9 @@
var distPath = path.join(basePath, 'dist'); var distPath = path.join(basePath, 'dist');
/** Load other modules */ /** Load other modules */
var preprocess = require(path.join(__dirname, 'pre-compile')), var preprocess = require('./pre-compile'),
postprocess = require(path.join(__dirname, 'post-compile')), postprocess = require('./post-compile'),
uglifyJS = require(path.join(basePath, 'vendor', 'uglifyjs', 'uglify-js')); uglifyJS = require('../vendor/uglifyjs/uglify-js');
/** Closure Compiler command-line options */ /** Closure Compiler command-line options */
var closureOptions = [ var closureOptions = [
@@ -50,14 +50,22 @@
options = source; options = source;
var filePath = options[options.length - 1], var filePath = options[options.length - 1],
dirPath = path.dirname(filePath), dirPath = path.dirname(filePath),
workingName = path.basename(filePath, '.js') + '.min', isSilent = options.indexOf('-s') > -1 || options.indexOf('--silent') > -1,
outputPath = path.join(dirPath, workingName + '.js'), isTemplate = options.indexOf('-t') > -1 || options.indexOf('--template') > -1,
isSilent = options.indexOf('-s') > -1 || options.indexOf('--silent') > -1; workingName = path.basename(filePath, '.js') + '.min';
workingName = options.reduce(function(result, value, index) {
return /-wn|--working-name/.test(value)
? options[index + 1]
: result;
}, workingName);
var outputPath = path.join(dirPath, workingName + '.js');
source = fs.readFileSync(filePath, 'utf8'); source = fs.readFileSync(filePath, 'utf8');
options = { options = {
'silent': isSilent, 'isSilent': isSilent,
'workingName': workingName, 'isTemplate': isTemplate,
'onComplete': function(source) { 'onComplete': function(source) {
fs.writeFileSync(outputPath, source, 'utf8'); fs.writeFileSync(outputPath, source, 'utf8');
} }
@@ -71,8 +79,8 @@
* *
* @private * @private
* @constructor * @constructor
* @param {String} source The source to minify. * @param {String} [source=''] The source to minify.
* @param {Object} options The options object containing `onComplete`, * @param {Object} [options={}] The options object containing `onComplete`,
* `silent`, and `workingName`. * `silent`, and `workingName`.
*/ */
function Minify(source, options) { function Minify(source, options) {
@@ -94,11 +102,12 @@
this.compiled = {}; this.compiled = {};
this.hybrid = {}; this.hybrid = {};
this.uglified = {}; this.uglified = {};
this.isSilent = !!options.silent; this.isSilent = !!options.isSilent;
this.isTemplate = !!options.isTemplate;
this.onComplete = options.onComplete || function() {}; this.onComplete = options.onComplete || function() {};
this.workingName = options.workingName || 'temp'; this.workingName = options.workingName || 'temp';
source = preprocess(source); source = preprocess(source, options);
this.source = source; this.source = source;
// begin the minification process // begin the minification process
@@ -117,10 +126,19 @@
* @param {Function} callback The function to call once the process completes. * @param {Function} callback The function to call once the process completes.
*/ */
function closureCompile(source, message, callback) { function closureCompile(source, message, callback) {
var options = closureOptions.slice();
// use simple optimizations when minifying template files
if (this.isTemplate) {
options = options.map(function(value) {
return value.replace(/^(compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS');
});
}
// the standard error stream, standard output stream, and Closure Compiler process // the standard error stream, standard output stream, and Closure Compiler process
var error = '', var error = '',
output = '', output = '',
compiler = spawn('java', ['-jar', closurePath].concat(closureOptions)); compiler = spawn('java', ['-jar', closurePath].concat(options));
// juggle arguments // juggle arguments
if (typeof message == 'function') { if (typeof message == 'function') {

View File

@@ -56,13 +56,16 @@
// expose `postprocess` // expose `postprocess`
if (module != require.main) { if (module != require.main) {
module.exports = postprocess; module.exports = postprocess;
} else { }
else {
// read the Lo-Dash source file from the first argument if the script // read the Lo-Dash source file from the first argument if the script
// was invoked directly (e.g. `node post-compile.js source.js`) and write to // was invoked directly (e.g. `node post-compile.js source.js`) and write to
// the same file // the same file
(function() { (function() {
var source = fs.readFileSync(process.argv[2], 'utf8'); var filePath = process.argv[2],
fs.writeFileSync(process.argv[2], postprocess(source), 'utf8'); source = fs.readFileSync(filePath, 'utf8');
fs.writeFileSync(filePath, postprocess(source), 'utf8');
}()); }());
} }
}()); }());

View File

@@ -246,15 +246,22 @@
* Pre-process a given Lo-Dash `source`, preparing it for minification. * Pre-process a given Lo-Dash `source`, preparing it for minification.
* *
* @param {String} source The source to process. * @param {String} source The source to process.
* @param {Object} [options={}] The options object.
* @returns {String} Returns the processed source. * @returns {String} Returns the processed source.
*/ */
function preprocess(source) { function preprocess(source, options) {
// remove copyright to add later in post-compile.js options || (options = {});
source = source.replace(/\/\*![\s\S]+?\*\//, '');
// remove unrecognized JSDoc tags so Closure Compiler won't complain // remove unrecognized JSDoc tags so Closure Compiler won't complain
source = source.replace(/@(?:alias|category)\b.*/g, ''); source = source.replace(/@(?:alias|category)\b.*/g, '');
if (options.isTemplate) {
return source;
}
// remove copyright to add later in post-compile.js
source = source.replace(/\/\*![\s\S]+?\*\//, '');
// add brackets to whitelisted properties so Closure Compiler won't mung them // add brackets to whitelisted properties so Closure Compiler won't mung them
// http://code.google.com/closure/compiler/docs/api-tutorial3.html#export // http://code.google.com/closure/compiler/docs/api-tutorial3.html#export
source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), "['$1']"); source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), "['$1']");
@@ -439,8 +446,12 @@
// was invoked directly (e.g. `node pre-compile.js source.js`) and write to // was invoked directly (e.g. `node pre-compile.js source.js`) and write to
// the same file // the same file
(function() { (function() {
var source = fs.readFileSync(process.argv[2], 'utf8'); var options = process.argv,
fs.writeFileSync(process.argv[2], preprocess(source), 'utf8'); filePath = options[options.length - 1],
isTemplate = options.indexOf('-t') > -1 || options.indexOf('--template') > -1,
source = fs.readFileSync(filePath, 'utf8');
fs.writeFileSync(filePath, preprocess(source, { 'isTemplate': isTemplate }), 'utf8');
}()); }());
} }
}()); }());

3
test/template/a.jst Normal file
View File

@@ -0,0 +1,3 @@
<ul>
<% _.forEach(people, function(name) { %><li><%= name %></li><% }); %>
</ul>

1
test/template/b.jst Normal file
View File

@@ -0,0 +1 @@
<% print("Hello " + epithet); %>.

1
test/template/c.tpl Normal file
View File

@@ -0,0 +1 @@
Hello {{ name }}!