Skip to content

Commit 05707b3

Browse files
authored
handle absolute global imports (#1758)
* fix #1755; handle absolute global imports * test * fix module hashes
1 parent fa2b5b6 commit 05707b3

File tree

10 files changed

+19
-14
lines changed

10 files changed

+19
-14
lines changed

src/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export async function build(
237237
if (!path.endsWith(".js")) continue;
238238
const sourcePath = join(cacheRoot, path);
239239
effects.output.write(`${faint("build")} ${path} ${faint("→")} `);
240-
const resolveImport = (i: string) => relativePath(path, aliases.get((i = resolvePath(path, i))) ?? i);
240+
const resolveImport = (i: string) => isPathImport(i) ? relativePath(path, aliases.get((i = resolvePath(path, i))) ?? i) : i; // prettier-ignore
241241
await effects.writeFile(aliases.get(path)!, rewriteNpmImports(await readFile(sourcePath, "utf-8"), resolveImport));
242242
}
243243

test/input/build/imports/foo/foo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "npm:d3";
2+
import "npm:@example/url-import";
23
import {bar} from "../bar/bar.js";
34
export {top} from "/top.js";
45

test/javascript/module-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const emptyHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b
88
// through the code and verifying that they consider all the relevant files.
99
describe("getModuleHash(root, path)", () => {
1010
it("returns the transitive content hash for the specified module", () => {
11-
assert.strictEqual(getModuleHash("test/input/build/imports", "foo/foo.js"), "32f934a52fa34ba1b06aa6089fe5922dc442c9bf2dcddef864bc649a39d9eace"); // prettier-ignore
12-
assert.strictEqual(getModuleHash("test/input/build/imports", "bar/bar.js"), "7fe009c8bb0049d9b84d53a00b29fb172bbf07d8232d2ace5f7c6f220b23eb16"); // prettier-ignore
11+
assert.strictEqual(getModuleHash("test/input/build/imports", "foo/foo.js"), "e743cc5455594df5a3bd78622594dfb7a8ddb9277957be9b9732f33a88955d82"); // prettier-ignore
12+
assert.strictEqual(getModuleHash("test/input/build/imports", "bar/bar.js"), "34442bce5f38762986a81229c551723cdc3d4c1509ac14dde193555e65013d76"); // prettier-ignore
1313
assert.strictEqual(getModuleHash("test/input/build/imports", "top.js"), "160847a6b4890d59f8e8862911bfbe3b8066955d31f2708cafbe51945c3c57b6"); // prettier-ignore
1414
assert.strictEqual(getModuleHash("test/input/build/fetches", "foo/foo.js"), "3bb4a170d2f3539934168741572d4aa3cd11da649d4ca88b408edefb5c287360"); // prettier-ignore
1515
assert.strictEqual(getModuleHash("test/input/build/fetches", "top.js"), "6c858de52de6ff26b19508e95448288da02fac62251b7ca2710a308a0ebfd7ba"); // prettier-ignore
@@ -27,8 +27,8 @@ describe("getModuleInfo(root, path)", () => {
2727
assert.deepStrictEqual(redactModuleInfo("test/input/build/imports", "foo/foo.js"), {
2828
fileMethods: new Set(),
2929
files: new Set(),
30-
hash: "c77c2490ea7b9a89dce7bad39973995e5158921bf8576955ae4a596c47a5a2a4",
31-
globalStaticImports: new Set(["npm:d3"]),
30+
hash: "17e03fbc08c28530c84ab1163901890915302d3f1d5af2c9256e3e8cab1324a9",
31+
globalStaticImports: new Set(["npm:@example/url-import", "npm:d3"]),
3232
globalDynamicImports: new Set(),
3333
localDynamicImports: new Set(),
3434
localStaticImports: new Set(["../bar/bar.js", "../top.js"])

test/mocks/jsdelivr.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {getCurrentAgent, mockAgent} from "./undici.js";
22

3-
const packages: [name: string, {version: string; dependencies?: Record<string, string>}][] = [
3+
const packages: [name: string, {version: string; contents?: string; dependencies?: Record<string, string>}][] = [
44
["@duckdb/duckdb-wasm", {version: "1.28.0"}],
5+
["@example/url-import", {version: "1.0.0", contents: "import('https://example.com');"}],
56
["@observablehq/inputs", {version: "0.10.6"}],
67
["@observablehq/plot", {version: "0.6.11"}],
78
["@observablehq/sample-datasets", {version: "1.0.1"}],
@@ -50,7 +51,7 @@ export function mockJsDelivr() {
5051
.persist(); // prettier-ignore
5152
cdnClient
5253
.intercept({path: new RegExp(`^/npm/${name}@${pkg.version}/`), method: "GET"})
53-
.reply(200, "", {headers: {"cache-control": "public, immutable", "content-type": "text/javascript; charset=utf-8"}})
54+
.reply(200, pkg.contents ?? "", {headers: {"cache-control": "public, immutable", "content-type": "text/javascript; charset=utf-8"}})
5455
.persist(); // prettier-ignore
5556
}
5657
});

test/output/build/imports/_import/bar/bar.13bb8056.js

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {bar} from "./baz.2add1dd0.js";
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {foo} from "../foo/foo.5963bf78.js";
1+
import {foo} from "../foo/foo.bcd720b2.js";
22

33
export const bar = "bar";
44
export const foobar = foo + "bar";

test/output/build/imports/_import/foo/foo.5963bf78.js renamed to test/output/build/imports/_import/foo/foo.bcd720b2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "../../_npm/[email protected]/cd372fb8.js";
2-
import {bar} from "../bar/bar.13bb8056.js";
2+
import "../../_npm/@example/[email protected]/1dd108c5.js";
3+
import {bar} from "../bar/bar.4460ccc2.js";
34
export {top} from "../top.160847a6.js";
45

56
export const foo = "foo";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import('https://example.com');

test/output/build/imports/foo/foo.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
<link rel="modulepreload" href="../_observablehq/runtime.00000002.js">
1515
<link rel="modulepreload" href="../_observablehq/stdlib.00000003.js">
1616
<link rel="modulepreload" href="../_npm/[email protected]/cd372fb8.js">
17-
<link rel="modulepreload" href="../_import/bar/bar.13bb8056.js">
17+
<link rel="modulepreload" href="../_import/bar/bar.4460ccc2.js">
1818
<link rel="modulepreload" href="../_import/top.160847a6.js">
1919
<link rel="modulepreload" href="../_import/foo/foo bar.b173d3de.js">
20-
<link rel="modulepreload" href="../_import/bar/baz.cdbfb28b.js">
21-
<link rel="modulepreload" href="../_import/foo/foo.5963bf78.js">
20+
<link rel="modulepreload" href="../_import/bar/baz.2add1dd0.js">
21+
<link rel="modulepreload" href="../_import/foo/foo.bcd720b2.js">
22+
<link rel="modulepreload" href="../_npm/@example/[email protected]/1dd108c5.js">
2223
<script type="module">
2324

2425
import {define} from "../_observablehq/client.00000001.js";
@@ -28,7 +29,7 @@
2829
registerFile("./hello.txt", {"name":"./hello.txt","mimeType":"text/plain","path":"../_file/foo/hello.5891b5b5.txt","lastModified":/* ts */1706742000000,"size":6});
2930

3031
define({id: "261e010e", inputs: ["display","FileAttachment"], outputs: ["d3","bar","top"], body: async (display,FileAttachment) => {
31-
const [d3, {bar}, {top}] = await Promise.all([import("../_npm/[email protected]/cd372fb8.js"), import("../_import/bar/bar.13bb8056.js"), import("../_import/top.160847a6.js")]);
32+
const [d3, {bar}, {top}] = await Promise.all([import("../_npm/[email protected]/cd372fb8.js"), import("../_import/bar/bar.4460ccc2.js"), import("../_import/top.160847a6.js")]);
3233

3334
display(bar);
3435
display(top);

0 commit comments

Comments
 (0)