From a8941989f7bf61716d73e32c33542d62056a102d Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Mon, 14 Oct 2024 14:38:23 -0400 Subject: [PATCH 1/3] fix #1755; handle absolute global imports --- src/build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build.ts b/src/build.ts index b34d8796c..ccf9df04d 100644 --- a/src/build.ts +++ b/src/build.ts @@ -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)); } From 5ab16ab33d7cbba5f7fe6dbfd8bc11121594ff03 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Mon, 14 Oct 2024 14:46:07 -0400 Subject: [PATCH 2/3] test --- test/input/build/imports/foo/foo.js | 1 + test/mocks/jsdelivr.ts | 5 +++-- test/output/build/imports/_import/bar/bar.13bb8056.js | 1 - test/output/build/imports/_import/bar/bar.4460ccc2.js | 1 + .../_import/bar/{baz.cdbfb28b.js => baz.2add1dd0.js} | 2 +- .../_import/foo/{foo.5963bf78.js => foo.bcd720b2.js} | 3 ++- .../imports/_npm/@example/url-import@1.0.0/1dd108c5.js | 1 + test/output/build/imports/foo/foo.html | 9 +++++---- 8 files changed, 14 insertions(+), 9 deletions(-) delete mode 100644 test/output/build/imports/_import/bar/bar.13bb8056.js create mode 100644 test/output/build/imports/_import/bar/bar.4460ccc2.js rename test/output/build/imports/_import/bar/{baz.cdbfb28b.js => baz.2add1dd0.js} (58%) rename test/output/build/imports/_import/foo/{foo.5963bf78.js => foo.bcd720b2.js} (58%) create mode 100644 test/output/build/imports/_npm/@example/url-import@1.0.0/1dd108c5.js diff --git a/test/input/build/imports/foo/foo.js b/test/input/build/imports/foo/foo.js index 41f582b58..7a1409aef 100644 --- a/test/input/build/imports/foo/foo.js +++ b/test/input/build/imports/foo/foo.js @@ -1,4 +1,5 @@ import "npm:d3"; +import "npm:@example/url-import"; import {bar} from "../bar/bar.js"; export {top} from "/top.js"; diff --git a/test/mocks/jsdelivr.ts b/test/mocks/jsdelivr.ts index 1dfbd4eee..70119be4b 100644 --- a/test/mocks/jsdelivr.ts +++ b/test/mocks/jsdelivr.ts @@ -1,7 +1,8 @@ import {getCurrentAgent, mockAgent} from "./undici.js"; -const packages: [name: string, {version: string; dependencies?: Record}][] = [ +const packages: [name: string, {version: string; contents?: string; dependencies?: Record}][] = [ ["@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"}], @@ -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 } }); diff --git a/test/output/build/imports/_import/bar/bar.13bb8056.js b/test/output/build/imports/_import/bar/bar.13bb8056.js deleted file mode 100644 index 43b124da9..000000000 --- a/test/output/build/imports/_import/bar/bar.13bb8056.js +++ /dev/null @@ -1 +0,0 @@ -export {bar} from "./baz.cdbfb28b.js"; diff --git a/test/output/build/imports/_import/bar/bar.4460ccc2.js b/test/output/build/imports/_import/bar/bar.4460ccc2.js new file mode 100644 index 000000000..e1fd9bbee --- /dev/null +++ b/test/output/build/imports/_import/bar/bar.4460ccc2.js @@ -0,0 +1 @@ +export {bar} from "./baz.2add1dd0.js"; diff --git a/test/output/build/imports/_import/bar/baz.cdbfb28b.js b/test/output/build/imports/_import/bar/baz.2add1dd0.js similarity index 58% rename from test/output/build/imports/_import/bar/baz.cdbfb28b.js rename to test/output/build/imports/_import/bar/baz.2add1dd0.js index 47e85439e..1e2c35f80 100644 --- a/test/output/build/imports/_import/bar/baz.cdbfb28b.js +++ b/test/output/build/imports/_import/bar/baz.2add1dd0.js @@ -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"; diff --git a/test/output/build/imports/_import/foo/foo.5963bf78.js b/test/output/build/imports/_import/foo/foo.bcd720b2.js similarity index 58% rename from test/output/build/imports/_import/foo/foo.5963bf78.js rename to test/output/build/imports/_import/foo/foo.bcd720b2.js index ccf22fb68..1eb0af22a 100644 --- a/test/output/build/imports/_import/foo/foo.5963bf78.js +++ b/test/output/build/imports/_import/foo/foo.bcd720b2.js @@ -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"; diff --git a/test/output/build/imports/_npm/@example/url-import@1.0.0/1dd108c5.js b/test/output/build/imports/_npm/@example/url-import@1.0.0/1dd108c5.js new file mode 100644 index 000000000..cc3538b9e --- /dev/null +++ b/test/output/build/imports/_npm/@example/url-import@1.0.0/1dd108c5.js @@ -0,0 +1 @@ +import('https://example.com'); \ No newline at end of file diff --git a/test/output/build/imports/foo/foo.html b/test/output/build/imports/foo/foo.html index cc2500f63..2cae2575d 100644 --- a/test/output/build/imports/foo/foo.html +++ b/test/output/build/imports/foo/foo.html @@ -14,11 +14,12 @@ - + - - + + +