Skip to content
Closed
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
3 changes: 1 addition & 2 deletions src/dev/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
type ListenOptions,
setBuildCache,
} from "../app.ts";
import { fsAdapter } from "../fs.ts";
import * as path from "@std/path";
import * as colors from "@std/fmt/colors";
import { bundleJs } from "./esbuild.ts";
Expand Down Expand Up @@ -45,7 +44,7 @@ export interface FreshBuilder {
}

export class Builder implements FreshBuilder {
#transformer = new FreshFileTransformer(fsAdapter);
#transformer = new FreshFileTransformer();
#addedInternalTransforms = false;
#options: { target: string | string[] };

Expand Down
13 changes: 9 additions & 4 deletions src/dev/dev_build_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { getSnapshotPath, type ResolvedFreshConfig } from "../config.ts";
import type { BuildSnapshot } from "../build_cache.ts";
import { encodeHex } from "@std/encoding/hex";
import { crypto } from "@std/crypto";
import { fsAdapter } from "../fs.ts";
import type { FreshFileTransformer } from "./file_transformer.ts";
import { assertInDir } from "../utils.ts";
import { ensureDir } from "@std/fs/ensure-dir";
import { walk } from "@std/fs/walk";

export interface MemoryFile {
hash: string | null;
Expand Down Expand Up @@ -193,7 +194,7 @@ export class DiskBuildCache implements DevBuildCache {
const filePath = path.join(outDir, pathname);
assertInDir(filePath, outDir);

await fsAdapter.mkdirp(path.dirname(filePath));
await ensureDir(path.dirname(filePath));
await Deno.writeFile(filePath, content);
}

Expand All @@ -206,8 +207,8 @@ export class DiskBuildCache implements DevBuildCache {
const staticDir = this.config.staticDir;
const outDir = this.config.build.outDir;

if (await fsAdapter.isDirectory(staticDir)) {
const entries = fsAdapter.walk(staticDir, {
try {
const entries = walk(staticDir, {
includeDirs: false,
includeFiles: true,
followSymlinks: false,
Expand Down Expand Up @@ -240,6 +241,10 @@ export class DiskBuildCache implements DevBuildCache {
this.addUnprocessedFile(pathname);
}
}
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
throw error;
}
}

const snapshot: BuildSnapshot = {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/dev_build_cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "@std/expect";
import * as path from "@std/path";
import { MemoryBuildCache } from "./dev_build_cache.ts";
import { FreshFileTransformer } from "./file_transformer.ts";
import { createFakeFs, withTmpDir } from "../test_utils.ts";
import { withTmpDir } from "../test_utils.ts";
import type { ResolvedFreshConfig } from "../mod.ts";

Deno.test({
Expand All @@ -19,7 +19,7 @@ Deno.test({
outDir: path.join(tmp, "dist"),
},
};
const fileTransformer = new FreshFileTransformer(createFakeFs({}));
const fileTransformer = new FreshFileTransformer();
const buildCache = new MemoryBuildCache(
config,
"testing",
Expand Down
8 changes: 1 addition & 7 deletions src/dev/file_transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +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";

Expand Down Expand Up @@ -58,11 +57,6 @@ interface TransformReq {

export class FreshFileTransformer {
#transformers: Transformer[] = [];
#fs: FsAdapter;

constructor(fs: FsAdapter) {
this.#fs = fs;
}

onTransform(options: OnTransformOptions, callback: TransformFn): void {
this.#transformers.push({ options, fn: callback });
Expand All @@ -88,7 +82,7 @@ export class FreshFileTransformer {

let content: Uint8Array;
try {
content = await this.#fs.readFile(filePath);
content = await Deno.readFile(filePath);
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return null;
Expand Down
59 changes: 21 additions & 38 deletions src/dev/file_transformer_test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import { expect } from "@std/expect";
import type { FsAdapter } from "../fs.ts";
import {
FreshFileTransformer,
type ProcessedFile,
} from "./file_transformer.ts";
import { delay } from "../test_utils.ts";
import { stub } from "@std/testing/mock";

function testTransformer(files: Record<string, string>) {
const mockFs: FsAdapter = {
cwd: () => "/",
isDirectory: () => Promise.resolve(false),
mkdirp: () => Promise.resolve(),
walk: async function* foo() {
},
readFile: (file) => {
if (file instanceof URL) throw new Error("Not supported");
// deno-lint-ignore no-explicit-any
const content = (files as any)[file];
const buf = new TextEncoder().encode(content);
return Promise.resolve(buf);
},
};
return new FreshFileTransformer(mockFs);
function stubDenoReadFile(content: string) {
return stub(Deno, "readFile", () => {
return Promise.resolve(
new TextEncoder().encode(content) as Uint8Array<ArrayBuffer>,
);
});
}

function consumeResult(result: ProcessedFile[]) {
Expand Down Expand Up @@ -52,9 +42,8 @@ function consumeResult(result: ProcessedFile[]) {
}

Deno.test("FileTransformer - transform sync", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
const transformer = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("foo");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first time seeing the using keyword, neat!


transformer.onTransform({ pluginName: "foo", filter: /.*/ }, (args) => {
return {
Expand All @@ -70,9 +59,8 @@ Deno.test("FileTransformer - transform sync", async () => {
});

Deno.test("FileTransformer - transform async", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
const transformer = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("foo");

transformer.onTransform({ pluginName: "foo", filter: /.*/ }, async (args) => {
await delay(1);
Expand All @@ -89,9 +77,8 @@ Deno.test("FileTransformer - transform async", async () => {
});

Deno.test("FileTransformer - transform return Uint8Array", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
const transformer = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("foo");

transformer.onTransform({ pluginName: "foo", filter: /.*/ }, () => {
return {
Expand All @@ -107,9 +94,8 @@ Deno.test("FileTransformer - transform return Uint8Array", async () => {
});

Deno.test("FileTransformer - pass transformed content", async () => {
const transformer = testTransformer({
"input.txt": "input",
});
const transformer = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("input");

transformer.onTransform({ pluginName: "A", filter: /.*/ }, (args) => {
return {
Expand Down Expand Up @@ -137,9 +123,8 @@ Deno.test("FileTransformer - pass transformed content", async () => {
Deno.test(
"FileTransformer - pass transformed content with multiple",
async () => {
const transformer = testTransformer({
"input.txt": "input",
});
const transformer = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("input");

transformer.onTransform({ pluginName: "A", filter: /.*/ }, (args) => {
return [{
Expand Down Expand Up @@ -167,9 +152,8 @@ Deno.test(
);

Deno.test("FileTransformer - return multiple results", async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
const transformer = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("foo");

const received: string[] = [];
transformer.onTransform({ pluginName: "A", filter: /foo\.txt$/ }, () => {
Expand Down Expand Up @@ -197,9 +181,8 @@ Deno.test("FileTransformer - return multiple results", async () => {
Deno.test(
"FileTransformer - track input files through temporary results",
async () => {
const transformer = testTransformer({
"foo.txt": "foo",
});
const transformer = new FreshFileTransformer();
using _denoReadFileStub = stubDenoReadFile("foo");

transformer.onTransform({ pluginName: "A", filter: /foo\.txt$/ }, () => {
return [{
Expand Down
36 changes: 0 additions & 36 deletions src/fs.ts

This file was deleted.

Loading
Loading