Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle absolute global imports #1758

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export async function build(
if (!path.endsWith(".js")) continue;
const sourcePath = join(cacheRoot, path);
effects.output.write(`${faint("build")} ${path} ${faint("→")} `);
const resolveImport = (i: string) => relativePath(path, aliases.get((i = resolvePath(path, i))) ?? i);
const resolveImport = (i: string) => isPathImport(i) ? relativePath(path, aliases.get((i = resolvePath(path, i))) ?? i) : i; // prettier-ignore
await effects.writeFile(aliases.get(path)!, rewriteNpmImports(await readFile(sourcePath, "utf-8"), resolveImport));
}

Expand Down
1 change: 1 addition & 0 deletions test/input/build/imports/foo/foo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "npm:d3";
import "npm:@example/url-import";
import {bar} from "../bar/bar.js";
export {top} from "/top.js";

Expand Down
5 changes: 3 additions & 2 deletions test/mocks/jsdelivr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {getCurrentAgent, mockAgent} from "./undici.js";

const packages: [name: string, {version: string; dependencies?: Record<string, string>}][] = [
const packages: [name: string, {version: string; contents?: string; dependencies?: Record<string, string>}][] = [
["@duckdb/duckdb-wasm", {version: "1.28.0"}],
["@example/url-import", {version: "1.0.0", contents: "import('https://example.com');"}],
["@observablehq/inputs", {version: "0.10.6"}],
["@observablehq/plot", {version: "0.6.11"}],
["@observablehq/sample-datasets", {version: "1.0.1"}],
Expand Down Expand Up @@ -50,7 +51,7 @@ export function mockJsDelivr() {
.persist(); // prettier-ignore
cdnClient
.intercept({path: new RegExp(`^/npm/${name}@${pkg.version}/`), method: "GET"})
.reply(200, "", {headers: {"cache-control": "public, immutable", "content-type": "text/javascript; charset=utf-8"}})
.reply(200, pkg.contents ?? "", {headers: {"cache-control": "public, immutable", "content-type": "text/javascript; charset=utf-8"}})
.persist(); // prettier-ignore
}
});
Expand Down
1 change: 0 additions & 1 deletion test/output/build/imports/_import/bar/bar.13bb8056.js

This file was deleted.

1 change: 1 addition & 0 deletions test/output/build/imports/_import/bar/bar.4460ccc2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {bar} from "./baz.2add1dd0.js";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {foo} from "../foo/foo.5963bf78.js";
import {foo} from "../foo/foo.bcd720b2.js";

export const bar = "bar";
export const foobar = foo + "bar";
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "../../_npm/d3@7.8.5/cd372fb8.js";
import {bar} from "../bar/bar.13bb8056.js";
import "../../_npm/@example/url-import@1.0.0/1dd108c5.js";
import {bar} from "../bar/bar.4460ccc2.js";
export {top} from "../top.160847a6.js";

export const foo = "foo";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('https://example.com');
9 changes: 5 additions & 4 deletions test/output/build/imports/foo/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
<link rel="modulepreload" href="../_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="../_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="../_npm/d3@7.8.5/cd372fb8.js">
<link rel="modulepreload" href="../_import/bar/bar.13bb8056.js">
<link rel="modulepreload" href="../_import/bar/bar.4460ccc2.js">
<link rel="modulepreload" href="../_import/top.160847a6.js">
<link rel="modulepreload" href="../_import/foo/foo bar.b173d3de.js">
<link rel="modulepreload" href="../_import/bar/baz.cdbfb28b.js">
<link rel="modulepreload" href="../_import/foo/foo.5963bf78.js">
<link rel="modulepreload" href="../_import/bar/baz.2add1dd0.js">
<link rel="modulepreload" href="../_import/foo/foo.bcd720b2.js">
<link rel="modulepreload" href="../_npm/@example/url-import@1.0.0/1dd108c5.js">
<script type="module">

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

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

display(bar);
display(top);
Expand Down
Loading