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

annotate local paths #1731

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions docs/annotate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {FileAttachment} from "observablehq:stdlib";

// Framework’s FileAttachment
FileAttachment("./lib/miserables.json").json();

// Static ESM imports
import {foo, bar} from "./foo.js";

import {Chart} from "./chart.js";

// Dynamic ESM imports
const {Chart: c} = await import("./chart.js");

// import.meta.resolve
const chartHelperURL = import.meta.resolve("./chart.js");

console.warn({foo, bar, Chart, c, chartHelperURL});
1 change: 1 addition & 0 deletions observablehq.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default {
],
dynamicPaths: [
"/chart.js",
"/annotate.js",
"/theme/dark",
"/theme/dark-alt",
"/theme/dashboard",
Expand Down
40 changes: 23 additions & 17 deletions src/javascript/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function transpileModule(

async function rewriteImportSource(source: StringLiteral) {
const specifier = getStringLiteralValue(source);
output.replaceLeft(source.start, source.end, JSON.stringify(await resolveImport(specifier)));
output.replaceLeft(source.start, source.end, annotatePath(await resolveImport(specifier)));
}

for (const {name, node} of findFiles(body, path, input)) {
Expand All @@ -111,17 +111,16 @@ export async function transpileModule(
output.replaceLeft(
source.start,
source.end,
`${JSON.stringify(
`${
info
? {
name: p,
mimeType: mime.getType(name) ?? undefined,
path: relativePath(servePath, resolveFile(name)),
lastModified: info.mtimeMs,
size: info.size
}
: p
)}, import.meta.url`
? `{
name: ${JSON.stringify(p)},
mimeType: ${JSON.stringify(mime.getType(name) ?? undefined)},
path: ${annotatePath(relativePath(servePath, resolveFile(name)))},
lastModified: ${JSON.stringify(info.mtimeMs)},
size: ${JSON.stringify(info.size)}`
: JSON.stringify(p)
}, import.meta.url`
);
}

Expand All @@ -137,7 +136,7 @@ export async function transpileModule(
if (isImportMetaResolve(node) && isStringLiteral(source)) {
const value = getStringLiteralValue(source);
const resolution = isPathImport(value) && !isJavaScript(value) ? resolveFile(value) : await resolveImport(value);
output.replaceLeft(source.start, source.end, JSON.stringify(resolution));
output.replaceLeft(source.start, source.end, annotatePath(resolution));
}
}

Expand All @@ -159,7 +158,7 @@ function rewriteImportExpressions(
resolveFile: (specifier: string) => string = String
): void {
function rewriteImportSource(source: StringLiteral) {
output.replaceLeft(source.start, source.end, JSON.stringify(resolveImport(getStringLiteralValue(source))));
output.replaceLeft(source.start, source.end, annotatePath(resolveImport(getStringLiteralValue(source))));
}
simple(body, {
ImportExpression(node) {
Expand All @@ -176,9 +175,7 @@ function rewriteImportExpressions(
output.replaceLeft(
node.start,
node.end,
isPathImport(resolution)
? `new URL(${JSON.stringify(resolution)}, location).href`
: JSON.stringify(resolution)
isPathImport(resolution) ? `new URL(${annotatePath(resolution)}, location).href` : JSON.stringify(resolution)
);
}
}
Expand All @@ -205,7 +202,7 @@ function rewriteImportDeclarations(
for (const node of declarations) {
output.delete(node.start, node.end + +(output.input[node.end] === "\n"));
specifiers.push(rewriteImportSpecifiers(node));
imports.push(`import(${JSON.stringify(resolve(getStringLiteralValue(node.source as StringLiteral)))})`);
imports.push(`import(${annotatePath(resolve(getStringLiteralValue(node.source as StringLiteral)))})`);
}
if (declarations.length > 1) {
output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`);
Expand Down Expand Up @@ -259,3 +256,12 @@ export function rewriteParams(output: Sourcemap, body: Node, params: Params, inp
output.replaceLeft(node.start, node.end, JSON.stringify(params[name]));
}
}

/**
* Annotate a path to a local import or file so it can be reworked server-side.
*/
function annotatePath(uri: string): string {
return !uri.startsWith(".") || /(?:[.][/]|(?:[.][.][/])+)(_jsr|_node|_npm|_observablehq)[/]/.test(uri)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be isAssetPath or whatever that’s ['./', '../', '/']. Don’t want to match against .foo.

? JSON.stringify(uri)
: (console.warn(`/* 👉 */${JSON.stringify(uri)}/* 👈 */`), `/* 👉 */${JSON.stringify(uri)}/* 👈 */`);
}
Loading