Skip to content

Commit b658794

Browse files
fix: expose root in transform args now that it's gone on app.config
1 parent 379dc87 commit b658794

4 files changed

Lines changed: 31 additions & 23 deletions

File tree

src/dev/builder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { bundleJs } from "./esbuild.ts";
77
import { liveReload } from "./middlewares/live_reload.ts";
88
import {
99
cssAssetHash,
10-
FreshFileTransformer,
10+
FileTransformer,
1111
type OnTransformOptions,
1212
} from "./file_transformer.ts";
1313
import type { TransformFn } from "./file_transformer.ts";
@@ -93,7 +93,7 @@ const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
9393

9494
// deno-lint-ignore no-explicit-any
9595
export class Builder<State = any> {
96-
#transformer = new FreshFileTransformer(fsAdapter);
96+
#transformer: FileTransformer;
9797
#addedInternalTransforms = false;
9898
config: ResolvedBuildConfig;
9999
#islandSpecifiers = new Set<string>();
@@ -109,6 +109,8 @@ export class Builder<State = any> {
109109

110110
this.#fsRoutes = { dir: routeDir, files: [], id: "default" };
111111

112+
this.#transformer = new FileTransformer(fsAdapter, root);
113+
112114
this.config = {
113115
target: options?.target ?? ["chrome99", "firefox99", "safari15"],
114116
root,

src/dev/dev_build_cache_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from "@std/expect";
22
import { MemoryBuildCache } from "./dev_build_cache.ts";
3-
import { FreshFileTransformer } from "./file_transformer.ts";
3+
import { FileTransformer } from "./file_transformer.ts";
44
import { createFakeFs, withTmpDir } from "../test_utils.ts";
55
import type { ResolvedBuildConfig } from "./builder.ts";
66

@@ -20,7 +20,7 @@ Deno.test({
2020
staticDir: "",
2121
target: "latest",
2222
};
23-
const fileTransformer = new FreshFileTransformer(createFakeFs({}));
23+
const fileTransformer = new FileTransformer(createFakeFs({}), tmp);
2424
const buildCache = new MemoryBuildCache(
2525
config,
2626
{ dir: "", files: [], id: "" },

src/dev/file_transformer.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface OnTransformArgs {
2323
text: string;
2424
content: Uint8Array;
2525
mode: TransformMode;
26+
root: string;
2627
}
2728
export type TransformFn = (
2829
args: OnTransformArgs,
@@ -56,21 +57,14 @@ interface TransformReq {
5657
inputFiles: string[];
5758
}
5859

59-
export interface FileTransformer {
60-
onTransform(options: OnTransformOptions, callback: TransformFn): void;
61-
process(
62-
filePath: string,
63-
mode: TransformMode,
64-
target: string | string[],
65-
): Promise<ProcessedFile[] | null>;
66-
}
67-
68-
export class FreshFileTransformer implements FileTransformer {
60+
export class FileTransformer {
6961
#transformers: Transformer[] = [];
7062
#fs: FsAdapter;
63+
#root: string;
7164

72-
constructor(fs: FsAdapter) {
65+
constructor(fs: FsAdapter, root: string) {
7366
this.#fs = fs;
67+
this.#root = root;
7468
}
7569

7670
onTransform(options: OnTransformOptions, callback: TransformFn): void {
@@ -156,6 +150,7 @@ export class FreshFileTransformer implements FileTransformer {
156150
mode,
157151
target,
158152
content: req!.content,
153+
root: this.#root,
159154
get text() {
160155
return new TextDecoder().decode(req!.content);
161156
},
@@ -252,7 +247,7 @@ export class FreshFileTransformer implements FileTransformer {
252247

253248
const CSS_URL_REGEX = /url\(("[^"]+"|'[^']+'|[^)]+)\)/g;
254249

255-
export function cssAssetHash(transformer: FreshFileTransformer) {
250+
export function cssAssetHash(transformer: FileTransformer) {
256251
transformer.onTransform({
257252
pluginName: "fresh-css",
258253
filter: /\.css$/,

src/dev/file_transformer_test.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { expect } from "@std/expect";
22
import type { FsAdapter } from "../fs.ts";
3-
import {
4-
FreshFileTransformer,
5-
type ProcessedFile,
6-
} from "./file_transformer.ts";
3+
import { FileTransformer, type ProcessedFile } from "./file_transformer.ts";
74
import { delay } from "../test_utils.ts";
85

9-
function testTransformer(files: Record<string, string>) {
6+
function testTransformer(files: Record<string, string>, root = "/") {
107
const mockFs: FsAdapter = {
11-
cwd: () => "/",
8+
cwd: () => root,
129
isDirectory: () => Promise.resolve(false),
1310
mkdirp: () => Promise.resolve(),
1411
walk: async function* foo() {
@@ -21,7 +18,7 @@ function testTransformer(files: Record<string, string>) {
2118
return Promise.resolve(buf);
2219
},
2320
};
24-
return new FreshFileTransformer(mockFs);
21+
return new FileTransformer(mockFs, root);
2522
}
2623

2724
function consumeResult(result: ProcessedFile[]) {
@@ -228,3 +225,17 @@ Deno.test(
228225
]);
229226
},
230227
);
228+
229+
Deno.test("FileTransformer - pass root to args", async () => {
230+
const transformer = testTransformer({ "foo.txt": "foo" }, "/<root>/");
231+
232+
let root = "";
233+
transformer.onTransform({ pluginName: "A", filter: /.*/ }, (args) => {
234+
root = args.root;
235+
return undefined;
236+
});
237+
238+
await transformer.process("foo.txt", "development", "");
239+
240+
expect(root).toEqual("/<root>/");
241+
});

0 commit comments

Comments
 (0)