feat(core): support exclude option for transformers (#2960)

This PR adds general support for an `exclude` option to file
transformers. This allows plugin to skip processing excluded files. The
exclude array can be a `string`, a `RegExp` or a glob pattern.

Fixes https://github.com/denoland/fresh/issues/2937
This commit is contained in:
Marvin Hagemeister
2025-05-16 15:17:43 +02:00
committed by GitHub
parent 107c25b79a
commit e996b5e2fe
4 changed files with 79 additions and 4 deletions
+22 -1
View File
@@ -1,3 +1,4 @@
import { globToRegExp, isGlob } from "@std/path";
import type { FsAdapter } from "../fs.ts";
import { BUILD_ID } from "../runtime/build_id.ts";
import { assetInternal } from "../runtime/shared_internal.tsx";
@@ -7,6 +8,7 @@ export type TransformMode = "development" | "production";
export interface OnTransformOptions {
pluginName: string;
filter: RegExp;
exclude?: Array<string | RegExp>;
}
export interface OnTransformResult {
@@ -112,7 +114,7 @@ export class FreshFileTransformer {
seen.add(req.filePath);
let transformed = false;
for (let i = 0; i < this.#transformers.length; i++) {
outer: for (let i = 0; i < this.#transformers.length; i++) {
const transformer = this.#transformers[i];
const { options, fn } = transformer;
@@ -121,6 +123,25 @@ export class FreshFileTransformer {
continue;
}
// Check if file is excluded
if (options.exclude !== undefined) {
for (let j = 0; j < options.exclude.length; j++) {
const exclude = options.exclude[j];
if (exclude instanceof RegExp) {
if (exclude.test(filePath)) {
continue outer;
}
} else if (isGlob(exclude)) {
const regex = globToRegExp(exclude);
if (regex.test(filePath)) {
continue outer;
}
} else if (filePath.includes(exclude)) {
continue outer;
}
}
}
const result = await fn({
path: req.filePath,
mode,