Skip to content

Commit

Permalink
fix(bundle): support both Uglify v3 and v2.
Browse files Browse the repository at this point in the history
Keep supporting Uglify v2.
Fix error when using Uglify v3.
Update `lib/dependencies.json` uglify-js version to latest 3.0.19.

fixes #636
  • Loading branch information
3cp committed Jun 23, 2017
1 parent cc65ec9 commit 7b606ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,36 @@ exports.Bundle = class {
console.log(`Writing ${bundleFileName}...`);

if (buildOptions.isApplicable('minify')) {
let minificationOptions = Object.assign({ fromString: true }, buildOptions.getValue('minify'));

const UglifyJS = require('uglify-js'); //added to user project devDependencies
// fromString is not a supported option in v3
const isV3 = !!(UglifyJS.minify('', {fromString: true}).error);

let minificationOptions = {};
if (!isV3) minificationOptions.fromString = true;

let minifyOptions = buildOptions.getValue('minify');
if (typeof minifyOptions === 'object') {
Object.assign(minificationOptions, minifyOptions);
}

if (needsSourceMap) {
minificationOptions.inSourceMap = Convert.fromJSON(concat.sourceMap).toObject();
minificationOptions.outSourceMap = mapFileName;
minificationOptions.sourceRoot = mapSourceRoot;
if (isV3) {
minificationOptions.sourceMap = {
content: concat.sourceMap,
filename: bundleFileName,
url: mapFileName,
root: mapSourceRoot
};
} else {
minificationOptions.inSourceMap = Convert.fromJSON(concat.sourceMap).toObject();
minificationOptions.outSourceMap = mapFileName;
minificationOptions.sourceRoot = mapSourceRoot;
}
}

const UglifyJS = require('uglify-js'); //added to user project devDependencies
let minificationResult = UglifyJS.minify(String(contents), minificationOptions);
if (minificationResult.error) throw minificationResult.error;

contents = minificationResult.code;
mapContents = needsSourceMap ? Convert.fromJSON(minificationResult.map).toJSON() : undefined;
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"@types/jasmine": "^2.2.0",
"@types/node": "^6.0.45",
"typescript": ">=1.9.0-dev || ^2.0.0",
"uglify-js": "^2.6.3",
"uglify-js": "^3.0.19",
"vinyl-fs": "^2.4.3"
}

0 comments on commit 7b606ab

Please sign in to comment.