diff --git a/src/dev/builder_test.ts b/src/dev/builder_test.ts index 882c1e97..caebca1e 100644 --- a/src/dev/builder_test.ts +++ b/src/dev/builder_test.ts @@ -155,6 +155,40 @@ Deno.test({ sanitizeResources: false, }); +Deno.test({ + name: "Builder - can bundle islands with import.meta.resolve()", + fn: async () => { + await using _tmp = await withTmpDir(); + const tmp = _tmp.dir; + + const outDir = path.join(tmp, "dist"); + const builder = new Builder({ outDir }); + + const specifier = import.meta.resolve( + "../../tests/fixtures_islands/Counter.tsx", + ); + builder.registerIsland(specifier); + + await builder.build({ mode: "production", snapshot: "disk" }); + + const name = specToName(specifier); + const code = await Deno.readTextFile( + path.join( + tmp, + "dist", + "static", + "_fresh", + "js", + BUILD_ID, + `${name}.js`, + ), + ); + expect(code).toContain('"decrement"'); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + Deno.test({ name: "Builder - exclude files", fn: async () => { diff --git a/src/dev/dev_build_cache.ts b/src/dev/dev_build_cache.ts index 17581b66..e11235cd 100644 --- a/src/dev/dev_build_cache.ts +++ b/src/dev/dev_build_cache.ts @@ -185,8 +185,8 @@ export class MemoryBuildCache implements DevBuildCache { await Promise.all( Array.from(this.islandModNameToChunk.entries()).map( async ([name, chunk]) => { - const fileUrl = path.toFileUrl(chunk.server); - const mod = await import(fileUrl.href); + const fileUrl = maybeToFileUrl(chunk.server); + const mod = await import(fileUrl); if (chunk.browser === null) { throw new Error(`Unexpected missing browser chunk`); @@ -201,12 +201,10 @@ export class MemoryBuildCache implements DevBuildCache { async prepare(): Promise { // Load FS routes const files = await Promise.all(this.#fsRoutes.files.map(async (file) => { - const fileUrl = path.toFileUrl(file.filePath); + const fileUrl = maybeToFileUrl(file.filePath); return { ...file, - mod: file.lazy - ? () => import(fileUrl.href) - : await import(fileUrl.href), + mod: file.lazy ? () => import(fileUrl) : await import(fileUrl), }; })); this.#commands = fsItemsToCommands(files); @@ -474,3 +472,7 @@ export function getContentType(filePath: string): string { const ext = path.extname(filePath); return getStdContentType(ext) ?? "text/plain"; } + +function maybeToFileUrl(file: string) { + return file.startsWith("file://") ? file : path.toFileUrl(file).href; +}