From 18f0df84d7f3f4c7de9b1cacf06a5f5d2f0de8a9 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 3 Sep 2020 15:39:27 +0200 Subject: [PATCH] [DEPENDENCY] Bump terser from 4.8.0 to 5.2.1 (#511) * Bump terser from 4.8.0 to 5.2.1 * Adapt code for terser v5 Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Matthias Osswald --- lib/lbt/bundle/AutoSplitter.js | 5 +---- lib/lbt/bundle/Builder.js | 21 ++++++------------- lib/processors/uglifier.js | 20 +++++++++---------- package-lock.json | 6 +++--- package.json | 2 +- test/lib/lbt/bundle/AutoSplitter.js | 2 +- test/lib/tasks/uglify.js | 31 +++++++++++++++++++++++++++++ 7 files changed, 52 insertions(+), 35 deletions(-) diff --git a/lib/lbt/bundle/AutoSplitter.js b/lib/lbt/bundle/AutoSplitter.js index 766750926..608a74885 100644 --- a/lib/lbt/bundle/AutoSplitter.js +++ b/lib/lbt/bundle/AutoSplitter.js @@ -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? @@ -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; } diff --git a/lib/lbt/bundle/Builder.js b/lib/lbt/bundle/Builder.js index 2f620c1c1..8286f64a6 100644 --- a/lib/lbt/bundle/Builder.js +++ b/lib/lbt/bundle/Builder.js @@ -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 ); @@ -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, @@ -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); @@ -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(); @@ -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(); @@ -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(`(`); } @@ -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(); diff --git a/lib/processors/uglifier.js b/lib/processors/uglifier.js index f5c4c9591..7b6e1ebc9 100644 --- a/lib/processors/uglifier.js +++ b/lib/processors/uglifier.js @@ -22,26 +22,24 @@ const copyrightCommentsAndBundleCommentPattern = /copyright|\(c\)(?:[0-9]+|\s+[0 * @returns {Promise} 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})`); + } })); }; diff --git a/package-lock.json b/package-lock.json index 9d87de24f..2565448a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5341,9 +5341,9 @@ "dev": true }, "terser": { - "version": "4.8.0", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.8.0.tgz", - "integrity": "sha512-EAPipTNeWsb/3wLPeup1tVPaXfIaU68xMnVdPafIL1TV05OhASArYyIfFvnvJCNrR2NIOvDVNNTFRa+Re2MWyw==", + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/terser/-/terser-5.2.1.tgz", + "integrity": "sha512-/AOtjRtAMNGO0fIF6m8HfcvXTw/2AKpsOzDn36tA5RfhRdeXyb4RvHxJ5Pah7iL6dFkLk+gOnCaNHGwJPl6TrQ==", "requires": { "commander": "^2.20.0", "source-map": "~0.6.1", diff --git a/package.json b/package.json index dd90fa863..750f82948 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/lib/lbt/bundle/AutoSplitter.js b/test/lib/lbt/bundle/AutoSplitter.js index b6f69306e..c6a0874e0 100644 --- a/test/lib/lbt/bundle/AutoSplitter.js +++ b/test/lib/lbt/bundle/AutoSplitter.js @@ -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 { diff --git a/test/lib/tasks/uglify.js b/test/lib/tasks/uglify.js index 135743494..509dc350a 100644 --- a/test/lib/tasks/uglify.js +++ b/test/lib/tasks/uglify.js @@ -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"); +});