diff --git a/src/build/deps.ts b/src/build/deps.ts index 2e2c9c22..81cdb341 100644 --- a/src/build/deps.ts +++ b/src/build/deps.ts @@ -1,6 +1,8 @@ // -- $std -- export { fromFileUrl, + join, + relative, toFileUrl, } from "https://deno.land/std@0.193.0/path/mod.ts"; export { escape as regexpEscape } from "https://deno.land/std@0.193.0/regexp/escape.ts"; diff --git a/src/build/esbuild.ts b/src/build/esbuild.ts index 33d7843c..d82c6c4a 100644 --- a/src/build/esbuild.ts +++ b/src/build/esbuild.ts @@ -5,7 +5,7 @@ import { esbuildWasmURL, fromFileUrl, regexpEscape, - toFileUrl, + relative, } from "./deps.ts"; import { Builder, BuildSnapshot } from "./mod.ts"; @@ -21,6 +21,7 @@ export interface EsbuildBuilderOptions { /** The JSX configuration. */ jsxConfig: JSXConfig; target: string | string[]; + absoluteWorkingDir: string; } export interface JSXConfig { @@ -40,7 +41,7 @@ export class EsbuildBuilder implements Builder { try { await initEsbuild(); - const absWorkingDir = Deno.cwd(); + const absWorkingDir = opts.absoluteWorkingDir; // In dev-mode we skip identifier minification to be able to show proper // component names in Preact DevTools instead of single characters. @@ -82,10 +83,8 @@ export class EsbuildBuilder implements Builder { const files = new Map(); const dependencies = new Map(); - const absWorkingDirLen = toFileUrl(absWorkingDir).href.length + 1; - for (const file of bundle.outputFiles) { - const path = toFileUrl(file.path).href.slice(absWorkingDirLen); + const path = relative(absWorkingDir, file.path); files.set(path, file.contents); } diff --git a/src/build/esbuild_test.ts b/src/build/esbuild_test.ts new file mode 100644 index 00000000..e4c8d550 --- /dev/null +++ b/src/build/esbuild_test.ts @@ -0,0 +1,62 @@ +import { assertEquals } from "$std/testing/asserts.ts"; +import { fromFileUrl, join, toFileUrl } from "../server/deps.ts"; +import { EsbuildBuilder } from "./esbuild.ts"; + +const denoJson = join( + fromFileUrl(import.meta.url), + "..", + "..", + "..", + "deno.json", +); + +const mainEntry = toFileUrl(join( + fromFileUrl(import.meta.url), + "..", + "..", + "runtime", + "entrypoints", + "client.ts", +)).href; + +Deno.test("esbuild snapshot with cwd=Deno.cwd()", async () => { + const builder = new EsbuildBuilder({ + absoluteWorkingDir: Deno.cwd(), + buildID: "foo", + configPath: denoJson, + dev: false, + entrypoints: { + main: mainEntry, + }, + jsxConfig: { + jsx: "react-jsx", + }, + target: "es2020", + }); + + const snapshot = await builder.build(); + assertEquals(snapshot.paths, ["main.js", "metafile.json"]); +}); + +Deno.test({ + name: "esbuild snapshot with cwd=/", + ignore: Deno.build.os === "windows", + fn: async () => { + const builder = new EsbuildBuilder({ + absoluteWorkingDir: "/", + buildID: "foo", + configPath: denoJson, + dev: false, + entrypoints: { + main: mainEntry, + }, + jsxConfig: { + jsx: "react-jsx", + }, + target: "es2020", + }); + + const snapshot = await builder.build(); + assertEquals(snapshot.paths, ["main.js", "metafile.json"]); + }, +}); diff --git a/src/server/context.ts b/src/server/context.ts index 36288b28..888bfaae 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -450,6 +450,7 @@ export class ServerContext { dev: this.#dev, jsxConfig, target, + absoluteWorkingDir: Deno.cwd(), }); this.#routerOptions = routerOptions; }