Skip to content

Commit 63f0d42

Browse files
authored
move imports to _import (#136)
1 parent f59f3cf commit 63f0d42

File tree

12 files changed

+29
-13
lines changed

12 files changed

+29
-13
lines changed

src/build.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export async function build(context: CommandContext = makeCommandContext()) {
3232
const config = await readConfig(sourceRoot);
3333
const pages = await readPages(sourceRoot);
3434
const files: string[] = [];
35+
const imports: string[] = [];
3536
const resolver = await makeCLIResolver();
3637
for await (const sourceFile of visitMarkdownFiles(sourceRoot)) {
3738
const sourcePath = join(sourceRoot, sourceFile);
@@ -46,7 +47,7 @@ export async function build(context: CommandContext = makeCommandContext()) {
4647
resolver
4748
});
4849
files.push(...render.files.map((f) => join(dirname(sourceFile), f.name)));
49-
files.push(...render.imports.filter((i) => i.type === "local").map((i) => join(dirname(sourceFile), i.name)));
50+
imports.push(...render.imports.filter((i) => i.type === "local").map((i) => join(dirname(sourceFile), i.name)));
5051
await prepareOutput(outputPath);
5152
await writeFile(outputPath, render.html);
5253
}
@@ -82,6 +83,19 @@ export async function build(context: CommandContext = makeCommandContext()) {
8283
await copyFile(sourcePath, outputPath);
8384
}
8485

86+
// Copy over the imported modules.
87+
for (const file of imports) {
88+
const sourcePath = join(sourceRoot, file);
89+
const outputPath = join(outputRoot, "_import", file);
90+
if (!existsSync(sourcePath)) {
91+
console.error("missing referenced file", sourcePath);
92+
continue;
93+
}
94+
if (verbose) console.log("copy", sourcePath, "→", outputPath);
95+
await prepareOutput(outputPath);
96+
await copyFile(sourcePath, outputPath);
97+
}
98+
8599
// Copy over required distribution files from node_modules.
86100
// TODO: Note that this requires that the build command be run relative to the node_modules directory.
87101
if (addPublic) {

src/javascript/imports.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function findImports(body: Node, root: string, sourcePath: string) {
6666
findLocalImports(join(dirname(path), value));
6767
} else {
6868
imports.push({name: value, type: "global"});
69-
// non-local imports don't need to be promoted to file attachments
69+
// non-local imports don't need to be traversed
7070
}
7171
}
7272
}
@@ -86,7 +86,7 @@ export function rewriteImports(output: any, rootNode: JavaScriptNode, root: stri
8686
node.source.end,
8787
JSON.stringify(
8888
isLocalImport(value, root, sourcePath)
89-
? join("/_file/", join(dirname(sourcePath), value))
89+
? join("/_import/", join(dirname(sourcePath), value))
9090
: resolveImport(value)
9191
)
9292
);
@@ -107,7 +107,7 @@ export function rewriteImports(output: any, rootNode: JavaScriptNode, root: stri
107107
: "{}"
108108
} = await import(${JSON.stringify(
109109
isLocalImport(value, root, sourcePath)
110-
? join("/_file/", join(dirname(sourcePath), value))
110+
? join("/_import/", join(dirname(sourcePath), value))
111111
: resolveImport(value)
112112
)});`
113113
);

src/preview.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class Server {
5151
send(req, "/@observablehq/runtime/dist/runtime.js", {root: "./node_modules"}).pipe(res);
5252
} else if (pathname.startsWith("/_observablehq/")) {
5353
send(req, pathname.slice("/_observablehq".length), {root: publicRoot}).pipe(res);
54+
} else if (pathname.startsWith("/_import/")) {
55+
send(req, pathname.slice("/_import".length), {root: this.root}).pipe(res);
5456
} else if (pathname.startsWith("/_file/")) {
5557
const path = pathname.slice("/_file".length);
5658
const filepath = join(this.root, path);

src/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function getImportPreloads(parseResult: ParseResult, path: string): Iterable<str
118118
const specifiers = new Set<string>(["npm:@observablehq/runtime"]);
119119
for (const {name, type} of parseResult.imports) {
120120
if (type === "local") {
121-
specifiers.add(`/_file${join(dirname(path), name)}`);
121+
specifiers.add(`/_import${join(dirname(path), name)}`);
122122
} else {
123123
specifiers.add(name);
124124
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
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="/_file/bar/bar.js">
10-
<link rel="modulepreload" href="/_file/bar/baz.js">
11-
<link rel="modulepreload" href="/_file/foo/foo.js">
9+
<link rel="modulepreload" href="/_import/bar/bar.js">
10+
<link rel="modulepreload" href="/_import/bar/baz.js">
11+
<link rel="modulepreload" href="/_import/foo/foo.js">
1212
<script type="module">
1313

1414
import {define} from "/_observablehq/client.js";
1515

1616
define({id: "d54e931e", inputs: ["display"], outputs: ["bar"], body: async (display) => {
17-
const {bar} = await import("/_file/bar/bar.js");
17+
const {bar} = await import("/_import/bar/bar.js");
1818

1919
display(bar);
2020
return {bar};

test/output/imports/dynamic-import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
define({id: "0", outputs: ["foo"], body: async () => {
2-
const foo = await import("/_file/bar.js");
2+
const foo = await import("/_import/bar.js");
33
return {foo};
44
}});

test/output/imports/static-import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
define({id: "0", inputs: ["display"], outputs: ["foo"], body: async (display) => {
2-
const {foo} = await import("/_file/bar.js");
2+
const {foo} = await import("/_import/bar.js");
33

44
display(foo);
55
return {foo};

0 commit comments

Comments
 (0)