8000 Tweak build option internals and add test template files. · lodash/lodash@463b5c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 463b5c6

Browse files
committed
Tweak build option internals and add test template files.
Former-commit-id: ed5ec6ed7886a066c8c727de19dc0fe6548a276d
1 parent 65ab5fd commit 463b5c6

File tree

7 files changed

+70
-32
lines changed

7 files changed

+70
-32
lines changed

build.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@
258258
* each template file's basename.
259259
*
260260
* @private
261-
* @param {String} pattern The file path pattern.
262-
* @param {Object} options The options object.
261+
* @param {String} [pattern='<cwd>/*.jst'] The file path pattern.
262+
* @param {Object} [options=_.templateSettings] The options object.
263263
* @returns {String} Returns the compiled source.
264264
*/
265265
function buildTemplate(pattern, options) {
266-
pattern || (pattern = './*.jst');
266+
pattern || (pattern = path.join(cwd, '*.jst'));
267267
options || (options = _.templateSettings);
268268

269269
var directory = path.dirname(pattern);
@@ -289,9 +289,9 @@
289289
);
290290

291291
fs.readdirSync(directory).forEach(function(filename) {
292-
var filepath = path.join(directory, filename);
292+
var filePath = path.join(directory, filename);
293293
if (pattern.test(filename)) {
294-
var text = fs.readFileSync(filepath, 'utf8'),
294+
var text = fs.readFileSync(filePath, 'utf8'),
295295
precompiled = getFunctionSource(_.template(text, null, options)),
296296
prop = filename.replace(/\..*$/, '');
297297

@@ -809,9 +809,9 @@
809809

810810
/**
811811
* Creates a debug and minified build, executing the `callback` for each.
812-
* The `callback` is invoked with 2 arguments; (filepath, source)
812+
* The `callback` is invoked with 2 arguments; (filePath, source)
813813
*
814-
* @param {Array} options The build options array.
814+
* @param {Array} [options=[]] The build options array.
815815
* @param {Function} callback The function called per build.
816816
*/
817817
function build(options, callback) {
@@ -1429,7 +1429,8 @@
14291429
if (!isDebug) {
14301430
workingName += '.min';
14311431
minify(source, {
1432-
'silent': isSilent,
1432+
'isSilent': isSilent,
1433+
'isTemplate': isTemplate,
14331434
'workingName': workingName,
14341435
'onComplete': function(source) {
14351436
// correct overly aggressive Closure Compiler minification
@@ -1458,8 +1459,8 @@
14581459
}
14591460
else {
14601461
// or invoked directly
1461-
build(process.argv, function(source, filepath) {
1462-
filepath && fs.writeFileSync(filepath, source, 'utf8');
1462+
build(process.argv, function(source, filePath) {
1463+
filePath && fs.writeFileSync(filePath, source, 'utf8');
14631464
});
14641465
}
14651466
}());

build/minify.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
spawn = require('child_process').spawn;
1010

1111
/** The directory that is the base of the repository */
12-
var basePath = path.join(__dirname, '../');
12+
var basePath = fs.realpathSync(path.join(__dirname, '../'));
1313

1414
/** The directory where the Closure Compiler is located */
1515
var closurePath = path.join(basePath, 'vendor', 'closure-compiler', 'compiler.jar');
@@ -18,9 +18,9 @@
1818
var distPath = path.join(basePath, 'dist');
1919

2020
/** Load other modules */
21-
var preprocess = require(path.join(__dirname, 'pre-compile')),
22-
postprocess = require(path.join(__dirname, 'post-compile')),
23-
uglifyJS = require(path.join(basePath, 'vendor', 'uglifyjs', 'uglify-js'));
21+
var preprocess = require('./pre-compile'),
22+
postprocess = require('./post-compile'),
23+
uglifyJS = require('../vendor/uglifyjs/uglify-js');
2424

2525
/** Closure Compiler command-line options */
2626
var closureOptions = [
@@ -50,14 +50,22 @@
5050
options = source;
5151
var filePath = options[options.length - 1],
5252
dirPath = path.dirname(filePath),
53-
workingName = path.basename(filePath, '.js') + '.min',
54-
outputPath = path.join(dirPath, workingName + '.js'),
55-
isSilent = options.indexOf('-s') > -1 || options.indexOf('--silent') > -1;
53+
isSilent = options.indexOf('-s') > -1 || options.indexOf('--silent') > -1,
54+
isTemplate = options.indexOf('-t') > -1 || options.indexOf('--template') > -1,
55+
workingName = path.basename(filePath, '.js') + '.min';
5656

57+
workingName = options.reduce(function(result, value, index) {
58+
return /-wn|--working-name/.test(value)
59+
? options[index + 1]
60+
: result;
61+
}, workingName);
62+
63+
var outputPath = path.join(dirPath, workingName + '.js');
5764
source = fs.readFileSync(filePath, 'utf8');
65+
5866
options = {
59-
'silent': isSilent,
60-
'workingName': workingName,
67+
'isSilent': isSilent,
68+
'isTemplate': isTemplate,
6169
'onComplete': function(source) {
6270
fs.writeFileSync(outputPath, source, 'utf8');
6371
}
@@ -71,8 +79,8 @@
7179
*
7280
* @private
7381
* @constructor
74-
* @param {String} source The source to minify.
75-
* @param {Object} options The options object containing `onComplete`,
82+
* @param {String} [source=''] The source to minify.
83+
* @param {Object} [options={}] The options object containing `onComplete`,
7684
* `silent`, and `workingName`.
7785
*/
7886
function Minify(source, options) {
@@ -94,11 +102,12 @@
94102
this.compiled = {};
95103
this.hybrid = {};
96104
this.uglified = {};
97-
this.isSilent = !!options.silent;
105+
this.isSilent = !!options.isSilent;
106+
this.isTemplate = !!options.isTemplate;
98107
this.onComplete = options.onComplete || function() {};
99108
this.workingName = options.workingName || 'temp';
100109

101-
source = preprocess(source);
110+
source = preprocess(source, options);
102111
this.source = source;
103112

104113
// begin the minification process
@@ -117,10 +126,19 @@
117126
* @param {Function} callback The function to call once the process completes.
118127
*/
119128
function closureCompile(source, message, callback) {
129+
var options = closureOptions.slice();
130+
131+
// use simple optimizations when minifying template files
132+
if (this.isTemplate) {
133+
options = options.map(function(value) {
134+
return value.replace(/^(compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS');
135+
});
136+
}
137+
120138
// the standard error stream, standard output stream, and Closure Compiler process
121139
var error = '',
122140
output = '',
123-
compiler = spawn('java', ['-jar', closurePath].concat(closureOptions));
141+
compiler = spawn('java', ['-jar', closurePath].concat(options));
124142

125143
// juggle arguments
126144
if (typeof message == 'function') {

build/post-compile.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@
5656
// expose `postprocess`
5757
if (module != require.main) {
5858
module.exports = postprocess;
59-
} else {
59+
}
60+
else {
6061
// read the Lo-Dash source file from the first argument if the script
6162
// was invoked directly (e.g. `node post-compile.js source.js`) and write to
6263
// the same file
6364
(function() {
64-
var source = fs.readFileSync(process.argv[2], 'utf8');
65-
fs.writeFileSync(process.argv[2], postprocess(source), 'utf8');
65+
var filePath = process.argv[2],
66+
source = fs.readFileSync(filePath, 'utf8');
67+
68+
fs.writeFileSync(filePath, postprocess(source), 'utf8');
6669
}());
6770
}
6871
}());

build/pre-compile.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,22 @@
246246
* Pre-process a given Lo-Dash `source`, preparing it for minification.
247247
*
248248
* @param {String} source The source to process.
249+
* @param {Object} [options={}] The options object.
249250
* @returns {String} Returns the processed source.
250251
*/
251-
function preprocess(source) {
252-
// remove copyright to add later in post-compile.js
253-
source = source.replace(/\/\*![\s\S]+?\*\//, '');
252+
function preprocess(source, options) {
253+
options || (options = {});
254254

255255
// remove unrecognized JSDoc tags so Closure Compiler won't complain
256256
source = source.replace(/@(?:alias|category)\b.*/g, '');
257257

258+
if (options.isTemplate) {
259+
return source;
260+
}
261+
262+
// remove copyright to add later in post-compile.js
263+
source = source.replace(/\/\*![\s\S]+?\*\//, '');
264+
258265
// add brackets to whitelisted properties so Closure Compiler won't mung them
259266
// http://code.google.com/closure/compiler/docs/api-tutorial3.html#export
260267
source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), "['$1']");
@@ -439,8 +446,12 @@
439446
// was invoked directly (e.g. `node pre-compile.js source.js`) and write to
440447
// the same file
441448
(function() {
442-
var source = fs.readFileSync(process.argv[2], 'utf8');
443-
fs.writeFileSync(process.argv[2], preprocess(source), 'utf8');
449+
var options = process.argv,
450+
filePath = options[options.length - 1],
451+
isTemplate = options.indexOf('-t') > -1 || options.indexOf('--template') > -1,
452+
source = fs.readFileSync(filePath, 'utf8');
453+
454+
fs.writeFileSync(filePath, preprocess(source, { 'isTemplate': isTemplate }), 'utf8');
444455
}());
445456
}
446457
}());

test/template/a.jst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<ul>
2+
<% _.forEach(people, function(name) { %><li><%= name %></li><% }); %>
3+
</ul>

test/template/b.jst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<% print("Hello " + epithet); %>.

test/template/c.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello {{ name }}!

0 commit comments

Comments
 (0)
0