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..2bf8fb656 --- /dev/null +++ b/packages/core/__tests__/setup-no-match.test.ts @@ -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(); + }); +}); diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index a04b9c34e..50f10fb5c 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 wildcard expansion of the input leaves 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 wildcard expansion of the input leaves 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[] }