Repository: denoland/fresh
File: src/dev/dev_build_cache.ts (TypeScript)

Code before:
export class DiskBuildCache implements DevBuildCache {
  hasSnapshot = true;
  islands = new Map<string, string>();
  #processedFiles = new Map<string, string | null>();
  #unprocessedFiles = new Map<string, string>();
  #transformer: FreshFileTransformer;
  #target: string | string[];

  constructor(
    public config: ResolvedFreshConfig,
    public buildId: string,
    transformer: FreshFileTransformer,
    target: string | string[],
  ) {
    this.#transformer = transformer;
    this.#target = target;
  }

  getIslandChunkName(islandName: string): string | null {
    return this.islands.get(islandName) ?? null;
  }

  addUnprocessedFile(pathname: string): void {
    this.#unprocessedFiles.set(
      pathname,
      path.join(this.config.staticDir, pathname),
    );
  }

  async addProcessedFile(
    pathname: string,
    content: Uint8Array,
    hash: string | null,
  ) {
    this.#processedFiles.set(pathname, hash);

    const outDir = pathname === "/metafile.json"
      ? this.config.build.outDir
      : path.join(this.config.build.outDir, "static");
    const filePath = path.join(outDir, pathname);
    assertInDir(filePath, outDir);

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

  // deno-lint-ignore require-await
  async readFile(_pathname: string): Promise<StaticFile | null> {
    throw new Error("Not implemented in build mode");
  }


Review comment (suggestion):
I don't know if there is a "better" way to normalize file system paths to URL-like paths. This seems to work at least, since manually replacing `\\` with `/` in my `snapshot.json` locally fixes the issue.

Code after:
// await fsAdapter.mkdirp(staticOutDir);
export class DiskBuildCache implements DevBuildCache {
  hasSnapshot = true;
  islands = new Map<string, string>();
  #processedFiles = new Map<string, string | null>();
  #unprocessedFiles = new Map<string, string>();
  #transformer: FreshFileTransformer;
  #target: string | string[];

  constructor(
    public config: ResolvedFreshConfig,
    public buildId: string,
    transformer: FreshFileTransformer,
    target: string | string[],
  ) {
    this.#transformer = transformer;
    this.#target = target;
  }

  getIslandChunkName(islandName: string): string | null {
    return this.islands.get(islandName) ?? null;
  }

  addUnprocessedFile(pathname: string): void {
    this.#unprocessedFiles.set(
      pathname.replaceAll(WINDOWS_SEPARATOR, "/"),
      path.join(this.config.staticDir, pathname),
    );
  }

  async addProcessedFile(
    pathname: string,
    content: Uint8Array,
    hash: string | null,
  ) {
    this.#processedFiles.set(pathname, hash);

    const outDir = pathname === "/metafile.json"
      ? this.config.build.outDir
      : path.join(this.config.build.outDir, "static");
    const filePath = path.join(outDir, pathname);
    assertInDir(filePath, outDir);

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

  // deno-lint-ignore require-await
  async readFile(_pathname: string): Promise<StaticFile | null> {
    throw new Error("Not implemented in build mode");
  }