Skip to content

Commit

Permalink
deduplicate resolved imports (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock authored Oct 3, 2024
1 parent 04161d0 commit e09dbe9
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {PageLink} from "./pager.js";
import {findLink, normalizePath} from "./pager.js";
import {isAssetPath, resolvePath, resolveRelativePath} from "./path.js";
import type {Resolvers} from "./resolvers.js";
import {getModuleStaticImports, getResolvers} from "./resolvers.js";
import {getModuleResolver, getModuleStaticImports, getResolvers} from "./resolvers.js";
import {rollupClient} from "./rollup.js";

export interface RenderOptions extends Config {
Expand Down Expand Up @@ -281,16 +281,26 @@ function hasGoogleFonts(stylesheets: Set<string>): boolean {
return false;
}

export type RenderModuleOptions = Omit<TranspileModuleOptions, "root" | "path" | "servePath" | "params">;

export async function renderModule(
root: string,
path: string,
options?: Omit<TranspileModuleOptions, "root" | "path" | "servePath" | "params">
{resolveImport = getModuleResolver(root, path), ...options}: RenderModuleOptions = {}
): Promise<string> {
const module = findModule(root, path);
if (!module) throw enoent(path);
const input = `${(await getModuleStaticImports(root, path))
.map((i) => `import ${JSON.stringify(i)};\n`)
.join("")}export * from ${JSON.stringify(path)};
`;
return await transpileModule(input, {root, path, servePath: path, params: module.params, ...options});
const imports = new Set<string>();
const resolutions = new Set<string>();
for (const i of await getModuleStaticImports(root, path)) {
const r = await resolveImport(i);
if (!resolutions.has(r)) {
resolutions.add(r);
imports.add(i);
}
}
const input = Array.from(imports, (i) => `import ${JSON.stringify(i)};\n`)
.concat(`export * from ${JSON.stringify(path)};\n`)
.join("");
return await transpileModule(input, {root, path, servePath: path, params: module.params, resolveImport, ...options});
}

0 comments on commit e09dbe9

Please sign in to comment.