From 6246e71c872ca51967a8397c0dae85e7a812514c Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Thu, 26 Apr 2012 19:56:31 -0600 Subject: [PATCH 1/2] Use the Node `zlib` module instead of shelling out to `gzip`. Former-commit-id: f73339f6fd421ec4ddd1ec13ef13a18926419cdb --- build.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/build.js b/build.js index e95919b5d..4ddd662cb 100755 --- a/build.js +++ b/build.js @@ -2,9 +2,10 @@ ;(function() { 'use strict'; - /** The Node filesystem, path, and child process modules */ + /** The Node filesystem, path, `zlib`, and child process modules */ var fs = require('fs'), path = require('path'), + gzip = require('zlib').gzip, spawn = require('child_process').spawn; /** The build directory containing the build scripts */ @@ -34,9 +35,6 @@ '--warning_level=QUIET' ]; - /** Gzip command-line options */ - var gzipOptions = ['-9f', '-c']; - /** The pre-processed Lo-Dash source */ var source = preprocess(fs.readFileSync(path.join(__dirname, 'lodash.js'), 'utf8')); @@ -159,7 +157,7 @@ } // store the post-processed Closure Compiler result and gzip it accumulator.compiled.source = result = postprocess(result); - invoke('gzip', gzipOptions, result, 'binary', onClosureGzip); + gzip(result, onClosureGzip); } /** @@ -167,7 +165,7 @@ * * @private * @param {Object|Undefined} exception The error object. - * @param {String} result The resulting gzipped source. + * @param {Buffer} result The resulting gzipped source. */ function onClosureGzip(exception, result) { if (exception) { @@ -194,7 +192,7 @@ } // store the post-processed Uglified result and gzip it accumulator.uglified.source = result = postprocess(result); - invoke('gzip', gzipOptions, result, 'binary', onUglifyGzip); + gzip(result, onUglifyGzip); } /** @@ -202,7 +200,7 @@ * * @private * @param {Object|Undefined} exception The error object. - * @param {String} result The resulting gzipped source. + * @param {Buffer} result The resulting gzipped source. */ function onUglifyGzip(exception, result) { if (exception) { @@ -227,12 +225,11 @@ // save the Closure Compiled version to disk fs.writeFileSync(path.join(distPath, 'lodash.compiler.js'), compiled.source); - // explicit 'binary' is necessary to ensure the stream is written correctly - fs.writeFileSync(path.join(distPath, 'lodash.compiler.js.gz'), compiled.gzip, 'binary'); + fs.writeFileSync(path.join(distPath, 'lodash.compiler.js.gz'), compiled.gzip); // save the Uglified version to disk fs.writeFileSync(path.join(distPath, 'lodash.uglify.js'), uglified.source); - fs.writeFileSync(path.join(distPath, 'lodash.uglify.js.gz'), uglified.gzip, 'binary'); + fs.writeFileSync(path.join(distPath, 'lodash.uglify.js.gz'), uglified.gzip); // select the smallest gzipped file and use its minified counterpart as the // official minified release (ties go to Closure Compiler) From 09d8561b3c20cd785d2774b825b5dda1b366de94 Mon Sep 17 00:00:00 2001 From: Kit Cambridge Date: Thu, 26 Apr 2012 20:06:47 -0600 Subject: [PATCH 2/2] `build.js`: Merge `invoke()` into `closureCompile()`. Former-commit-id: 0fbdeffeba06c49db1d8b0801c8e3b721c3a5a43 --- build.js | 82 +++++++++++++++++++------------------------------------- 1 file changed, 28 insertions(+), 54 deletions(-) diff --git a/build.js b/build.js index 4ddd662cb..302837fb4 100755 --- a/build.js +++ b/build.js @@ -40,59 +40,6 @@ /*--------------------------------------------------------------------------*/ - /** - * Invokes a process with the given `name`, `parameters`, and `source` (used as - * the standard input). Yields the result to a `callback` function. The optional - * `encoding` argument specifies the output stream encoding. - * - * @private - * @param {String} name The name of the process. - * @param {Array} parameters An array of arguments to proxy to the process. - * @param {String} source The standard input to proxy to the process. - * @param {String} [encoding] The expected encoding of the output stream. - * @param {Function} callback The function to call once the process completes. - */ - function invoke(name, parameters, source, encoding, callback) { - // the standard error stream, standard output stream, and process instance - var error = '', - output = '', - process = spawn(name, parameters); - - // juggle arguments - if (typeof encoding == 'string') { - // explicitly set the encoding of the output stream if one is specified - process.stdout.setEncoding(encoding); - } else { - callback = encoding; - encoding = null; - } - - process.stdout.on('data', function(data) { - // append the data to the output stream - output += data; - }); - - process.stderr.on('data', function(data) { - // append the error message to the error stream - error += data; - }); - - process.on('exit', function(status) { - var exception = null; - // `status` contains the process exit code - if (status) { - exception = new Error(error); - exception.status = status; - } - callback(exception, output); - }); - - // proxy the standard input to the process - process.stdin.end(source); - } - - /*--------------------------------------------------------------------------*/ - /** * Compresses a `source` string using the Closure Compiler. Yields the * minified result, and any exceptions encountered, to a `callback` function. @@ -103,7 +50,34 @@ */ function closureCompile(source, callback) { console.log('Compressing lodash.js using the Closure Compiler...'); - invoke('java', ['-jar', closurePath].concat(closureOptions), source, callback); + + // the standard error stream, standard output stream, and Closure Compiler process + var error = '', + output = '', + compiler = spawn('java', ['-jar', closurePath].concat(closureOptions)); + + compiler.stdout.on('data', function(data) { + // append the data to the output stream + output += data; + }); + + compiler.stderr.on('data', function(data) { + // append the error message to the error stream + error += data; + }); + + compiler.on('exit', function(status) { + var exception = null; + // `status` contains the process exit code + if (status) { + exception = new Error(error); + exception.status = status; + } + callback(exception, output); + }); + + // proxy the standard input to the Closure Compiler + compiler.stdin.end(source); } /**