import { expect } from "@std/expect"; // Guards against runtime code importing the esbuild dev builder (`src/dev/`) // or test helpers. That happened once through `app.ts` -> `test_utils.ts` -> // `dev/dev_build_cache.ts`: every app then loaded esbuild at startup, and // Vite projects printed "dynamic import cannot be analyzed" warnings. const SRC_URL = new URL("../src/", import.meta.url).href; /** Entry points that end up in a user's app at runtime (see deno.json). */ const RUNTIME_ENTRIES = [ "mod.ts", "runtime/shared.ts", "runtime/client/mod.ts", "internals.ts", "compat.ts", ]; /** Under `src/dev/` but lightweight and needed by the server renderer. */ const ALLOWED_DEV_MODULES = ["dev/middlewares/error_overlay/code_frame.ts"]; /** Paths under `src/` of every local module in the graph, plus all others. */ async function moduleGraph( entry: string, ): Promise<{ local: string[]; other: string[] }> { const out = await new Deno.Command(Deno.execPath(), { // Must be a file URL: on Windows a bare `C:\...` path parses as a URL // with scheme `c:` and the graph comes back empty. args: ["info", "--json", new URL(entry, SRC_URL).href], stdout: "piped", stderr: "piped", }).output(); if (!out.success) { throw new Error(new TextDecoder().decode(out.stderr)); } const info = JSON.parse(new TextDecoder().decode(out.stdout)) as { modules: { specifier: string }[]; }; // Drive letter casing can differ between the two, so compare lowercased. const prefix = SRC_URL.toLowerCase(); const local: string[] = []; const other: string[] = []; for (const { specifier } of info.modules) { if (specifier.toLowerCase().startsWith(prefix)) { local.push(specifier.slice(prefix.length)); } else { other.push(specifier); } } return { local, other }; } for (const entry of RUNTIME_ENTRIES) { Deno.test(`runtime imports - ${entry} doesn't load dev builder or test helpers`, async () => { const { local, other } = await moduleGraph(entry); // Sanity check, so a failed lookup can't pass with an empty graph. expect(local).toContain(entry); const forbidden = [ ...local.filter((rel) => rel === "test_utils.ts" || (rel.startsWith("dev/") && !ALLOWED_DEV_MODULES.includes(rel)) ), ...other.filter((spec) => /esbuild/.test(spec)), ]; expect(forbidden).toEqual([]); }); }