mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
Rework build.js to work as a module and add a "silent" mode to minify.js.
Former-commit-id: cf62532b957d37da77a2d64aa64d2d388e6382ae
This commit is contained in:
28
build.js
28
build.js
@@ -265,6 +265,7 @@
|
|||||||
' Options:',
|
' Options:',
|
||||||
'',
|
'',
|
||||||
' -h, --help Display help information',
|
' -h, --help Display help information',
|
||||||
|
' -s, --silent Skip printing status updates to the console',
|
||||||
' -V, --version Output current version of Lo-Dash',
|
' -V, --version Output current version of Lo-Dash',
|
||||||
''
|
''
|
||||||
].join('\n'));
|
].join('\n'));
|
||||||
@@ -664,6 +665,9 @@
|
|||||||
// flag used to specify a mobile build
|
// flag used to specify a mobile build
|
||||||
var isMobile = !isLegacy && (isCSP || isUnderscore || options.indexOf('mobile') > -1);
|
var isMobile = !isLegacy && (isCSP || isUnderscore || options.indexOf('mobile') > -1);
|
||||||
|
|
||||||
|
// flag used to specify skipping status updates to the console
|
||||||
|
var isSilent = options.indexOf('-s') > -1 || options.indexOf('--silent') > -1;
|
||||||
|
|
||||||
// flag used to specify `_.bindAll`, `_.extend`, and `_.defaults` are
|
// flag used to specify `_.bindAll`, `_.extend`, and `_.defaults` are
|
||||||
// constructed using the "use strict" directive
|
// constructed using the "use strict" directive
|
||||||
var isStrict = options.indexOf('strict') > -1;
|
var isStrict = options.indexOf('strict') > -1;
|
||||||
@@ -1200,17 +1204,25 @@
|
|||||||
if (filterType || isBackbone || isLegacy || isMobile || isStrict || isUnderscore) {
|
if (filterType || isBackbone || isLegacy || isMobile || isStrict || isUnderscore) {
|
||||||
callback(path.join(cwd, 'lodash.custom.js'), debugSource);
|
callback(path.join(cwd, 'lodash.custom.js'), debugSource);
|
||||||
|
|
||||||
minify(source, 'lodash.custom.min', function(source) {
|
minify(source, {
|
||||||
if (isStrict) {
|
'silent': isSilent,
|
||||||
// inject "use strict" directive
|
'workingName': 'lodash.custom.min',
|
||||||
source = source.replace(/^(\/\*![\s\S]+?\*\/\n;\(function[^)]+\){)([^'"])/, '$1"use strict";$2');
|
'onComplete': function(source) {
|
||||||
|
if (isStrict) {
|
||||||
|
// inject "use strict" directive
|
||||||
|
source = source.replace(/^(\/\*![\s\S]+?\*\/\n;\(function[^)]+\){)([^'"])/, '$1"use strict";$2');
|
||||||
|
}
|
||||||
|
callback(path.join(cwd, 'lodash.custom.min.js'), postMinify(source));
|
||||||
}
|
}
|
||||||
callback(path.join(cwd, 'lodash.custom.min.js'), postMinify(source));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
minify(source, 'lodash.min', function(source) {
|
minify(source, {
|
||||||
callback(path.join(cwd, 'lodash.min.js'), postMinify(source));
|
'silent': isSilent,
|
||||||
|
'workingName': 'lodash.min',
|
||||||
|
'onComplete': function(source) {
|
||||||
|
callback(path.join(cwd, 'lodash.min.js'), postMinify(source));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1224,7 +1236,7 @@
|
|||||||
else {
|
else {
|
||||||
// or invoked directly
|
// or invoked directly
|
||||||
build(process.argv.slice(2), function(filepath, source) {
|
build(process.argv.slice(2), function(filepath, source) {
|
||||||
fs.writeFileSync(filepath, source);
|
fs.writeFileSync(filepath, source, 'utf8');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|||||||
@@ -38,11 +38,11 @@
|
|||||||
* the `onComplete` callback when finished.
|
* the `onComplete` callback when finished.
|
||||||
*
|
*
|
||||||
* @param {String} source The source to minify.
|
* @param {String} source The source to minify.
|
||||||
* @param {String} workingName The name to give temporary files creates during the minification process.
|
* @param {Object} options The options object containing `onComplete`, `silent`,
|
||||||
* @param {Function} onComplete A function called when minification has completed.
|
* and `workingName`.
|
||||||
*/
|
*/
|
||||||
function minify(source, workingName, onComplete) {
|
function minify(source, options) {
|
||||||
new Minify(source, workingName, onComplete);
|
new Minify(source, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,10 +51,17 @@
|
|||||||
* @private
|
* @private
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {String} source The source to minify.
|
* @param {String} source The source to minify.
|
||||||
* @param {String} workingName The name to give temporary files creates during the minification process.
|
* @param {Object} options The options object containing `onComplete`, `silent`,
|
||||||
* @param {Function} onComplete A function called when minification has completed.
|
* and `workingName`.
|
||||||
*/
|
*/
|
||||||
function Minify(source, workingName, onComplete) {
|
function Minify(source, options) {
|
||||||
|
source || (source = '');
|
||||||
|
options || (options = {});
|
||||||
|
|
||||||
|
if (typeof source != 'string') {
|
||||||
|
options = source || options;
|
||||||
|
source = options.source || '';
|
||||||
|
}
|
||||||
// create the destination directory if it doesn't exist
|
// create the destination directory if it doesn't exist
|
||||||
if (!fs.existsSync(distPath)) {
|
if (!fs.existsSync(distPath)) {
|
||||||
// avoid errors when called as a npm executable
|
// avoid errors when called as a npm executable
|
||||||
@@ -63,12 +70,15 @@
|
|||||||
} catch(e) { }
|
} catch(e) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
source = preprocess(source);
|
||||||
|
|
||||||
this.compiled = {};
|
this.compiled = {};
|
||||||
this.hybrid = {};
|
this.hybrid = {};
|
||||||
this.uglified = {};
|
this.uglified = {};
|
||||||
this.onComplete = onComplete;
|
this.isSilent = !!options.silent;
|
||||||
this.source = source = preprocess(source);
|
this.onComplete = options.onComplete || function() {};
|
||||||
this.workingName = workingName;
|
this.source = source;
|
||||||
|
this.workingName = options.workingName || 'temp';
|
||||||
|
|
||||||
// begin the minification process
|
// begin the minification process
|
||||||
closureCompile.call(this, source, onClosureCompile.bind(this));
|
closureCompile.call(this, source, onClosureCompile.bind(this));
|
||||||
@@ -97,10 +107,12 @@
|
|||||||
message = null;
|
message = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(message == null
|
if (!this.isSilent) {
|
||||||
? 'Compressing ' + this.workingName + ' using the Closure Compiler...'
|
console.log(message == null
|
||||||
: message
|
? 'Compressing ' + this.workingName + ' using the Closure Compiler...'
|
||||||
);
|
: message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
compiler.stdout.on('data', function(data) {
|
compiler.stdout.on('data', function(data) {
|
||||||
// append the data to the output stream
|
// append the data to the output stream
|
||||||
@@ -148,10 +160,12 @@
|
|||||||
message = null;
|
message = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(message == null
|
if (!this.isSilent) {
|
||||||
? 'Compressing ' + this.workingName + ' using UglifyJS...'
|
console.log(message == null
|
||||||
: message
|
? 'Compressing ' + this.workingName + ' using UglifyJS...'
|
||||||
);
|
: message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = ugly.gen_code(
|
result = ugly.gen_code(
|
||||||
@@ -202,9 +216,12 @@
|
|||||||
if (exception) {
|
if (exception) {
|
||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
|
if (!this.isSilent) {
|
||||||
|
console.log('Done. Size: %d bytes.', result.length);
|
||||||
|
}
|
||||||
|
|
||||||
// store the gzipped result and report the size
|
// store the gzipped result and report the size
|
||||||
this.compiled.gzip = result;
|
this.compiled.gzip = result;
|
||||||
console.log('Done. Size: %d bytes.', result.length);
|
|
||||||
|
|
||||||
// next, minify the source using only UglifyJS
|
// next, minify the source using only UglifyJS
|
||||||
uglify.call(this, this.source, onUglify.bind(this));
|
uglify.call(this, this.source, onUglify.bind(this));
|
||||||
@@ -237,11 +254,13 @@
|
|||||||
if (exception) {
|
if (exception) {
|
||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
|
if (!this.isSilent) {
|
||||||
|
console.log('Done. Size: %d bytes.', result.length);
|
||||||
|
}
|
||||||
var message = 'Compressing ' + this.workingName + ' using hybrid minification...';
|
var message = 'Compressing ' + this.workingName + ' using hybrid minification...';
|
||||||
|
|
||||||
// store the gzipped result and report the size
|
// store the gzipped result and report the size
|
||||||
this.uglified.gzip = result;
|
this.uglified.gzip = result;
|
||||||
console.log('Done. Size: %d bytes.', result.length);
|
|
||||||
|
|
||||||
// next, minify the Closure Compiler minified source using UglifyJS
|
// next, minify the Closure Compiler minified source using UglifyJS
|
||||||
uglify.call(this, this.compiled.source, message, onHybrid.bind(this));
|
uglify.call(this, this.compiled.source, message, onHybrid.bind(this));
|
||||||
@@ -274,9 +293,11 @@
|
|||||||
if (exception) {
|
if (exception) {
|
||||||
throw exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
|
if (!this.isSilent) {
|
||||||
|
console.log('Done. Size: %d bytes.', result.length);
|
||||||
|
}
|
||||||
// store the gzipped result and report the size
|
// store the gzipped result and report the size
|
||||||
this.hybrid.gzip = result;
|
this.hybrid.gzip = result;
|
||||||
console.log('Done. Size: %d bytes.', result.length);
|
|
||||||
|
|
||||||
// finish by choosing the smallest compressed file
|
// finish by choosing the smallest compressed file
|
||||||
onComplete.call(this);
|
onComplete.call(this);
|
||||||
@@ -333,13 +354,24 @@
|
|||||||
// was invoked directly (e.g. `node minify.js source.js`) and write to
|
// was invoked directly (e.g. `node minify.js source.js`) and write to
|
||||||
// `<filename>.min.js`
|
// `<filename>.min.js`
|
||||||
(function() {
|
(function() {
|
||||||
var filePath = process.argv[2],
|
var options = process.argv;
|
||||||
|
if (options.length < 3) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var filePath = options[options.length - 1],
|
||||||
dirPath = path.dirname(filePath),
|
dirPath = path.dirname(filePath),
|
||||||
|
outputPath = path.join(dirPath, workingName + '.js'),
|
||||||
|
isSilent = options.indexOf('-s') > -1 || options.indexOf('--silent') > -1,
|
||||||
source = fs.readFileSync(filePath, 'utf8'),
|
source = fs.readFileSync(filePath, 'utf8'),
|
||||||
workingName = path.basename(filePath, '.js') + '.min';
|
workingName = path.basename(filePath, '.js') + '.min';
|
||||||
|
|
||||||
minify(source, workingName, function(result) {
|
minify(source, {
|
||||||
fs.writeFileSync(path.join(dirPath, workingName + '.js'), result, 'utf8');
|
'silent': isSilent,
|
||||||
|
'workingName': workingName,
|
||||||
|
'onComplete': function(source) {
|
||||||
|
fs.writeFileSync(outputPath, source, 'utf8');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user