From 221827038d665ee130ae594f245680566719607c Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Thu, 26 Sep 2024 10:27:28 -0700 Subject: [PATCH] fix hashes for modules importing builtins --- src/javascript/module.ts | 9 ++++- src/resolvers.ts | 12 ++++-- test/input/build/npm/index.js | 1 + test/input/build/npm/index.md | 3 ++ test/mocks/jsdelivr.ts | 1 + .../_import/import-test.e7269c4e.js | 2 +- .../{test.17d808d0.js => test.86a60bc6.js} | 0 test/output/build/data-loaders/index.html | 2 +- .../{chart.76ef5352.js => chart.2ce91e05.js} | 0 test/output/build/embed/chart.js | 2 +- .../foo/{foo.3bb4a170.js => foo.666599bc.js} | 0 .../{top.6c858de5.js => top.c85e149a.js} | 2 +- test/output/build/fetches/foo.html | 4 +- test/output/build/fetches/top.html | 6 +-- .../build/npm/_import/index.4bdc071f.js | 1 + .../build/npm/_npm/htl@0.3.1/cd372fb8.js | 0 .../npm/_npm/isoformat@0.2.1/cd372fb8.js | 0 .../npm/_observablehq/client.00000001.js | 0 .../npm/_observablehq/runtime.00000002.js | 0 .../npm/_observablehq/stdlib.00000003.js | 0 .../_observablehq/stdlib/inputs.00000005.js | 0 .../_observablehq/stdlib/inputs.00000006.css | 0 .../theme-air,near-midnight.00000004.css | 0 test/output/build/npm/index.html | 40 +++++++++++++++++++ 24 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 test/input/build/npm/index.js create mode 100644 test/input/build/npm/index.md rename test/output/build/data-loaders/_import/{test.17d808d0.js => test.86a60bc6.js} (100%) rename test/output/build/embed/_import/{chart.76ef5352.js => chart.2ce91e05.js} (100%) rename test/output/build/fetches/_import/foo/{foo.3bb4a170.js => foo.666599bc.js} (100%) rename test/output/build/fetches/_import/{top.6c858de5.js => top.c85e149a.js} (88%) create mode 100644 test/output/build/npm/_import/index.4bdc071f.js create mode 100644 test/output/build/npm/_npm/htl@0.3.1/cd372fb8.js create mode 100644 test/output/build/npm/_npm/isoformat@0.2.1/cd372fb8.js create mode 100644 test/output/build/npm/_observablehq/client.00000001.js create mode 100644 test/output/build/npm/_observablehq/runtime.00000002.js create mode 100644 test/output/build/npm/_observablehq/stdlib.00000003.js create mode 100644 test/output/build/npm/_observablehq/stdlib/inputs.00000005.js create mode 100644 test/output/build/npm/_observablehq/stdlib/inputs.00000006.css create mode 100644 test/output/build/npm/_observablehq/theme-air,near-midnight.00000004.css create mode 100644 test/output/build/npm/index.html diff --git a/src/javascript/module.ts b/src/javascript/module.ts index 7ba45f766..46daa8f47 100644 --- a/src/javascript/module.ts +++ b/src/javascript/module.ts @@ -6,10 +6,11 @@ import {extname, join} from "node:path/posix"; import type {Program} from "acorn"; import type {TransformOptions} from "esbuild"; import {transform, transformSync} from "esbuild"; +import {resolveJsrImport} from "../jsr.js"; import {resolveNodeImport} from "../node.js"; import {resolveNpmImport} from "../npm.js"; import {resolvePath} from "../path.js"; -import {builtins} from "../resolvers.js"; +import {builtins, resolveBuiltin} from "../resolvers.js"; import type {RouteResult} from "../route.js"; import {route} from "../route.js"; import {findFiles} from "./files.js"; @@ -94,8 +95,12 @@ export async function getLocalModuleHash(root: string, path: string, getHash?: ( if (info) { const globalPaths = new Set(); for (const i of [...info.globalStaticImports, ...info.globalDynamicImports]) { - if (i.startsWith("npm:") && !builtins.has(i)) { + if (builtins.has(i) || i.startsWith("observablehq:")) { + hash.update(`${resolveBuiltin(i)}?version=${process.env.npm_package_version}`); // change hash when Framework changes + } else if (i.startsWith("npm:")) { globalPaths.add(await resolveNpmImport(root, i.slice("npm:".length))); + } else if (i.startsWith("jsr:")) { + globalPaths.add(await resolveJsrImport(root, i.slice("jsr:".length))); } else if (!/^\w+:/.test(i)) { globalPaths.add(await resolveNodeImport(root, i)); } diff --git a/src/resolvers.ts b/src/resolvers.ts index a531f411e..8343c4552 100644 --- a/src/resolvers.ts +++ b/src/resolvers.ts @@ -477,10 +477,8 @@ export function getModuleResolver( return async (specifier) => { return isPathImport(specifier) ? relativePath(servePath, resolveImportPath(root, resolvePath(path, specifier), getHash)) - : builtins.has(specifier) - ? relativePath(servePath, builtins.get(specifier)!) - : specifier.startsWith("observablehq:") - ? relativePath(servePath, `/_observablehq/${specifier.slice("observablehq:".length)}${extname(specifier) ? "" : ".js"}`) // prettier-ignore + : builtins.has(specifier) || specifier.startsWith("observablehq:") + ? relativePath(servePath, resolveBuiltin(specifier)) : specifier.startsWith("npm:") ? relativePath(servePath, await resolveNpmImport(root, specifier.slice("npm:".length))) : specifier.startsWith("jsr:") @@ -491,6 +489,12 @@ export function getModuleResolver( }; } +export function resolveBuiltin(specifier: string): string { + if (builtins.has(specifier)) return builtins.get(specifier)!; + if (!specifier.startsWith("observablehq:")) throw new Error(`not built-in: ${specifier}`); + return `/_observablehq/${specifier.slice("observablehq:".length)}${extname(specifier) ? "" : ".js"}`; +} + export function resolveStylesheetPath(root: string, path: string): string { return `/${join("_import", path)}?sha=${getFileHash(root, path)}`; } diff --git a/test/input/build/npm/index.js b/test/input/build/npm/index.js new file mode 100644 index 000000000..35785ba46 --- /dev/null +++ b/test/input/build/npm/index.js @@ -0,0 +1 @@ +import "npm:@observablehq/inputs"; diff --git a/test/input/build/npm/index.md b/test/input/build/npm/index.md new file mode 100644 index 000000000..5171a891a --- /dev/null +++ b/test/input/build/npm/index.md @@ -0,0 +1,3 @@ +```js +import "./index.js"; +``` diff --git a/test/mocks/jsdelivr.ts b/test/mocks/jsdelivr.ts index bc5c36b75..38b7c81c3 100644 --- a/test/mocks/jsdelivr.ts +++ b/test/mocks/jsdelivr.ts @@ -15,6 +15,7 @@ const packages: [name: string, {version: string; dependencies?: Record - + +
+ +
+
+
+ +