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 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: <pattern>`. Explicit empty arrays and in-memory `content` are unaffected.
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
) {
throw new Error(
`No files matched input pattern: ${patterns.join(", ")}`
);
}
}
const optionsWithFormats = enhancedSettings.options as
| { formats?: unknown[] }
Expand Down
Loading