Skip to content

Commit

Permalink
feat: use terser to replace uglifyjs for better es6 support
Browse files Browse the repository at this point in the history
Terser is the current active fork of uglify-es. This is not only required to support babel targeting es6, but also improve compatibility of any vendor js code which uses "const".

closes #883, #490, supersedes #864
  • Loading branch information
3cp committed Jul 27, 2018
1 parent dea6281 commit 588ce58
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
31 changes: 12 additions & 19 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const path = require('path');
const os = require('os');
const Terser = require('terser');
const Convert = require('./convert-source-map');
const fs = require('../file-system');
const SourceInclusion = require('./source-inclusion').SourceInclusion;
Expand Down Expand Up @@ -291,34 +292,26 @@ exports.Bundle = class {
}

if (buildOptions.isApplicable('minify')) {
const UglifyJS = require('uglify-js');
// fromString is not a supported option in v3
const isV3 = !!(UglifyJS.minify('', {fromString: true}).error);

let minificationOptions = {};
if (!isV3) minificationOptions.fromString = true;
// Terser fast minify mode
// https://github.com/fabiosantoscode/terser#terser-fast-minify-mode
// It's a good balance on size and speed to turn off compress.
let minificationOptions = {compress: true};

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

if (needsSourceMap) {
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;
}
minificationOptions.sourceMap = {
content: concat.sourceMap,
filename: bundleFileName,
url: mapFileName,
root: mapSourceRoot
};
}

let minificationResult = UglifyJS.minify(String(contents), minificationOptions);
let minificationResult = Terser.minify(String(contents), minificationOptions);
if (minificationResult.error) throw minificationResult.error;

contents = minificationResult.code;
Expand Down
1 change: 0 additions & 1 deletion lib/commands/new/project-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ exports.ProjectTemplate = class {
'gulp',
'minimatch',
'through2',
'uglify-js',
'vinyl-fs'
);

Expand Down
1 change: 0 additions & 1 deletion lib/dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
"ts-loader": "^4.0.1",
"tslint": "^5.9.1",
"typescript": "^2.7.2",
"uglify-js": "^3.3.15",
"url-loader": "^1.0.1",
"vinyl-fs": "^3.0.2",
"wait-on": "^2.1.0",
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@
"aurelia-dependency-injection": "^1.0.0",
"aurelia-logging": "^1.2.0",
"aurelia-polyfills": "^1.0.0",
"babel-polyfill": "^6.0.0 || ^7.0.0 || ^8.0.0",
"babel-register": "^6.0.0 || ^7.0.0 || ^8.0.0",
"esprima": "^4.0.0",
"glob": "^7.1.1",
"gulp": "^4.0.0",
"npm": "^6.1.0",
"npm-which": "^3.0.1",
"opn": "^5.0.0",
"preprocess": "^3.1.0",
"rfc6902": "^1.2.2",
"semver": "^5.3.0",
"typescript": "^1.0.0 || ^2.0.0 || ^3.0.0",
"opn": "^5.0.0",
"babel-register": "^6.0.0 || ^7.0.0 || ^8.0.0",
"babel-polyfill": "^6.0.0 || ^7.0.0 || ^8.0.0"
"terser": "^3.8.1",
"typescript": "^1.0.0 || ^2.0.0 || ^3.0.0"
},
"devDependencies": {
"aurelia-tools": "^0.2.4",
Expand Down

0 comments on commit 588ce58

Please sign in to comment.