From 79a2ac5aa714ca63569b83f096a1ffe4cf3f93dc Mon Sep 17 00:00:00 2001 From: "Christopher J. Brody" Date: Tue, 24 Jan 2023 14:30:11 -0500 Subject: [PATCH] fix: improve some Terser & Webpack error reporting - show result with error info from Terser if needed - show informative message instead of an internal error in case there is no code for Webpack to process (temporary workaround solution) ref: https://github.com/brodybits/rollup-plugin-size-snapshot/issues/21 --- src/index.js | 12 ++++++++---- src/treeshakeWithWebpack.js | 5 +++++ tests/index.test.js | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index f74a775..410a439 100644 --- a/src/index.js +++ b/src/index.js @@ -77,14 +77,18 @@ export const sizeSnapshot = (options?: Options = {}): Plugin => { const outputName = chunk.fileName; - // Improvement with better error handling was proposed in + // Cleaner error reporting was discussed in // brodybits/rollup-plugin-size-snapshot#17 // but a reproduction is needed to add a test case, see // https://github.com/brodybits/rollup-plugin-size-snapshot/issues/19 - const minified = minify(source).code; - if (!minified) { + const minifyResult = minify(source); + const minified = minifyResult.code; + if (!minified && minified !== "") { + // TODO needs test case with a reproduction, see + // https://github.com/brodybits/rollup-plugin-size-snapshot/issues/19 throw new Error( - "INTERNAL ERROR - terser error - see https://github.com/brodybits/rollup-plugin-size-snapshot/issues/19" + "INTERNAL ERROR - terser error - see https://github.com/brodybits/rollup-plugin-size-snapshot/issues/19 " + + JSON.stringify(minifyResult) ); } diff --git a/src/treeshakeWithWebpack.js b/src/treeshakeWithWebpack.js index ac52c3c..e9247ee 100644 --- a/src/treeshakeWithWebpack.js +++ b/src/treeshakeWithWebpack.js @@ -38,6 +38,11 @@ export const treeshakeWithWebpack = (code: string): Promise => { ], }); + // TEMPORARY WORKAROUND SOLUTION: + if (code === "") { + return Promise.reject(new Error("no minified code for Webpack to process")); + } + inputFS.writeFileSync(`/${inputName}`, `import {} from '/${bundleName}'`); inputFS.writeFileSync(`/${bundleName}`, code); diff --git a/tests/index.test.js b/tests/index.test.js index b011837..ff21299 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -445,5 +445,5 @@ test("reproduce failure for esm with comments only ref: brodybits/rollup-plugin- plugins: [sizeSnapshot({ snapshotPath })], output: { file: path.resolve("fixtures/output.js"), format: "esm" }, }); - }).rejects.toThrow(/INTERNAL ERROR - terser error/); // was "No content" in v0.13.2 + }).rejects.toThrow(/no minified code for Webpack to process/); });