Skip to content

Commit

Permalink
Minify JS code at the bundle level too
Browse files Browse the repository at this point in the history
Closes #695
  • Loading branch information
Manuel Mujica committed May 3, 2017
1 parent c6b85e9 commit 2c13be1
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 73 deletions.
35 changes: 0 additions & 35 deletions lib/buildTypes/minifyJS.js

This file was deleted.

File renamed without changes.
46 changes: 46 additions & 0 deletions 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);
}
17 changes: 11 additions & 6 deletions 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);
};
6 changes: 3 additions & 3 deletions 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){
Expand Down
2 changes: 1 addition & 1 deletion 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");
Expand Down
11 changes: 5 additions & 6 deletions 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: {
Expand Down
19 changes: 7 additions & 12 deletions 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";
47 changes: 37 additions & 10 deletions 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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -1205,7 +1205,8 @@ describe("multi build", function(){
}, close);

}, done);
}).catch(done);
})
.catch(done);
});
});
});
Expand Down Expand Up @@ -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");
});
});
});

0 comments on commit 2c13be1

Please sign in to comment.