Skip to content

Commit

Permalink
fix: improve some Terser & Webpack error reporting
Browse files Browse the repository at this point in the history
- 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: #21
  • Loading branch information
brodybits committed Jan 24, 2023
1 parent fd305a4 commit 79a2ac5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/treeshakeWithWebpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export const treeshakeWithWebpack = (code: string): Promise<Output> => {
],
});

// 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);

Expand Down
2 changes: 1 addition & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});

0 comments on commit 79a2ac5

Please sign in to comment.