Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minify JS code at the bundle level too
Closes #695
- Loading branch information
Manuel Mujica
committed
May 3, 2017
1 parent
c6b85e9
commit 2c13be1
Showing
9 changed files
with
110 additions
and
73 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters