Skip to content

Commit febf851

Browse files
authored
import.meta.resolve runtime (#572)
1 parent 05505b2 commit febf851

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/build.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {existsSync} from "node:fs";
22
import {access, constants, copyFile, readFile, writeFile} from "node:fs/promises";
33
import {basename, dirname, join} from "node:path";
4+
import {fileURLToPath} from "node:url";
45
import type {Config, Style} from "./config.js";
56
import {mergeStyle} from "./config.js";
67
import {Loader} from "./dataloader.js";
@@ -14,7 +15,12 @@ import {Telemetry} from "./telemetry.js";
1415
import {faint} from "./tty.js";
1516
import {resolvePath} from "./url.js";
1617

17-
const EXTRA_FILES = new Map([["node_modules/@observablehq/runtime/dist/runtime.js", "_observablehq/runtime.js"]]);
18+
const EXTRA_FILES = new Map([
19+
[
20+
join(fileURLToPath(import.meta.resolve("@observablehq/runtime")), "../../dist/runtime.js"),
21+
"_observablehq/runtime.js"
22+
]
23+
]);
1824

1925
// TODO Remove library helpers (e.g., duckdb) when they are published to npm.
2026
function clientBundles(clientPath: string): [entry: string, name: string][] {
@@ -156,8 +162,7 @@ export async function build(
156162
await effects.writeFile(outputPath, contents);
157163
}
158164

159-
// Copy over required distribution files from node_modules.
160-
// TODO: Note that this requires that the build command be run relative to the node_modules directory.
165+
// Copy over required distribution files.
161166
if (addPublic) {
162167
for (const [sourcePath, outputPath] of EXTRA_FILES) {
163168
effects.output.write(`${faint("copy")} ${sourcePath} ${faint("→")} `);

src/preview.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export class PreviewServer {
8989
let {pathname} = url;
9090
let match: RegExpExecArray | null;
9191
if (pathname === "/_observablehq/runtime.js") {
92-
send(req, "/@observablehq/runtime/dist/runtime.js", {root: "./node_modules"}).pipe(res);
92+
const root = join(fileURLToPath(import.meta.resolve("@observablehq/runtime")), "../../");
93+
send(req, "/dist/runtime.js", {root}).pipe(res);
9394
} else if (pathname.startsWith("/_observablehq/stdlib.js")) {
9495
end(req, res, await rollupClient(getClientPath("./src/client/stdlib.js")), "text/javascript");
9596
} else if (pathname.startsWith("/_observablehq/stdlib/")) {

0 commit comments

Comments
 (0)