diff --git a/src/dev/builder.ts b/src/dev/builder.ts
index f826ff8e..ec351798 100644
--- a/src/dev/builder.ts
+++ b/src/dev/builder.ts
@@ -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+([^/]+)?)?(\/.*)?$/,
diff --git a/src/dev/builder_test.ts b/src/dev/builder_test.ts
index caebca1e..1f586fc2 100644
--- a/src/dev/builder_test.ts
+++ b/src/dev/builder_test.ts
@@ -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 () =>
ok
;`,
+ "routes/index.tsx": `import Foo from "../other/Foo.tsx";
+ export default () => ;`,
+ "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("ok
");
+ },
+ 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 () => ok
;`,
+ "routes/index.tsx": `import Foo from "foo-island";
+ export default () => ;`,
+ "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("ok
");
+ },
+ sanitizeOps: false,
+ sanitizeResources: false,
+});
+
Deno.test({
name: "Builder - source maps",
fn: async () => {
diff --git a/src/dev/dev_build_cache.ts b/src/dev/dev_build_cache.ts
index e11235cd..9c45b36e 100644
--- a/src/dev/dev_build_cache.ts
+++ b/src/dev/dev_build_cache.ts
@@ -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}`;
diff --git a/src/file_url.ts b/src/file_url.ts
new file mode 100644
index 00000000..2bb46aa2
--- /dev/null
+++ b/src/file_url.ts
@@ -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);
+}
diff --git a/src/file_url_test.ts b/src/file_url_test.ts
new file mode 100644
index 00000000..5b5bf005
--- /dev/null
+++ b/src/file_url_test.ts
@@ -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");
+});
diff --git a/src/utils.ts b/src/utils.ts
index 0c98e60a..0f0a4090 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -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(value: MaybeLazy): value is Lazy {
diff --git a/src/utils_test.ts b/src/utils_test.ts
index cbbcd594..cdb45f3a 100644
--- a/src/utils_test.ts
+++ b/src/utils_test.ts
@@ -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");
});