Skip to content

Commit

Permalink
[FEATURE] BundleBuilder: support modules using ES6 with usePredefineC…
Browse files Browse the repository at this point in the history
…alls (#67)

When using the bundle option usePredefineCalls modules using ES6
features such as arrow functions, classes, ... are not supported
and the bundler will break as the mozilla-ast of uglify-es is not
ES6 ready.
  • Loading branch information
petermuessig authored Aug 4, 2018
1 parent 427515f commit d1a4f1f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 86 deletions.
61 changes: 21 additions & 40 deletions lib/lbt/bundle/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const uglify = require("uglify-es");
const {pd} = require("pretty-data");
const esprima = require("esprima");
const escodegen = require("escodegen");
const {Syntax} = esprima;
// const MOZ_SourceMap = require("source-map");

Expand Down Expand Up @@ -321,46 +322,25 @@ class BundleBuilder {

compressJS(fileContent, resource) {
if ( this.optimize ) {
if ( typeof fileContent === "object" && fileContent.type in Syntax ) {
let uglifyAST = uglify.AST_Node.from_mozilla_ast(fileContent);
let result = uglify.minify(uglifyAST, {
warnings: false, // TODO configure?
compress: false, // TODO configure?
output: {
comments: copyrightCommentsPattern
}
// , outFileName: resource.name
// , outSourceMap: true
});
if ( result.error ) {
throw result.error;
let result = uglify.minify({
[resource.name]: String(fileContent)
}, {
warnings: false, // TODO configure?
compress: false, // TODO configure?
output: {
comments: copyrightCommentsPattern
}
// console.log(result.map);
// const map = new MOZ_SourceMap.SourceMapConsumer(result.map);
// map.eachMapping(function (m) { console.log(m); }); // console.log(map);
fileContent = result.code;
// throw new Error();
} else {
let result = uglify.minify({
[resource.name]: String(fileContent)
}, {
warnings: false, // TODO configure?
compress: false, // TODO configure?
output: {
comments: copyrightCommentsPattern
}
// , outFileName: resource.name
// , outSourceMap: true
});
if ( result.error ) {
throw result.error;
}
// console.log(result.map);
// const map = new MOZ_SourceMap.SourceMapConsumer(result.map);
// map.eachMapping(function (m) { console.log(m); }); // console.log(map);
fileContent = result.code;
// throw new Error();
// , outFileName: resource.name
// , outSourceMap: true
});
if ( result.error ) {
throw result.error;
}
// console.log(result.map);
// const map = new MOZ_SourceMap.SourceMapConsumer(result.map);
// map.eachMapping(function (m) { console.log(m); }); // console.log(map);
fileContent = result.code;
// throw new Error();
}
return fileContent;
}
Expand All @@ -382,14 +362,15 @@ class BundleBuilder {
let remaining = [];
for ( let module of sequence ) {
if ( /\.js$/.test(module) ) {
// console.log("trying to read " + module);
// console.log("Processing " + module);
let resource = await this.pool.findResourceWithInfo(module);
let code = await resource.buffer();
const ast = rewriteDefine(this.targetBundleFormat, code, module, avoidLazyParsing);
if ( ast ) {
outW.startSegment(module);
outW.ensureNewLine();
const fileContent = this.compressJS( ast, resource );
let astAsCode = escodegen.generate(ast);
const fileContent = this.compressJS(astAsCode, resource);
outW.write( fileContent );
outW.ensureNewLine();
const compressedSize = outW.endSegment();
Expand Down
88 changes: 42 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"@ui5/fs": "^0.2.0",
"@ui5/logger": "^0.2.0",
"archiver": "^2.1.0",
"escodegen": "^1.11.0",
"escope": "^3.6.0",
"esprima": "^2.7.2",
"estraverse": "^4.2.0",
Expand Down

0 comments on commit d1a4f1f

Please sign in to comment.