Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump terser from 4.8.0 to 5.2.1 #511

Merged
merged 2 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/lbt/bundle/AutoSplitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class AutoSplitter {
let fileContent = await resource.buffer();
if ( this.optimize ) {
// console.log("uglify %s start", module);
const result = terser.minify({
const result = await terser.minify({
[resource.name]: String(fileContent)
}, {
warnings: false, // TODO configure?
Expand All @@ -211,9 +211,6 @@ class AutoSplitter {
// , outFileName: resource.name
// , outSourceMap: true
});
if ( result.error ) {
throw result.error;
}
// console.log("uglify %s end", module);
fileContent = result.code;
}
Expand Down
21 changes: 6 additions & 15 deletions lib/lbt/bundle/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class BundleBuilder {
async writeRawModule(module, resource) {
let fileContent = await resource.buffer();
if ( /\.js$/.test(module) ) {
fileContent = this.compressJS( fileContent, resource );
fileContent = await this.compressJS( fileContent, resource );
}
this.outW.ensureNewLine();
this.outW.write( fileContent );
Expand Down Expand Up @@ -304,12 +304,11 @@ class BundleBuilder {
// this.afterWriteFunctionPreloadSection();
}

compressJS(fileContent, resource) {
async compressJS(fileContent, resource) {
if ( this.optimize ) {
const result = terser.minify({
const result = await terser.minify({
[resource.name]: String(fileContent)
}, {
warnings: false, // TODO configure?
compress: false, // TODO configure?
output: {
comments: copyrightCommentsPattern,
Expand All @@ -318,9 +317,6 @@ class BundleBuilder {
// , 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);
Expand All @@ -330,11 +326,6 @@ class BundleBuilder {
return fileContent;
}

async compressJSAsync(resource) {
const content = await resource.buffer();
return this.compressJS( content, resource );
}

beforeWriteFunctionPreloadSection(sequence) {
// simple version: just sort alphabetically
sequence.sort();
Expand All @@ -355,7 +346,7 @@ class BundleBuilder {
outW.startSegment(module);
outW.ensureNewLine();
const astAsCode = escodegen.generate(ast);
const fileContent = this.compressJS(astAsCode, resource);
const fileContent = await this.compressJS(astAsCode, resource);
outW.write( fileContent );
outW.ensureNewLine();
const compressedSize = outW.endSegment();
Expand Down Expand Up @@ -393,7 +384,7 @@ class BundleBuilder {
const outW = this.outW;

if ( /\.js$/.test(module) && (info == null || !info.requiresTopLevelScope) ) {
const compressedContent = await this.compressJSAsync( resource );
const compressedContent = await this.compressJS( await resource.buffer(), resource );
if ( avoidLazyParsing ) {
outW.write(`(`);
}
Expand All @@ -408,7 +399,7 @@ class BundleBuilder {
} else if ( /\.js$/.test(module) /* implicitly: && info != null && info.requiresTopLevelScope */ ) {
log.warn("**** warning: module %s requires top level scope" +
" and can only be embedded as a string (requires 'eval')", module);
const compressedContent = await this.compressJSAsync( resource );
const compressedContent = await this.compressJS( await resource.buffer(), resource );
outW.write( makeStringLiteral( compressedContent ) );
} else if ( /\.html$/.test(module) ) {
const fileContent = await resource.buffer();
Expand Down
20 changes: 9 additions & 11 deletions lib/processors/uglifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,24 @@ const copyrightCommentsAndBundleCommentPattern = /copyright|\(c\)(?:[0-9]+|\s+[0
* @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving with uglified resources
*/
module.exports = function({resources}) {
return Promise.all(resources.map((resource) => {
return resource.getString().then((code) => {
const result = terser.minify({
return Promise.all(resources.map(async (resource) => {
const code = await resource.getString();
try {
const result = await terser.minify({
[resource.getPath()]: code
}, {
warnings: false,
output: {
comments: copyrightCommentsAndBundleCommentPattern,
wrap_func_args: false
},
compress: false
});
if (result.error) {
throw new Error(
`Uglification failed with error: ${result.error.message} in file ${result.error.filename} ` +
`(line ${result.error.line}, col ${result.error.col}, pos ${result.error.pos})`);
}

resource.setString(result.code);
return resource;
});
} catch (err) {
throw new Error(
`Uglification failed with error: ${err.message} in file ${err.filename} ` +
`(line ${err.line}, col ${err.col}, pos ${err.pos})`);
}
}));
};
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"rimraf": "^3.0.2",
"semver": "^7.3.2",
"slash": "^3.0.0",
"terser": "^4.8.0",
"terser": "^5.2.1",
"xml2js": "^0.4.23",
"yazl": "^2.5.1"
},
Expand Down
2 changes: 1 addition & 1 deletion test/lib/lbt/bundle/AutoSplitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ test("_calcMinSize: js resource", async (t) => {


test.serial("_calcMinSize: uglify js resource", async (t) => {
const stubTerser = sinon.stub(terser, "minify").returns({code: "123"});
const stubTerser = sinon.stub(terser, "minify").resolves({code: "123"});
const pool = {
findResourceWithInfo: function() {
return {
Expand Down
31 changes: 31 additions & 0 deletions test/lib/tasks/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,34 @@ function test(t){var o=t;console.log(o)}test();`;
return t.deepEqual(buffer.toString(), expected, "Correct content");
});
});

test("integration: uglify error handling", async (t) => {
const reader = resourceFactory.createAdapter({
virBasePath: "/"
});
const writer = resourceFactory.createAdapter({
virBasePath: "/"
});
const duplexCollection = new DuplexCollection({reader: reader, writer: writer});
const content = `
this code can't be parsed!`;
const testResource = resourceFactory.createResource({
path: "/test.js",
string: content
});

await reader.write(testResource);

const error = await t.throwsAsync(uglify({
workspace: duplexCollection,
options: {
pattern: "/test.js"
}
}));

t.regex(error.message, /Uglification failed with error/, "Error should contain expected message");
t.regex(error.message, /test\.js/, "Error should contain filename");
t.regex(error.message, /col/, "Error should contain col");
t.regex(error.message, /pos/, "Error should contain pos");
t.regex(error.message, /line/, "Error should contain line");
});