Skip to content

Commit 4ca8e9e

Browse files
authored
fix hashes for modules importing builtins (#1688)
1 parent c3012a3 commit 4ca8e9e

24 files changed

+70
-15
lines changed

src/javascript/module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {extname, join} from "node:path/posix";
66
import type {Program} from "acorn";
77
import type {TransformOptions} from "esbuild";
88
import {transform, transformSync} from "esbuild";
9+
import {resolveJsrImport} from "../jsr.js";
910
import {resolveNodeImport} from "../node.js";
1011
import {resolveNpmImport} from "../npm.js";
1112
import {resolvePath} from "../path.js";
12-
import {builtins} from "../resolvers.js";
13+
import {builtins, resolveBuiltin} from "../resolvers.js";
1314
import type {RouteResult} from "../route.js";
1415
import {route} from "../route.js";
1516
import {findFiles} from "./files.js";
@@ -94,8 +95,12 @@ export async function getLocalModuleHash(root: string, path: string, getHash?: (
9495
if (info) {
9596
const globalPaths = new Set<string>();
9697
for (const i of [...info.globalStaticImports, ...info.globalDynamicImports]) {
97-
if (i.startsWith("npm:") && !builtins.has(i)) {
98+
if (builtins.has(i) || i.startsWith("observablehq:")) {
99+
hash.update(`${resolveBuiltin(i)}?version=${process.env.npm_package_version}`); // change hash when Framework changes
100+
} else if (i.startsWith("npm:")) {
98101
globalPaths.add(await resolveNpmImport(root, i.slice("npm:".length)));
102+
} else if (i.startsWith("jsr:")) {
103+
globalPaths.add(await resolveJsrImport(root, i.slice("jsr:".length)));
99104
} else if (!/^\w+:/.test(i)) {
100105
globalPaths.add(await resolveNodeImport(root, i));
101106
}

src/resolvers.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,8 @@ export function getModuleResolver(
477477
return async (specifier) => {
478478
return isPathImport(specifier)
479479
? relativePath(servePath, resolveImportPath(root, resolvePath(path, specifier), getHash))
480-
: builtins.has(specifier)
481-
? relativePath(servePath, builtins.get(specifier)!)
482-
: specifier.startsWith("observablehq:")
483-
? relativePath(servePath, `/_observablehq/${specifier.slice("observablehq:".length)}${extname(specifier) ? "" : ".js"}`) // prettier-ignore
480+
: builtins.has(specifier) || specifier.startsWith("observablehq:")
481+
? relativePath(servePath, resolveBuiltin(specifier))
484482
: specifier.startsWith("npm:")
485483
? relativePath(servePath, await resolveNpmImport(root, specifier.slice("npm:".length)))
486484
: specifier.startsWith("jsr:")
@@ -491,6 +489,12 @@ export function getModuleResolver(
491489
};
492490
}
493491

492+
export function resolveBuiltin(specifier: string): string {
493+
if (builtins.has(specifier)) return builtins.get(specifier)!;
494+
if (!specifier.startsWith("observablehq:")) throw new Error(`not built-in: ${specifier}`);
495+
return `/_observablehq/${specifier.slice("observablehq:".length)}${extname(specifier) ? "" : ".js"}`;
496+
}
497+
494498
export function resolveStylesheetPath(root: string, path: string): string {
495499
return `/${join("_import", path)}?sha=${getFileHash(root, path)}`;
496500
}

test/input/build/npm/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "npm:@observablehq/inputs";

test/input/build/npm/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```js
2+
import "./index.js";
3+
```

test/mocks/jsdelivr.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const packages: [name: string, {version: string; dependencies?: Record<string, s
1515
["echarts", {version: "5.5.0"}],
1616
["exceljs", {version: "4.4.0"}],
1717
["htl", {version: "0.3.1"}],
18+
["isoformat", {version: "0.2.1"}],
1819
["jszip", {version: "3.10.1"}],
1920
["katex", {version: "0.16.9"}],
2021
["leaflet", {version: "1.9.4"}],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export {test} from "./test.17d808d0.js";
1+
export {test} from "./test.86a60bc6.js";

test/output/build/data-loaders/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
1313
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
1414
<link rel="modulepreload" href="./_import/import-test.e7269c4e.js">
15-
<link rel="modulepreload" href="./_import/test.17d808d0.js">
15+
<link rel="modulepreload" href="./_import/test.86a60bc6.js">
1616
<script type="module">
1717

1818
import {define} from "./_observablehq/client.00000001.js";

test/output/build/embed/chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import "./_observablehq/stdlib.00000003.js";
22
import "./_npm/@observablehq/[email protected]/cd372fb8.js";
33
import "./_npm/[email protected]/cd372fb8.js";
4-
export * from "./_import/chart.76ef5352.js";
4+
export * from "./_import/chart.2ce91e05.js";

0 commit comments

Comments
 (0)