From 5b87896031cb21ba51e8a72885579cc3cf344317 Mon Sep 17 00:00:00 2001 From: Rodolphe Stoclin Date: Mon, 23 Nov 2026 15:17:21 +0100 Subject: [PATCH] fix(oxc): report oxc parse errors instead of an empty result --- .changeset/oxc-report-parse-errors.md | 7 ++++ packages/oxc/__tests__/oxc-error.test.ts | 50 ++++++++++++++++++++++++ packages/oxc/__tests__/oxc.test.ts | 8 ++-- packages/oxc/src/index.ts | 9 +++++ 4 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 .changeset/oxc-report-parse-errors.md diff --git a/.changeset/oxc-report-parse-errors.md b/.changeset/oxc-report-parse-errors.md new file mode 100644 index 000000000..6c99bc18c --- /dev/null +++ b/.changeset/oxc-report-parse-errors.md @@ -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: `. diff --git a/packages/oxc/__tests__/oxc-error.test.ts b/packages/oxc/__tests__/oxc-error.test.ts index 92e738b23..5e5cb58c6 100644 --- a/packages/oxc/__tests__/oxc-error.test.ts +++ b/packages/oxc/__tests__/oxc-error.test.ts @@ -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;"); + }); }); diff --git a/packages/oxc/__tests__/oxc.test.ts b/packages/oxc/__tests__/oxc.test.ts index 54ad1d11e..231098404 100644 --- a/packages/oxc/__tests__/oxc.test.ts +++ b/packages/oxc/__tests__/oxc.test.ts @@ -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" + ); }); }); diff --git a/packages/oxc/src/index.ts b/packages/oxc/src/index.ts index dda7be28d..b1ddcd05c 100644 --- a/packages/oxc/src/index.ts +++ b/packages/oxc/src/index.ts @@ -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, @@ -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 {