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:
@@ -1,17 +1,17 @@
|
|||||||
import type { TailwindPluginOptions } from "./types.ts";
|
import type { TailwindPluginOptions } from "./types.ts";
|
||||||
import { initTailwind } from "./compiler.ts";
|
import { initTailwind } from "./compiler.ts";
|
||||||
import type { Builder } from "fresh/dev";
|
import type { FreshBuilder } from "fresh/dev";
|
||||||
import type { App } from "fresh";
|
import type { App } from "fresh";
|
||||||
|
|
||||||
export function tailwind<T>(
|
export function tailwind<T>(
|
||||||
builder: Builder,
|
builder: FreshBuilder,
|
||||||
app: App<T>,
|
app: App<T>,
|
||||||
options: TailwindPluginOptions = {},
|
options: TailwindPluginOptions = {},
|
||||||
): void {
|
): void {
|
||||||
let processor: ReturnType<typeof initTailwind> | null;
|
let processor: ReturnType<typeof initTailwind> | null;
|
||||||
|
|
||||||
builder.onTransformStaticFile(
|
builder.onTransformStaticFile(
|
||||||
{ pluginName: "tailwind", filter: /\.css$/ },
|
{ pluginName: "tailwind", filter: /\.css$/, exclude: options.exclude },
|
||||||
async (args) => {
|
async (args) => {
|
||||||
if (!processor) processor = initTailwind(app.config, options);
|
if (!processor) processor = initTailwind(app.config, options);
|
||||||
const instance = await processor;
|
const instance = await processor;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import type { OnTransformOptions } from "@fresh/core/dev";
|
||||||
|
|
||||||
export interface AutoprefixerOptions {
|
export interface AutoprefixerOptions {
|
||||||
/** environment for `Browserslist` */
|
/** environment for `Browserslist` */
|
||||||
env?: string;
|
env?: string;
|
||||||
@@ -41,4 +43,6 @@ export interface AutoprefixerOptions {
|
|||||||
|
|
||||||
export interface TailwindPluginOptions {
|
export interface TailwindPluginOptions {
|
||||||
autoprefixer?: AutoprefixerOptions;
|
autoprefixer?: AutoprefixerOptions;
|
||||||
|
/** Exclude paths or globs that should not be processed */
|
||||||
|
exclude?: OnTransformOptions["exclude"];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -159,3 +159,53 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "Builder - exclude files",
|
||||||
|
fn: async () => {
|
||||||
|
const logs: string[] = [];
|
||||||
|
const builder = new Builder();
|
||||||
|
|
||||||
|
// String
|
||||||
|
builder.onTransformStaticFile(
|
||||||
|
{ pluginName: "A", filter: /\.css$/, exclude: ["foo.css"] },
|
||||||
|
(args) => {
|
||||||
|
logs.push(`A: ${path.basename(args.path)}`);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Regex
|
||||||
|
builder.onTransformStaticFile(
|
||||||
|
{ pluginName: "B", filter: /\.css$/, exclude: [/foo\.css$/] },
|
||||||
|
(args) => {
|
||||||
|
logs.push(`B: ${path.basename(args.path)}`);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Glob
|
||||||
|
builder.onTransformStaticFile(
|
||||||
|
{ pluginName: "C", filter: /\.css$/, exclude: ["**/foo.css"] },
|
||||||
|
(args) => {
|
||||||
|
logs.push(`C: ${path.basename(args.path)}`);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const tmp = await Deno.makeTempDir();
|
||||||
|
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
|
||||||
|
await Deno.writeTextFile(
|
||||||
|
path.join(tmp, "bar.css"),
|
||||||
|
"body { color: blue; }",
|
||||||
|
);
|
||||||
|
const app = new App({
|
||||||
|
staticDir: tmp,
|
||||||
|
build: {
|
||||||
|
outDir: path.join(tmp, "dist"),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await builder.build(app);
|
||||||
|
|
||||||
|
expect(logs).toEqual(["A: bar.css", "B: bar.css", "C: bar.css"]);
|
||||||
|
},
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { globToRegExp, isGlob } from "@std/path";
|
||||||
import type { FsAdapter } from "../fs.ts";
|
import type { FsAdapter } from "../fs.ts";
|
||||||
import { BUILD_ID } from "../runtime/build_id.ts";
|
import { BUILD_ID } from "../runtime/build_id.ts";
|
||||||
import { assetInternal } from "../runtime/shared_internal.tsx";
|
import { assetInternal } from "../runtime/shared_internal.tsx";
|
||||||
@@ -7,6 +8,7 @@ export type TransformMode = "development" | "production";
|
|||||||
export interface OnTransformOptions {
|
export interface OnTransformOptions {
|
||||||
pluginName: string;
|
pluginName: string;
|
||||||
filter: RegExp;
|
filter: RegExp;
|
||||||
|
exclude?: Array<string | RegExp>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OnTransformResult {
|
export interface OnTransformResult {
|
||||||
@@ -112,7 +114,7 @@ export class FreshFileTransformer {
|
|||||||
seen.add(req.filePath);
|
seen.add(req.filePath);
|
||||||
|
|
||||||
let transformed = false;
|
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 transformer = this.#transformers[i];
|
||||||
|
|
||||||
const { options, fn } = transformer;
|
const { options, fn } = transformer;
|
||||||
@@ -121,6 +123,25 @@ export class FreshFileTransformer {
|
|||||||
continue;
|
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({
|
const result = await fn({
|
||||||
path: req.filePath,
|
path: req.filePath,
|
||||||
mode,
|
mode,
|
||||||
|
|||||||
Reference in New Issue
Block a user