fix(build): incorrect specifier with file:// islands (#3157)

This commit is contained in:
Marvin Hagemeister
2025-07-31 09:23:15 +02:00
committed by GitHub
parent f87d2180fb
commit b898548e95
7 changed files with 172 additions and 9 deletions
+1 -1
View File
@@ -354,7 +354,7 @@ export function specToName(spec: string): string {
}
const idx = spec.lastIndexOf("/");
return spec.slice(idx + 1);
return pathToExportName(spec.slice(idx + 1));
} else if (spec.startsWith("jsr:")) {
const match = spec.match(
/jsr:@([^/]+)\/([^@/]+)(@[\^~]?\d+\.\d+\.\d+([^/]+)?)?(\/.*)?$/,
+87
View File
@@ -312,6 +312,93 @@ export const app = new App().fsRoutes()`,
sanitizeResources: false,
});
Deno.test({
name: "Builder - file:// islands",
fn: async () => {
const root = path.join(import.meta.dirname!, "..", "..");
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
const tmp = _tmp.dir;
await writeFiles(tmp, {
"other/Foo.tsx": `export default () => <h1>ok</h1>;`,
"routes/index.tsx": `import Foo from "../other/Foo.tsx";
export default () => <Foo />;`,
"main.ts": `import { App } from "fresh";
export const app = new App().fsRoutes()`,
});
const builder = new Builder({
root: tmp,
outDir: path.join(tmp, "dist"),
});
const islandPath = path.join(tmp, "other", "Foo.tsx");
builder.registerIsland(path.toFileUrl(islandPath).href);
await builder.build();
let text = "fail";
await withChildProcessServer(
tmp,
["serve", "-A", "dist/server.js"],
async (address) => {
const res = await fetch(address);
text = await res.text();
},
);
expect(text).toContain("<h1>ok</h1>");
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
// Currently waiting on bug fixes in @deno/loader
// to support resolving without passing entry points
ignore: true,
name: "Builder - mapped islands",
fn: async () => {
const root = path.join(import.meta.dirname!, "..", "..");
await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" });
const tmp = _tmp.dir;
await writeFiles(tmp, {
"other/Foo.tsx": `export default () => <h1>ok</h1>;`,
"routes/index.tsx": `import Foo from "foo-island";
export default () => <Foo />;`,
"main.ts": `import { App } from "fresh";
export const app = new App().fsRoutes()`,
"deno.json": JSON.stringify({
imports: { "foo-island": "other/Foo.tsx" },
}),
});
const builder = new Builder({
root: tmp,
outDir: path.join(tmp, "dist"),
});
builder.registerIsland("foo-island");
await builder.build();
let text = "fail";
await withChildProcessServer(
tmp,
["serve", "-A", "dist/server.js"],
async (address) => {
const res = await fetch(address);
text = await res.text();
},
);
expect(text).toContain("<h1>ok</h1>");
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "Builder - source maps",
fn: async () => {
+3 -3
View File
@@ -369,7 +369,7 @@ import staticFileData from "./static-files.json" with { type: "json" };
${
islands
.map((item) => {
const spec = pathToSpec(path.relative(outDir, item.server));
const spec = pathToSpec(outDir, item.server);
return `import * as ${item.name} from "${spec}";`;
})
.join("\n")
@@ -380,7 +380,7 @@ ${
this.#fsRoutes.files
.map((item, i) => {
if (item.lazy) return null;
const spec = pathToSpec(path.relative(outDir, item.filePath));
const spec = pathToSpec(outDir, item.filePath);
return `import * as fsRoute_${i} from "${spec}"`;
})
.filter(Boolean)
@@ -423,7 +423,7 @@ ${
let mod = "";
if (item.lazy) {
const spec = pathToSpec(path.relative(outDir, item.filePath));
const spec = pathToSpec(outDir, item.filePath);
mod = `() => import(${JSON.stringify(spec)})`;
} else {
mod = `fsRoute_${i}`;
+20
View File
@@ -0,0 +1,20 @@
import * as pathPosix from "@std/path/posix";
import * as path from "@std/path";
export type FileUrl = string & { readonly __brand: unique symbol };
export function pathToFileUrl(str: string): FileUrl {
if (str.startsWith("file://")) {
// all good
} else if (path.isAbsolute(str)) {
str = path.toFileUrl(str).href;
} else {
throw new Error(`Cannot convert "${str}" to file:// url`);
}
return str as FileUrl;
}
export function relativeUrl(from: FileUrl, to: FileUrl): string {
return pathPosix.relative(from, to);
}
+14
View File
@@ -0,0 +1,14 @@
import { expect } from "@std/expect";
import { pathToFileUrl, relativeUrl } from "./file_url.ts";
Deno.test("pathToFileUrl", () => {
expect(pathToFileUrl("file:///foo/bar")).toEqual("file:///foo/bar");
expect(pathToFileUrl("/foo/bar")).toEqual("file:///foo/bar");
});
Deno.test("relativeUrl", () => {
const a = pathToFileUrl("file://foo/bar");
const b = pathToFileUrl("file://foo/baz");
expect(relativeUrl(a, b)).toEqual("../baz");
});
+35 -2
View File
@@ -1,5 +1,6 @@
import * as path from "@std/path";
import type { Lazy, MaybeLazy } from "./types.ts";
import { pathToFileUrl, relativeUrl } from "./file_url.ts";
export function assertInDir(
filePath: string,
@@ -66,8 +67,40 @@ export class UniqueNamer {
}
const PATH_TO_SPEC = /[\\/]+/g;
export function pathToSpec(str: string): string {
return str.replaceAll(PATH_TO_SPEC, "/");
export function pathToSpec(outDir: string, spec: string): string {
const outDirUrl = pathToFileUrl(outDir);
if (
spec.startsWith("http:") || spec.startsWith("https:") ||
spec.startsWith("jsr:")
) {
return spec;
} else if (spec.startsWith("file://")) {
const fileUrl = pathToFileUrl(spec);
spec = relativeUrl(outDirUrl, fileUrl);
return maybeDot(spec);
}
spec = path.normalize(spec);
if (path.isAbsolute(spec)) {
const fileUrl = pathToFileUrl(spec);
spec = relativeUrl(outDirUrl, fileUrl);
return maybeDot(spec);
}
spec = spec.replaceAll(PATH_TO_SPEC, "/");
if (spec.startsWith("/")) {
const fileUrl = pathToFileUrl(spec);
spec = relativeUrl(outDirUrl, fileUrl);
return maybeDot(spec);
}
spec = maybeDot(spec);
return spec;
}
function maybeDot(spec: string): string {
return spec.startsWith(".") ? spec : `./${spec}`;
}
export function isLazy<T>(value: MaybeLazy<T>): value is Lazy<T> {
+12 -3
View File
@@ -54,7 +54,16 @@ Deno.test("escapeScript - json", () => {
});
Deno.test("pathToSpec", () => {
expect(pathToSpec("/foo/bar")).toEqual("/foo/bar");
expect(pathToSpec("\\foo\\bar")).toEqual("/foo/bar");
expect(pathToSpec("\\\\foo//bar")).toEqual("/foo/bar");
expect(pathToSpec("/foo", "/foo/bar")).toEqual("./bar");
expect(pathToSpec("/", "/foo//bar")).toEqual("./foo/bar");
expect(pathToSpec("/foo", "bar")).toEqual("./bar");
expect(pathToSpec("/foo/bar", "/foo/baz")).toEqual("../baz");
expect(pathToSpec("/foo", "file:///foo//bar")).toEqual("./bar");
expect(pathToSpec("/foo", "http://example.com")).toEqual(
"http://example.com",
);
expect(pathToSpec("/foo", "https://example.com")).toEqual(
"https://example.com",
);
expect(pathToSpec("/foo", "jsr:@foo/bar")).toEqual("jsr:@foo/bar");
});