Skip to content

Commit

Permalink
Make build.js work as a npm executable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Jul 11, 2012
1 parent 10886be commit 568e70d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
9 changes: 6 additions & 3 deletions build.js
Expand Up @@ -8,6 +8,9 @@
vm = require('vm'),
minify = require(path.join(__dirname, 'build', 'minify'));

/** The current working directory */
var cwd = process.cwd();

/** Flag used to specify a backbone build */
var isBackbone = process.argv.indexOf('backbone') > -1;

Expand Down Expand Up @@ -871,14 +874,14 @@

// begin the minification process
if (filterType || isBackbone || isLegacy || isMobile) {
fs.writeFileSync(path.join(__dirname, 'lodash.custom.js'), source);
fs.writeFileSync(path.join(cwd, 'lodash.custom.js'), source);
minify(source, 'lodash.custom.min', function(result) {
fs.writeFileSync(path.join(__dirname, 'lodash.custom.min.js'), result);
fs.writeFileSync(path.join(cwd, 'lodash.custom.min.js'), result);
});
}
else {
minify(source, 'lodash.min', function(result) {
fs.writeFileSync(path.join(__dirname, 'lodash.min.js'), result);
fs.writeFileSync(path.join(cwd, 'lodash.min.js'), result);
});
}
}());
32 changes: 19 additions & 13 deletions build/minify.js
Expand Up @@ -35,8 +35,8 @@
/*--------------------------------------------------------------------------*/

/**
* The exposed `minify` function minifies a given `source` and invokes the
* `onComplete` callback when finished.
* The exposed `minify` function minifies a given Lo-Dash `source` and invokes
* the `onComplete` callback when finished.
*
* @param {String} source The source to minify.
* @param {String} workingName The name to give temporary files creates during the minification process.
Expand All @@ -58,7 +58,10 @@
function Minify(source, workingName, onComplete) {
// create the destination directory if it doesn't exist
if (!fs.existsSync(distPath)) {
fs.mkdirSync(distPath);
// avoid errors when called as a npm executable
try {
fs.mkdirSync(distPath);
} catch(e) { }
}

this.compiled = {};
Expand Down Expand Up @@ -291,17 +294,20 @@
name = this.workingName,
uglified = this.uglified;

// save the Closure Compiled version to disk
fs.writeFileSync(path.join(distPath, name + '.compiler.js'), compiled.source);
fs.writeFileSync(path.join(distPath, name + '.compiler.js.gz'), compiled.gzip);
// avoid errors when called as a npm executable
try {
// save the Closure Compiled version to disk
fs.writeFileSync(path.join(distPath, name + '.compiler.js'), compiled.source);
fs.writeFileSync(path.join(distPath, name + '.compiler.js.gz'), compiled.gzip);

// save the Uglified version to disk
fs.writeFileSync(path.join(distPath, name + '.uglify.js'), uglified.source);
fs.writeFileSync(path.join(distPath, name + '.uglify.js.gz'), uglified.gzip);
// save the Uglified version to disk
fs.writeFileSync(path.join(distPath, name + '.uglify.js'), uglified.source);
fs.writeFileSync(path.join(distPath, name + '.uglify.js.gz'), uglified.gzip);

// save the hybrid minified version to disk
fs.writeFileSync(path.join(distPath, name + '.hybrid.js'), hybrid.source);
fs.writeFileSync(path.join(distPath, name + '.hybrid.js.gz'), hybrid.gzip);
// save the hybrid minified version to disk
fs.writeFileSync(path.join(distPath, name + '.hybrid.js'), hybrid.source);
fs.writeFileSync(path.join(distPath, name + '.hybrid.js.gz'), hybrid.gzip);
} catch(e) { }

// select the smallest gzipped file and use its minified counterpart as the
// official minified release (ties go to Closure Compiler)
Expand All @@ -324,7 +330,7 @@
module.exports = minify;
}
else {
// read the JavaScript 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 minify.js source.js`) and write to
// the same file
(function() {
Expand Down
2 changes: 1 addition & 1 deletion build/post-compile.js
Expand Up @@ -56,7 +56,7 @@
if (module != require.main) {
module.exports = postprocess;
} else {
// read the JavaScript 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
// the same file
(function() {
Expand Down
4 changes: 2 additions & 2 deletions build/pre-compile.js
Expand Up @@ -196,7 +196,7 @@
/*--------------------------------------------------------------------------*/

/**
* 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.
* @returns {String} Returns the processed source.
Expand Down Expand Up @@ -361,7 +361,7 @@
module.exports = preprocess;
}
else {
// read the JavaScript 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 pre-compile.js source.js`) and write to
// the same file
(function() {
Expand Down

0 comments on commit 568e70d

Please sign in to comment.