Skip to content
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
7 changes: 7 additions & 0 deletions .changeset/oxc-report-parse-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@node-minify/oxc": patch
---

Fix the oxc compressor ignoring parse errors reported by `oxc-minify`.

A JavaScript syntax error used to surface as `No content provided`, or as a silent success when `allowEmptyOutput` was set. It now fails with `oxc minification failed: <oxc's message>`.
50 changes: 50 additions & 0 deletions packages/oxc/__tests__/oxc-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,54 @@ describe("Package: oxc error handling", () => {
oxc({ settings: {}, content: "var x = 1;" })
).rejects.toThrow("oxc");
});

test("should throw when oxc reports an error", async () => {
vi.doMock("oxc-minify", () => ({
minify: async () => ({
code: "",
errors: [
{
severity: "Error",
message: "Unexpected token",
labels: [],
helpMessage: null,
codeframe: null,
},
],
}),
}));

const { oxc } = await import("../src/index.ts");

await expect(
// @ts-expect-error testing invalid input: settings is missing required fields
oxc({ settings: {}, content: "var a = ;" })
).rejects.toThrow("oxc minification failed: Unexpected token");
});

test("should not throw when oxc reports only warnings", async () => {
vi.doMock("oxc-minify", () => ({
minify: async () => ({
code: "var x=1;",
errors: [
{
severity: "Warning",
message: "w",
labels: [],
helpMessage: null,
codeframe: null,
},
],
}),
}));

const { oxc } = await import("../src/index.ts");

const result = await (
// @ts-expect-error testing invalid input: settings is missing required fields
oxc({ settings: {}, content: "var x = 1;" })
);

expect(result.code).toBe("var x=1;");
});
});
8 changes: 3 additions & 5 deletions packages/oxc/__tests__/oxc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ describe("Package: oxc", async () => {
output: filesJS.fileJSOut,
};

try {
return await minify(settings);
} catch (err) {
return expect(err).not.toBeNull();
}
await expect(minify(settings)).rejects.toThrow(
"oxc minification failed"
);
});
});
9 changes: 9 additions & 0 deletions packages/oxc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { minify as oxcMinify } from "oxc-minify";
* @param settings - Minifier settings; `settings.options` are forwarded to oxc-minify (sourcemap enabled when `options.sourceMap` is truthy).
* @param content - Input to minify; converted to a string before minification.
* @returns The minified output: `code` contains the minified JavaScript, `map` contains the source map when available.
* @throws {Error} When oxc reports a parse error
*/
export async function oxc({
settings,
Expand All @@ -35,6 +36,14 @@ export async function oxc({
...restOptions,
});

// String(): Severity is an ambient const enum, unusable under isolatedModules
const errors = result.errors.filter(
(error) => String(error.severity) === "Error"
);
if (errors.length > 0) {
throw new Error(errors.map((error) => error.message).join("; "));
}

validateMinifyResult(result, "oxc");

return {
Expand Down
Loading