Skip to content

Commit d137767

Browse files
committed
WIP
1 parent 3757b4f commit d137767

File tree

5 files changed

+100
-8
lines changed

5 files changed

+100
-8
lines changed

packages/observablehq-compiler/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@observablehq/inspector": "5.0.1",
5252
"@observablehq/stdlib": "5.8.8",
5353
"@observablehq/notebook-kit": "1.0.1"
54+
5455
},
5556
"repository": {
5657
"type": "git",

packages/observablehq-compiler/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export * from "./writer.ts";
66

77
import "../src/index.css";
88

9-
// export { ohqnk } from "./ohqnk/index.ts";
9+
// Note: ohqnk exports are not re-exported at the root to avoid pulling
10+
// @observablehq/notebook-kit (which uses npm: specifiers and top-level await)
11+
// into Node/test environments by default.

packages/observablehq-compiler/src/ohqnk/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Notebook, Cell } from '@observablehq/notebook-kit';
2-
import { define, runtime, main, type DefineState, type Definition } from '@observablehq/notebook-kit/runtime';
1+
import type { Notebook } from "@observablehq/notebook-kit";
2+
import { define, type DefineState, type Definition } from "@observablehq/notebook-kit/runtime";
33
import type { Inspector } from "@observablehq/inspector";
44
import type { Runtime, Module } from "@observablehq/runtime";
55

packages/observablehq-compiler/src/ohqnk/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Notebook as ohqnkNotebook, Cell as ohqnkCell } from '@observablehq/notebook-kit';
1+
import { Notebook as ohqnkNotebook, Cell as ohqnkCell } from "@observablehq/notebook-kit";
22
import { compile as ohqnkCompile } from "./compiler.ts";
33

44
export namespace ohqnk {

packages/observablehq-compiler/vite.config.ts

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { createHpccViteConfig, browserConfig, nodeConfig } from "@hpcc-js/esbuil
22
import pkg from "./package.json" with { type: "json" };
33

44
const aliasPlugin = {
5-
name: 'alias-plugin',
5+
name: "alias-plugin",
6+
// esbuild plugin interface
67
setup(build) {
78
const aliases = [
89
{
@@ -19,17 +20,46 @@ const aliasPlugin = {
1920
for (const alias of aliases) {
2021
const match = args.path.match(alias.find);
2122
if (match) {
22-
const resolved = alias.replacement.replace('$1', match[1]);
23+
const resolved = alias.replacement.replace("$1", match[1]);
2324
return { path: resolved, external: true };
2425
}
2526
}
2627
});
28+
},
29+
// Rollup plugin interface
30+
resolveId(id, importer) {
31+
const aliases = [
32+
{
33+
find: /^npm:(.*)$/,
34+
replacement: "https://cdn.jsdelivr.net/npm/$1/+esm"
35+
},
36+
{
37+
find: /^jsr:(.*)$/,
38+
replacement: "https://esm.sh/jsr/$1"
39+
}
40+
];
41+
42+
for (const alias of aliases) {
43+
const match = id.match(alias.find);
44+
if (match) {
45+
const resolved = alias.replacement.replace("$1", match[1]);
46+
return { id: resolved, external: true };
47+
}
48+
}
49+
return null;
2750
}
2851
};
2952

3053
const myBrowserConfig = { ...browserConfig };
3154
myBrowserConfig.optimizeDeps = {
32-
include: ["acorn", "acorn-walk", "@observablehq/parser"]
55+
include: [
56+
"acorn",
57+
"acorn-walk",
58+
"@observablehq/parser",
59+
"@observablehq/stdlib",
60+
"@observablehq/runtime",
61+
"@observablehq/inspector"
62+
]
3363
};
3464

3565
const myNodeConfig = { ...nodeConfig };
@@ -40,8 +70,67 @@ myNodeConfig.optimizeDeps = {
4070
const config = createHpccViteConfig(pkg, {
4171
plugins: [aliasPlugin],
4272
configOverrides: {
73+
// Ensure modern syntax like top-level await is supported during transforms
74+
esbuild: {
75+
target: "esnext",
76+
supported: { "top-level-await": true }
77+
},
78+
// Make sure dependency optimization (esbuild) also understands our custom schemes
79+
optimizeDeps: {
80+
esbuildOptions: {
81+
target: "esnext",
82+
supported: { "top-level-await": true },
83+
plugins: [aliasPlugin as any]
84+
}
85+
},
86+
build: {
87+
target: "esnext",
88+
rollupOptions: {
89+
external: [
90+
"@observablehq/notebook-kit",
91+
"@observablehq/notebook-kit/runtime"
92+
],
93+
output: {
94+
globals: {
95+
"@observablehq/notebook-kit": "ObservableNotebookKit",
96+
"@observablehq/notebook-kit/runtime": "ObservableNotebookKitRuntime"
97+
}
98+
}
99+
}
100+
},
43101
test: {
44-
projects: [myBrowserConfig, myNodeConfig]
102+
projects: [myBrowserConfig, myNodeConfig],
103+
deps: {
104+
optimizer: {
105+
web: {
106+
exclude: [
107+
"@observablehq/notebook-kit",
108+
"@observablehq/notebook-kit/runtime"
109+
],
110+
esbuildOptions: {
111+
target: "esnext",
112+
supported: { "top-level-await": true },
113+
plugins: [aliasPlugin as any]
114+
}
115+
},
116+
ssr: {
117+
exclude: [
118+
"@observablehq/notebook-kit",
119+
"@observablehq/notebook-kit/runtime"
120+
],
121+
esbuildOptions: {
122+
target: "esnext",
123+
supported: { "top-level-await": true },
124+
plugins: [aliasPlugin as any]
125+
}
126+
}
127+
}
128+
},
129+
alias: {
130+
// Use local stubs to avoid loading notebook-kit in tests
131+
"@observablehq/notebook-kit": "/tests/mocks/notebook-kit.stub.ts",
132+
"@observablehq/notebook-kit/runtime": "/tests/mocks/notebook-kit.stub.ts"
133+
}
45134
}
46135
}
47136
});

0 commit comments

Comments
 (0)