From b51f83b42d35054453fab6e4d7ab655bb52891f7 Mon Sep 17 00:00:00 2001 From: ayame113 <40050810+ayame113@users.noreply.github.com> Date: Sun, 4 Jun 2023 05:07:01 +0900 Subject: [PATCH] fix: actually preload all necessary files (#1242) --- src/build/esbuild.ts | 21 +++----------- src/server/render.ts | 12 ++++---- tests/main_test.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 23 deletions(-) diff --git a/src/build/esbuild.ts b/src/build/esbuild.ts index cb18c9c5..91aafee2 100644 --- a/src/build/esbuild.ts +++ b/src/build/esbuild.ts @@ -90,24 +90,11 @@ export class EsbuildBuilder implements Builder { const metaOutputs = new Map(Object.entries(bundle.metafile.outputs)); - // deno-lint-ignore no-inner-declarations - function walk(path: string, seen: Set) { - if (seen.has(path)) return; - seen.add(path); - const entry = metaOutputs.get(path); - if (entry === undefined) return; - for (const dep of entry.imports) { - if (dep.kind === "import-statement") { - walk(dep.path, seen); - } - } - } - for (const [path, entry] of metaOutputs.entries()) { - if (entry.entryPoint === undefined) continue; - const seen = new Set(); - walk(path, seen); - dependencies.set(path, [...seen]); + const imports = entry.imports + .filter(({ kind }) => kind === "import-statement") + .map(({ path }) => path); + dependencies.set(path, imports); } return new EsbuildSnapshot(files, dependencies); diff --git a/src/server/render.ts b/src/server/render.ts index 9f5910b1..071c4c6a 100644 --- a/src/server/render.ts +++ b/src/server/render.ts @@ -218,7 +218,7 @@ export async function render( nonce(randomNonce), ]; } - const url = bundleAssetUrl(path); + const url = bundleAssetUrl(`/${path}`); imports.push([url, randomNonce]); preloadSet.add(url); for (const depPath of opts.dependenciesFn(path)) { @@ -264,11 +264,11 @@ export async function render( ``; if (res.requiresDeserializer) { - const url = addImport("/deserializer.js"); + const url = addImport("deserializer.js"); script += `import { deserialize } from "${url}";`; } if (res.hasSignals) { - const url = addImport("/signals.js"); + const url = addImport("signals.js"); script += `import { signal } from "${url}";`; } script += `const ST = document.getElementById("__FRSH_STATE").textContent;`; @@ -287,7 +287,7 @@ export async function render( // Then it imports all plugin scripts and executes them (with their respective // state). for (const [pluginName, entrypoint, i] of pluginScripts) { - const url = addImport(`/plugin-${pluginName}-${entrypoint}.js`); + const url = addImport(`plugin-${pluginName}-${entrypoint}.js`); script += `import p${i} from "${url}";p${i}(STATE[1][${i}]);`; } @@ -295,13 +295,13 @@ export async function render( // reviver from the "main" script. if (ENCOUNTERED_ISLANDS.size > 0) { // Load the main.js script - const url = addImport("/main.js"); + const url = addImport("main.js"); script += `import { revive } from "${url}";`; // Prepare the inline script that loads and revives the islands let islandRegistry = ""; for (const island of ENCOUNTERED_ISLANDS) { - const url = addImport(`/island-${island.id}.js`); + const url = addImport(`island-${island.id}.js`); script += `import ${island.name} from "${url}";`; islandRegistry += `${island.id}:${island.name},`; } diff --git a/tests/main_test.ts b/tests/main_test.ts index 5cc83f00..35094aba 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -724,3 +724,70 @@ Deno.test("jsx pragma works", { await lines.cancel(); serverProcess.kill("SIGTERM"); }); + +Deno.test("preloading javascript files", { + sanitizeOps: false, + sanitizeResources: false, +}, async () => { + // Preparation + const serverProcess = new Deno.Command(Deno.execPath(), { + args: ["run", "-A", "./tests/fixture/main.ts"], + stdout: "piped", + stderr: "inherit", + }).spawn(); + + const decoder = new TextDecoderStream(); + const lines = serverProcess.stdout + .pipeThrough(decoder) + .pipeThrough(new TextLineStream()); + + let started = false; + for await (const line of lines) { + if (line.includes("Listening on http://")) { + started = true; + break; + } + } + if (!started) { + throw new Error("Server didn't start up"); + } + + const browser = await puppeteer.launch({ args: ["--no-sandbox"] }); + const page = await browser.newPage(); + + try { + // request js file to start esbuild execution + await page.goto("http://localhost:8000", { + waitUntil: "networkidle2", + }); + + await delay(5000); // wait running esbuild + + await page.goto("http://localhost:8000", { + waitUntil: "networkidle2", + }); + + const preloads: string[] = await page.$$eval( + 'link[rel="modulepreload"]', + (elements) => elements.map((element) => element.getAttribute("href")), + ); + + assert( + preloads.some((url) => url.match(/\/_frsh\/js\/.*\/main\.js/)), + "preloads does not include main.js", + ); + assert( + preloads.some((url) => url.match(/\/_frsh\/js\/.*\/island-.*\.js/)), + "preloads does not include island-*.js", + ); + assert( + preloads.some((url) => url.match(/\/_frsh\/js\/.*\/chunk-.*\.js/)), + "preloads does not include chunk-*.js", + ); + } finally { + await browser.close(); + + await lines.cancel(); + serverProcess.kill("SIGTERM"); + } +});