diff --git a/lib/buildTypes/minifyJS.js b/lib/buildTypes/minifyJS.js deleted file mode 100644 index a247995d..00000000 --- a/lib/buildTypes/minifyJS.js +++ /dev/null @@ -1,35 +0,0 @@ -var isFunction = require("lodash").isFunction; - -module.exports = function(source, options) { - return (isFunction(options.minify)) ? - options.minify(source, options) : - uglify(source, options); -}; - -function uglify(source, options) { - var envify = require("loose-envify/replace"); - var UglifyJS = require("uglify-js"); - - var code = source.code; - var opts = (options != null) ? options.uglifyOptions : {}; - - opts = opts || {}; - opts.fromString = true; - - if (source.map) { - var inMap = source.map.toJSON(); - var file = inMap.sources && inMap.sources[0]; - opts.inSourceMap = inMap; - opts.outSourceMap = file; - - if (options.sourceMapsContent) { - opts.sourceMapIncludeSources = true; - } - } - - if(options.envify) { - code = envify(code, [process.env]); - } - - return UglifyJS.minify(code, opts); -} diff --git a/lib/buildTypes/minifyCSS.js b/lib/build_types/minify_css.js similarity index 100% rename from lib/buildTypes/minifyCSS.js rename to lib/build_types/minify_css.js diff --git a/lib/build_types/minify_js.js b/lib/build_types/minify_js.js new file mode 100644 index 00000000..3a0d9117 --- /dev/null +++ b/lib/build_types/minify_js.js @@ -0,0 +1,46 @@ +var assign = require("lodash/assign"); +var isFunction = require("lodash/isFunction"); + +exports.sync = uglify; + +exports.async = function(source, options) { + // use the `minify` function if provided + if (isFunction(options.minify)) { + return Promise.resolve().then(function() { + return options.minify(source, options); + }); + } + + return Promise.resolve() + .then(function() { + return uglify(source, options); + }); +}; + +function uglify(source, options) { + var envify = require("loose-envify/replace"); + var UglifyJS = require("uglify-js"); + + var code = source.code; + var opts = assign({}, options ? options.uglifyOptions : {}, { + fromString: true + }); + + if (source.map) { + var inMap = source.map.toJSON(); + var file = inMap.sources && inMap.sources[0]; + + opts.inSourceMap = inMap; + opts.outSourceMap = file; + + if (opts.sourceMapsContent) { + opts.sourceMapIncludeSources = true; + } + } + + if (options.envify) { + code = envify(code, [process.env]); + } + + return UglifyJS.minify(code, opts); +} diff --git a/lib/bundle/minify_source.js b/lib/bundle/minify_source.js index 2f93bfd2..01383550 100644 --- a/lib/bundle/minify_source.js +++ b/lib/bundle/minify_source.js @@ -1,11 +1,16 @@ -var minifyCSS = require("../buildTypes/minifyCSS"); +var minifiers = { + css: require("../build_types/minify_css"), + js: require("../build_types/minify_js").async +}; module.exports = function(bundle, options) { - options = options || {}; + var opts = options || {}; + var minify = minifiers[bundle.buildType]; - if (options.minify && bundle.buildType === "css") { - return minifyCSS(bundle.source, options); - } + // Minification is optional, but on by default + var shouldMinify = (opts.minify !== false) && !!minify; - return Promise.resolve(bundle.source); + return shouldMinify ? + minify(bundle.source, opts) : + Promise.resolve(bundle.source); }; diff --git a/lib/graph/minify.js b/lib/graph/minify.js index 75b3e9a7..e4e0af14 100644 --- a/lib/graph/minify.js +++ b/lib/graph/minify.js @@ -1,6 +1,6 @@ -var minifyJS = require('../buildTypes/minifyJS'), - transformActiveSource = require("../node/transform_active_source"), - winston = require("winston"); +var winston = require("winston"); +var minifyJS = require("../build_types/minify_js").sync; +var transformActiveSource = require("../node/transform_active_source"); function minify(node, options) { transformActiveSource(node,"minify-true", function(node, source){ diff --git a/lib/graph/transpile.js b/lib/graph/transpile.js index 787805ea..59006649 100644 --- a/lib/graph/transpile.js +++ b/lib/graph/transpile.js @@ -1,6 +1,6 @@ var _ = require("lodash"); var transpile = require('transpile'); -var minify = require("../buildTypes/minifyJS"); +var minify = require("../build_types/minify_js").sync; var processBabelPlugins = require("../process_babel_plugins"); var processBabelPresets = require("../process_babel_presets"); var nodeDependencyMap = require("../node/dependency_map"); diff --git a/lib/node/make_steal_node.js b/lib/node/make_steal_node.js index 0a86066f..6ad4227b 100644 --- a/lib/node/make_steal_node.js +++ b/lib/node/make_steal_node.js @@ -1,10 +1,9 @@ -var fs = require('fs'), - path = require('path'); +var fs = require("fs"); +var path = require("path"); -module.exports = function(configuration){ - var stealPath = path.join(require.resolve("steal"), "/../steal"); - var stealSource = fs.readFileSync(path.join(stealPath + - (configuration.options.minify ? ".production" : "") + ".js"), 'utf8'); +module.exports = function() { + var stealPath = path.join(require.resolve("steal"), "..", "steal"); + var stealSource = fs.readFileSync(path.join(stealPath + ".js"), "utf8"); return { load: { diff --git a/test/minify/config.js b/test/minify/config.js index b9cd4e14..f6935c48 100644 --- a/test/minify/config.js +++ b/test/minify/config.js @@ -1,14 +1,9 @@ -// just to make sure this file is being minified properly - -// this code is a noop meant to force UglifyJS to include the -// `anotherLongVariableName` (with the mangle flag off) in the minified code -// for testing purposes -var anotherLongVariableName; - -function funcName(firstLongName, lastLongName) { - anotherLongVariableName = firstLongName + lastLongName; -} +System.paths.foo = "bar"; -if (anotherLongVariableName == "foo") { console.log(); }; +// make sure this file is minified, too. +// The code below is used to assert uglify-js can take options +var anotherLongObjectName = { + bar: "baz" +}; +module.exports = anotherLongObjectName; -System.paths.foo = "bar"; diff --git a/test/multibuild_test.js b/test/multibuild_test.js index b4ead37d..2ddea4ed 100644 --- a/test/multibuild_test.js +++ b/test/multibuild_test.js @@ -1,11 +1,11 @@ -var asap = require("pdenodeify"), - assert = require("assert"), - comparify = require("comparify"), - fs = require("fs-extra"), - multiBuild = require("../lib/build/multi"), - rmdir = require("rimraf"), - path = require("path"), - testHelpers = require("./helpers"); +var asap = require("pdenodeify"); +var assert = require("assert"); +var comparify = require("comparify"); +var fs = require("fs-extra"); +var multiBuild = require("../lib/build/multi"); +var rmdir = require("rimraf"); +var path = require("path"); +var testHelpers = require("./helpers"); var find = testHelpers.find; var open = testHelpers.open; @@ -232,7 +232,7 @@ describe("multi build", function(){ var actualJS = fs.readFileSync(main, "utf8"); var hasLongVariable = actualJS.indexOf("thisObjectHasABigName") !== -1; - var hasAnotherLongVariable = actualJS.indexOf("anotherLongVariableName") !== -1; + var hasAnotherLongVariable = actualJS.indexOf("anotherLongObjectName") !== -1; assert(hasLongVariable, "Skip mangling names in dependencies graph files"); assert(hasAnotherLongVariable, "skip mangling names in stealconfig and main files"); @@ -1205,7 +1205,8 @@ describe("multi build", function(){ }, close); }, done); - }).catch(done); + }) + .catch(done); }); }); }); @@ -2139,4 +2140,30 @@ describe("multi build", function(){ done(); }); }); + + it("minifies the whole bundle", function() { + return asap(rmdir)(path.join(__dirname, "bundle", "dist")) + .then(function() { + return multiBuild({ + main: "bundle", + config: path.join(__dirname, "bundle", "stealconfig.js") + }, { + quiet: true + }); + }) + .then(function() { + return asap(fs.readFile)( + path.join(__dirname, "bundle", "dist", "bundles", "bundle.js") + ); + }) + .then(function(bundle) { + // matches /*stealconfig.js*/ comment + assert.ok(!/\/\*stealconfig.js\*\//.test(bundle), + "node name comment is removed when the bundle is minified"); + + // matches /*bundle*/ comment + assert.ok(!/\/\*bundle\*\//.test(bundle), + "node name comment is removed when the bundle is minified"); + }); + }); });