Skip to content

Commit

Permalink
annotate local paths (_import and _file)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Oct 8, 2024
1 parent abef61b commit 6cfb74d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/javascript/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {ImportDeclaration, ImportDefaultSpecifier, ImportNamespaceSpecifier
import {simple} from "acorn-walk";
import mime from "mime";
import {isPathImport, relativePath, resolvePath, resolveRelativePath} from "../path.js";
import {annotatePath} from "../render.js";
import {getModuleResolver} from "../resolvers.js";
import type {Params} from "../route.js";
import {Sourcemap} from "../sourcemap.js";
Expand Down Expand Up @@ -159,7 +160,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 +177,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 +204,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
27 changes: 18 additions & 9 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ function registerFile(
getInfo: (name: string) => FileInfo | undefined
): string {
const info = getInfo(name);
return `\nregisterFile(${JSON.stringify(name)}, ${JSON.stringify({
name,
mimeType: mime.getType(name) ?? undefined,
path: resolve(name),
lastModified: info?.mtimeMs,
size: info?.size
})});`;
return `\nregisterFile(${JSON.stringify(name)}, {
name: ${JSON.stringify(name)},
mimeType: ${JSON.stringify(mime.getType(name) ?? undefined)},
path: ${annotatePath(resolve(name))},
lastModified: ${JSON.stringify(info?.mtimeMs)},
size: ${JSON.stringify(info?.size)}
});`;
}

async function renderSidebar(options: RenderOptions, {resolveImport, resolveLink}: Resolvers): Promise<Html> {
Expand Down Expand Up @@ -299,8 +299,17 @@ export async function renderModule(
imports.add(i);
}
}
const input = Array.from(imports, (i) => `import ${JSON.stringify(i)};\n`)
.concat(`export * from ${JSON.stringify(path)};\n`)
const input = Array.from(imports, (i) => `import ${annotatePath(i)};\n`)
.concat(`export * from ${annotatePath(path)};\n`)
.join("");
return await transpileModule(input, {root, path, servePath: path, params: module.params, resolveImport, ...options});
}

/**
* Annotate a path to a local import or file so it can be reworked server-side.
*/
export function annotatePath(uri: string): string {
return !uri.startsWith(".") || /(?:[.][/]|(?:[.][.][/])+)(_jsr|_node|_npm|_observablehq)[/]/.test(uri)
? JSON.stringify(uri)
: (console.warn(`/* 👉 */${JSON.stringify(uri)}/* 👈 */`), `/* 👉 */${JSON.stringify(uri)}/* 👈 */`);
}

0 comments on commit 6cfb74d

Please sign in to comment.