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/fail-on-glob-with-no-match.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@node-minify/core": patch
---

Fail when no input file is left after glob expansion.

`minify()` used to resolve with an empty string and write nothing when a wildcard input such as `src/**/*.js` matched nothing and the output used `$1`; with a plain output it failed with `No content provided`. Both cases now throw `No files matched input pattern: <pattern>`. An array where at least one entry matches keeps working, and unmatched globs in it are skipped as before. Explicit empty arrays and in-memory `content` are unaffected.
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>`.
74 changes: 19 additions & 55 deletions packages/core/__tests__/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ describe("Package: core", async () => {
output: filesJS.fileJSOut,
};

try {
return await minify(settings);
} catch (err: unknown) {
if (err instanceof Error) {
return expect(err.toString()).toEqual(
"Error: compressor should be a function, maybe you forgot to install the compressor"
);
}
}
await expect(minify(settings)).rejects.toThrow(
"compressor should be a function, maybe you forgot to install the compressor"
);
});
});

Expand All @@ -58,15 +52,9 @@ describe("Package: core", async () => {
output: filesJS.fileJSOut,
};

try {
return await minify(settings);
} catch (err: unknown) {
if (err instanceof Error) {
return expect(err.toString()).toEqual(
"Error: compressor is mandatory."
);
}
}
await expect(minify(settings)).rejects.toThrow(
"compressor is mandatory."
);
});

test("should throw an error if no input", async () => {
Expand All @@ -75,15 +63,9 @@ describe("Package: core", async () => {
output: filesJS.fileJSOut,
};

try {
return await minify(settings);
} catch (err: unknown) {
if (err instanceof Error) {
return expect(err.toString()).toEqual(
"Error: input is mandatory."
);
}
}
await expect(minify(settings)).rejects.toThrow(
"input is mandatory."
);
});

test("should throw an error if no output", async () => {
Expand All @@ -92,36 +74,24 @@ describe("Package: core", async () => {
input: filesJS.oneFileWithWildcards,
};

try {
return await minify(settings);
} catch (err: unknown) {
if (err instanceof Error) {
return expect(err.toString()).toEqual(
"Error: output is mandatory."
);
}
}
await expect(minify(settings)).rejects.toThrow(
"output is mandatory."
);
});
});

describe("Mandatory", () => {
test("should show throw on type option", async () => {
test("should require a compressor even when type is set", async () => {
const settings: Settings = {
// @ts-expect-error testing invalid input: "uglifyjs" is not a valid FileType ("js" | "css")
type: "uglifyjs",
input: filesJS.oneFileWithWildcards,
output: filesJS.fileJSOut,
};

try {
return await minify(settings);
} catch (err: unknown) {
if (err instanceof Error) {
return expect(err.toString()).toEqual(
"Error: compressor is mandatory."
);
}
}
await expect(minify(settings)).rejects.toThrow(
"compressor is mandatory."
);
});
});

Expand Down Expand Up @@ -167,15 +137,9 @@ describe("Package: core", async () => {
"<html lang='en'><body><div>content</div></body></html>",
};

try {
return await minify(settings);
} catch (err: unknown) {
if (err instanceof Error) {
return expect(err.toString()).toEqual(
"Error: compressor should be a function, maybe you forgot to install the compressor"
);
}
}
await expect(minify(settings)).rejects.toThrow(
"compressor should be a function, maybe you forgot to install the compressor"
);
});
});

Expand Down
78 changes: 78 additions & 0 deletions packages/core/__tests__/setup-no-match.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*!
* node-minify
* Copyright (c) 2011-2026 Rodolphe Stoclin
* MIT Licensed
*/

import { randomUUID } from "node:crypto";
import os from "node:os";
import path from "node:path";
import type { Compressor } from "@node-minify/types";
import { describe, expect, test } from "vitest";
import { setup } from "../src/setup.ts";

const compressor: Compressor = async () => ({ code: "" });
const noMatch = path
.join(os.tmpdir(), `node-minify-no-such-dir-${randomUUID()}`, "**", "*.js")
.replaceAll("\\", "/");

describe("setup glob with no matching files", () => {
test("throws when a string glob with $1 output matches no file", () => {
expect(() =>
setup({
compressor,
input: noMatch,
output: "out/$1.min.js",
})
).toThrow("No files matched input pattern");
});

test("throws when a string glob with plain output matches no file", () => {
expect(() =>
setup({
compressor,
input: noMatch,
output: "out.js",
})
).toThrow("No files matched input pattern");
});

test("throws when an array containing only an unmatched glob is given", () => {
expect(() =>
setup({
compressor,
input: [noMatch],
output: "out.js",
})
).toThrow("No files matched input pattern");
});

test("includes the unmatched pattern in the error message", () => {
expect(() =>
setup({
compressor,
input: noMatch,
output: "out.js",
})
).toThrow(noMatch);
});

test("does not throw from setup for a non-glob missing file", () => {
expect(() =>
setup({
compressor,
input: "missing.js",
output: "out.js",
})
).not.toThrow();
});

test("does not throw from setup in in-memory mode", () => {
expect(() =>
setup({
compressor,
content: "var a = 1;",
})
).not.toThrow();
});
});
17 changes: 16 additions & 1 deletion packages/core/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const defaultSettings: Partial<Settings> = {
*
* @param inputSettings - User-provided settings that override defaults
* @returns The validated and enhanced Settings object ready for use
* @throws {Error} When wildcard expansion of the input leaves no file
*/
function setup<T extends CompressorOptions = CompressorOptions>(
inputSettings: Settings<T>
Expand Down Expand Up @@ -66,17 +67,31 @@ function setup<T extends CompressorOptions = CompressorOptions>(
*
* @param settings - The initial settings to enhance
* @returns The enhanced Settings object with derived inputs, outputs, and public-folder values applied
* @throws {Error} When wildcard expansion of the input leaves no file
*/
function enhanceSettings<T extends CompressorOptions = CompressorOptions>(
settings: Settings<T>
): Settings<T> {
let enhancedSettings = settings;

if (enhancedSettings.input) {
const requestedInput = enhancedSettings.input;
enhancedSettings = {
...enhancedSettings,
...wildcards(enhancedSettings.input, enhancedSettings.publicFolder),
...wildcards(requestedInput, enhancedSettings.publicFolder),
};

const patterns = [requestedInput].flat();
const hasWildcard = patterns.some((pattern) => pattern.includes("*"));
if (
hasWildcard &&
Array.isArray(enhancedSettings.input) &&
enhancedSettings.input.length === 0
Comment thread
srod marked this conversation as resolved.
Resolved
) {
throw new Error(
`No files matched input pattern: ${patterns.join(", ")}`
);
}
}
const optionsWithFormats = enhancedSettings.options as
| { formats?: unknown[] }
Expand Down
8 changes: 3 additions & 5 deletions packages/cssnano/__tests__/cssnano.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ describe("Package: cssnano", async () => {
output: filesCSS.fileCSSOut,
};

try {
return await minify(settings);
} catch (err) {
return expect(err).not.toBeNull();
}
await expect(minify(settings)).rejects.toThrow(
"cssnano minification failed"
);
});
});
14 changes: 4 additions & 10 deletions packages/esbuild/__tests__/esbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ describe("Package: esbuild", async () => {
type: "js",
};

try {
return await minify(settings);
} catch (err) {
return expect(err).not.toBeNull();
}
await expect(minify(settings)).rejects.toThrow("Transform failed");
});
});

Expand Down Expand Up @@ -100,10 +96,8 @@ describe("Package: esbuild", async () => {
output: filesJS.fileJSOut,
};

try {
return await minify(settings);
} catch (err) {
return expect(err).toBeInstanceOf(Error);
}
await expect(minify(settings)).rejects.toThrow(
"You must specify a type: js or css"
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ describe("Package: google-closure-compiler", async () => {
output: filesJS.fileJSOut,
};

try {
return await minify(settings);
} catch (err) {
return expect(err).not.toBeNull();
}
await expect(minify(settings)).rejects.toThrow("exited with code");
});

test("should compress with boolean options", async (): Promise<void> => {
Expand Down
6 changes: 1 addition & 5 deletions packages/lightningcss/__tests__/lightningcss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ describe("Package: lightningcss", async () => {
output: filesCSS.fileCSSOut,
};

try {
return await minify(settings);
} catch (err) {
return expect(err).not.toBeNull();
}
await expect(minify(settings)).rejects.toThrow(SyntaxError);
});

test("should throw an error for array content", async () => {
Expand Down
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"
);
});
});
Loading
Loading