Skip to content

Commit

Permalink
Use uglify-es to support minification of more permissive babel presets
Browse files Browse the repository at this point in the history
Currently, only ES5 javascript can be minified, due to the reliance on
uglify-js. With uglify-es (the harmony branch of UglifyJS2) it is
possible to support a broader range of javascript syntax.

The API changed between uglify-js@2 and uglify-js@3 / uglify-es, but the
translation to the new API was fairly straightforward. It hews more
closely to the original code than systemjs#815, and consequently I think it
avoids regressing any features (for instance, the ability to include
sourceContents in source maps or to control the comment stripping.

As browsers continue to support more features beyond ES5, and
because babel recommends use of the 'env' preset, it will become
increasingly likely that users' babel transpilation settings will cause
errors when trying to minify with jspm and friends.

refs systemjs#815, systemjs#726
  • Loading branch information
fdintino committed Aug 23, 2018
1 parent c00faa6 commit 2686f47
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
39 changes: 22 additions & 17 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ var extend = require('./utils').extend;
var fromFileURL = require('./utils').fromFileURL;
var toFileURL = require('./utils').toFileURL;

// Ugly hack (no pun intended) to get around uglify-js strange code loading
// and export system, and the fact that their SourceMap wrapper is no longer
// exported in uglify-js@3 / uglify-es
var UglifySourceMap = new Function('MOZ_SourceMap', function() {
var code = ['uglify-es/lib/utils.js', 'uglify-es/lib/sourcemap.js'].map(function(file) {
return fs.readFileSync(require.resolve(file), 'utf8');
});
code.push('return SourceMap;');
return code.join('\n');
}())(require('source-map'));

function countLines(str) {
return str.split(/\r\n|\r|\n/).length;
}
Expand Down Expand Up @@ -72,36 +83,30 @@ function createOutput(outFile, outputs, basePath, sourceMaps, sourceMapContents)
}

function minify(output, fileName, mangle, uglifyOpts) {
var uglify = require('uglify-js');
var uglify = require('uglify-es');
var files = {};
files[fileName] = output.source;
var ast;
try{
ast = uglify.parse(output.source, { filename: fileName });
ast = uglify.minify(files, {
parse: {},
compress: false,
mangle: mangle,
output: { ast: true, code: false }
}).ast;
} catch(e){
throw new Error(e);
}
ast.figure_out_scope();

ast = ast.transform(uglify.Compressor(uglifyOpts.compress));
ast.figure_out_scope();
if (mangle !== false)
ast.mangle_names();

var sourceMap;
if (output.sourceMap) {
if (typeof output.sourceMap === 'string')
output.sourceMap = JSON.parse(output.sourceMap);

var sourceMapIn = output.sourceMap;
sourceMap = uglify.SourceMap({
sourceMap = UglifySourceMap({
file: fileName,
orig: sourceMapIn
orig: output.sourceMap
});

if (uglifyOpts.sourceMapIncludeSources && sourceMapIn && Array.isArray(sourceMapIn.sourcesContent)) {
sourceMapIn.sourcesContent.forEach(function(content, idx) {
sourceMap.get().setSourceContent(sourceMapIn.sources[idx], content);
});
}
}

var outputOptions = uglifyOpts.beautify;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"source-map": "^0.5.3",
"systemjs": "^0.19.46",
"traceur": "0.0.105",
"uglify-js": "^2.6.1"
"uglify-es": "^3.3.9"
},
"devDependencies": {
"babel": "^5.8.38",
Expand Down
3 changes: 1 addition & 2 deletions test/test-sfx.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
<div id="mocha">
<script>
window.$ = { jquery: 1 };
window.DEBUG = false;
</script>
<script src="output/sfx.js"></script>
<script>
mocha.setup('tdd');

DEBUG = false;

function assert(assertion, msg) {
if (!assertion)
throw new Error(msg || 'Assertion failed');
Expand Down

0 comments on commit 2686f47

Please sign in to comment.