Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move imports to _import #136

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function build(context: CommandContext = makeCommandContext()) {
const config = await readConfig(sourceRoot);
const pages = await readPages(sourceRoot);
const files: string[] = [];
const imports: string[] = [];
const resolver = await makeCLIResolver();
for await (const sourceFile of visitMarkdownFiles(sourceRoot)) {
const sourcePath = join(sourceRoot, sourceFile);
Expand All @@ -46,7 +47,7 @@ export async function build(context: CommandContext = makeCommandContext()) {
resolver
});
files.push(...render.files.map((f) => join(dirname(sourceFile), f.name)));
files.push(...render.imports.filter((i) => i.type === "local").map((i) => join(dirname(sourceFile), i.name)));
imports.push(...render.imports.filter((i) => i.type === "local").map((i) => join(dirname(sourceFile), i.name)));
await prepareOutput(outputPath);
await writeFile(outputPath, render.html);
}
Expand Down Expand Up @@ -82,6 +83,19 @@ export async function build(context: CommandContext = makeCommandContext()) {
await copyFile(sourcePath, outputPath);
}

// Copy over the imported modules.
for (const file of imports) {
const sourcePath = join(sourceRoot, file);
const outputPath = join(outputRoot, "_import", file);
if (!existsSync(sourcePath)) {
console.error("missing referenced file", sourcePath);
continue;
}
if (verbose) console.log("copy", sourcePath, "→", outputPath);
await prepareOutput(outputPath);
await copyFile(sourcePath, outputPath);
}

// Copy over required distribution files from node_modules.
// TODO: Note that this requires that the build command be run relative to the node_modules directory.
if (addPublic) {
Expand Down
6 changes: 3 additions & 3 deletions src/javascript/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function findImports(body: Node, root: string, sourcePath: string) {
findLocalImports(join(dirname(path), value));
} else {
imports.push({name: value, type: "global"});
// non-local imports don't need to be promoted to file attachments
// non-local imports don't need to be traversed
}
}
}
Expand All @@ -86,7 +86,7 @@ export function rewriteImports(output: any, rootNode: JavaScriptNode, root: stri
node.source.end,
JSON.stringify(
isLocalImport(value, root, sourcePath)
? join("/_file/", join(dirname(sourcePath), value))
? join("/_import/", join(dirname(sourcePath), value))
: resolveImport(value)
)
);
Expand All @@ -107,7 +107,7 @@ export function rewriteImports(output: any, rootNode: JavaScriptNode, root: stri
: "{}"
} = await import(${JSON.stringify(
isLocalImport(value, root, sourcePath)
? join("/_file/", join(dirname(sourcePath), value))
? join("/_import/", join(dirname(sourcePath), value))
: resolveImport(value)
)});`
);
Expand Down
2 changes: 2 additions & 0 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Server {
send(req, "/@observablehq/runtime/dist/runtime.js", {root: "./node_modules"}).pipe(res);
} else if (pathname.startsWith("/_observablehq/")) {
send(req, pathname.slice("/_observablehq".length), {root: publicRoot}).pipe(res);
} else if (pathname.startsWith("/_import/")) {
send(req, pathname.slice("/_import".length), {root: this.root}).pipe(res);
} else if (pathname.startsWith("/_file/")) {
const path = pathname.slice("/_file".length);
const filepath = join(this.root, path);
Expand Down
2 changes: 1 addition & 1 deletion src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function getImportPreloads(parseResult: ParseResult, path: string): Iterable<str
const specifiers = new Set<string>(["npm:@observablehq/runtime"]);
for (const {name, type} of parseResult.imports) {
if (type === "local") {
specifiers.add(`/_file${join(dirname(path), name)}`);
specifiers.add(`/_import${join(dirname(path), name)}`);
} else {
specifiers.add(name);
}
Expand Down
8 changes: 4 additions & 4 deletions test/output/build/imports/foo/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<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">
<link rel="stylesheet" type="text/css" href="/_observablehq/style.css">
<link rel="modulepreload" href="/_observablehq/runtime.js">
<link rel="modulepreload" href="/_file/bar/bar.js">
<link rel="modulepreload" href="/_file/bar/baz.js">
<link rel="modulepreload" href="/_file/foo/foo.js">
<link rel="modulepreload" href="/_import/bar/bar.js">
<link rel="modulepreload" href="/_import/bar/baz.js">
<link rel="modulepreload" href="/_import/foo/foo.js">
<script type="module">

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

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

display(bar);
return {bar};
Expand Down
2 changes: 1 addition & 1 deletion test/output/imports/dynamic-import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define({id: "0", outputs: ["foo"], body: async () => {
const foo = await import("/_file/bar.js");
const foo = await import("/_import/bar.js");
return {foo};
}});
2 changes: 1 addition & 1 deletion test/output/imports/static-import.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define({id: "0", inputs: ["display"], outputs: ["foo"], body: async (display) => {
const {foo} = await import("/_file/bar.js");
const {foo} = await import("/_import/bar.js");

display(foo);
return {foo};
Expand Down
2 changes: 1 addition & 1 deletion test/output/imports/transitive-dynamic-import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define({id: "0", outputs: ["foo"], body: async () => {
const foo = await import("/_file/other/foo.js");
const foo = await import("/_import/other/foo.js");
return {foo};
}});
2 changes: 1 addition & 1 deletion test/output/imports/transitive-static-import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define({id: "0", outputs: ["foo"], body: async () => {
const {foo} = await import("/_file/other/foo.js");
const {foo} = await import("/_import/other/foo.js");
return {foo};
}});