From 29ba6de01bcb89a1b4e330c75c66a58ed8c37f7b Mon Sep 17 00:00:00 2001 From: Rodolphe Stoclin Date: Mon, 23 Nov 2026 15:19:10 +0100 Subject: [PATCH 1/2] fix(core): fail when an input glob matches no file --- .changeset/fail-on-glob-with-no-match.md | 7 ++ .../core/__tests__/setup-no-match.test.ts | 77 +++++++++++++++++++ packages/core/src/setup.ts | 17 +++- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 .changeset/fail-on-glob-with-no-match.md create mode 100644 packages/core/__tests__/setup-no-match.test.ts diff --git a/.changeset/fail-on-glob-with-no-match.md b/.changeset/fail-on-glob-with-no-match.md new file mode 100644 index 000000000..660486130 --- /dev/null +++ b/.changeset/fail-on-glob-with-no-match.md @@ -0,0 +1,7 @@ +--- +"@node-minify/core": patch +--- + +Fail when an `input` glob matches no file. + +`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: `. Explicit empty arrays and in-memory `content` are unaffected. diff --git a/packages/core/__tests__/setup-no-match.test.ts b/packages/core/__tests__/setup-no-match.test.ts new file mode 100644 index 000000000..c30f2e9e6 --- /dev/null +++ b/packages/core/__tests__/setup-no-match.test.ts @@ -0,0 +1,77 @@ +/*! + * node-minify + * Copyright (c) 2011-2026 Rodolphe Stoclin + * MIT Licensed + */ + +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", "**", "*.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(); + }); +}); diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index a04b9c34e..fe572d89c 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -22,6 +22,7 @@ const defaultSettings: Partial = { * * @param inputSettings - User-provided settings that override defaults * @returns The validated and enhanced Settings object ready for use + * @throws {Error} When an input glob matches no file */ function setup( inputSettings: Settings @@ -66,6 +67,7 @@ function setup( * * @param settings - The initial settings to enhance * @returns The enhanced Settings object with derived inputs, outputs, and public-folder values applied + * @throws {Error} When an input glob matches no file */ function enhanceSettings( settings: Settings @@ -73,10 +75,23 @@ function enhanceSettings( 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 + ) { + throw new Error( + `No files matched input pattern: ${patterns.join(", ")}` + ); + } } const optionsWithFormats = enhancedSettings.options as | { formats?: unknown[] } From 4d4ca6062e4a5fefe36239250d2ce5cbb119edd0 Mon Sep 17 00:00:00 2001 From: Rodolphe Stoclin Date: Mon, 23 Nov 2026 16:09:36 +0100 Subject: [PATCH 2/2] test(core): unique no-match glob path, precise @throws wording --- packages/core/__tests__/setup-no-match.test.ts | 3 ++- packages/core/src/setup.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/core/__tests__/setup-no-match.test.ts b/packages/core/__tests__/setup-no-match.test.ts index c30f2e9e6..2bf8fb656 100644 --- a/packages/core/__tests__/setup-no-match.test.ts +++ b/packages/core/__tests__/setup-no-match.test.ts @@ -4,6 +4,7 @@ * MIT Licensed */ +import { randomUUID } from "node:crypto"; import os from "node:os"; import path from "node:path"; import type { Compressor } from "@node-minify/types"; @@ -12,7 +13,7 @@ import { setup } from "../src/setup.ts"; const compressor: Compressor = async () => ({ code: "" }); const noMatch = path - .join(os.tmpdir(), "node-minify-no-such-dir", "**", "*.js") + .join(os.tmpdir(), `node-minify-no-such-dir-${randomUUID()}`, "**", "*.js") .replaceAll("\\", "/"); describe("setup glob with no matching files", () => { diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index fe572d89c..50f10fb5c 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -22,7 +22,7 @@ const defaultSettings: Partial = { * * @param inputSettings - User-provided settings that override defaults * @returns The validated and enhanced Settings object ready for use - * @throws {Error} When an input glob matches no file + * @throws {Error} When wildcard expansion of the input leaves no file */ function setup( inputSettings: Settings @@ -67,7 +67,7 @@ function setup( * * @param settings - The initial settings to enhance * @returns The enhanced Settings object with derived inputs, outputs, and public-folder values applied - * @throws {Error} When an input glob matches no file + * @throws {Error} When wildcard expansion of the input leaves no file */ function enhanceSettings( settings: Settings