Skip to content

Commit eebc8c9

Browse files
committed
fix duplicate module preload
1 parent 76c7509 commit eebc8c9

File tree

5 files changed

+11
-6
lines changed

5 files changed

+11
-6
lines changed

src/render.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ function getImportPreloads(parseResult: ParseResult, path: string): Iterable<str
131131
if (inputs.has("dot")) specifiers.add("npm:@viz-js/viz");
132132
if (inputs.has("mermaid")) specifiers.add("npm:mermaid").add("npm:d3");
133133
if (inputs.has("tex")) specifiers.add("npm:katex");
134-
const preloads: string[] = [];
134+
const preloads = new Set<string>();
135135
for (const specifier of specifiers) {
136-
preloads.push(resolveImport(specifier));
136+
preloads.add(resolveImport(specifier));
137137
}
138138
if (parseResult.cells.some((cell) => cell.databases?.length)) {
139-
preloads.push("/_observablehq/database.js");
139+
preloads.add("/_observablehq/database.js");
140140
}
141141
return preloads;
142142
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "https://cdn.jsdelivr.net/npm/d3/+esm";
12
import {bar} from "../bar/bar.js";
23

34
export const foo = "foo";

test/input/build/imports/foo/foo.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Foo
22

33
```js
4+
import * as d3 from "npm:d3";
45
import {bar} from "../bar/bar.js";
56

67
display(bar);

test/output/build/imports/_import/foo/foo.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import "https://cdn.jsdelivr.net/npm/d3/+esm";
12
import {bar} from "../bar/bar.js";
23

34
export const foo = "foo";

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@
66
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Source+Serif+Pro:ital,wght@0,400;0,600;0,700;1,400;1,600;1,700&display=swap">
77
<link rel="stylesheet" type="text/css" href="/_observablehq/style.css">
88
<link rel="modulepreload" href="/_observablehq/runtime.js">
9+
<link rel="modulepreload" href="https://cdn.jsdelivr.net/npm/d3/+esm">
910
<link rel="modulepreload" href="/_import/bar/bar.js">
1011
<link rel="modulepreload" href="/_import/bar/baz.js">
1112
<link rel="modulepreload" href="/_import/foo/foo.js">
1213
<script type="module">
1314

1415
import {define} from "/_observablehq/client.js";
1516

16-
define({id: "d54e931e", inputs: ["display"], outputs: ["bar"], body: async (display) => {
17+
define({id: "a9220fae", inputs: ["display"], outputs: ["d3","bar"], body: async (display) => {
18+
const d3 = await import("https://cdn.jsdelivr.net/npm/d3/+esm");
1719
const {bar} = await import("/_import/bar/bar.js");
1820

1921
display(bar);
20-
return {bar};
22+
return {d3,bar};
2123
}});
2224

2325
</script>
2426
<div id="observablehq-center">
2527
<main id="observablehq-main" class="observablehq">
2628
<h1 id="foo" tabindex="-1"><a class="observablehq-header-anchor" href="#foo">Foo</a></h1>
27-
<div id="cell-d54e931e" class="observablehq observablehq--block"></div>
29+
<div id="cell-a9220fae" class="observablehq observablehq--block"></div>
2830
</main>
2931
<footer id="observablehq-footer">© 2023 Observable, Inc.</footer>
3032
</div>

0 commit comments

Comments
 (0)