Skip to content

Commit e09dbe9

Browse files
authored
deduplicate resolved imports (#1719)
1 parent 04161d0 commit e09dbe9

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/render.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {PageLink} from "./pager.js";
1515
import {findLink, normalizePath} from "./pager.js";
1616
import {isAssetPath, resolvePath, resolveRelativePath} from "./path.js";
1717
import type {Resolvers} from "./resolvers.js";
18-
import {getModuleStaticImports, getResolvers} from "./resolvers.js";
18+
import {getModuleResolver, getModuleStaticImports, getResolvers} from "./resolvers.js";
1919
import {rollupClient} from "./rollup.js";
2020

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

284+
export type RenderModuleOptions = Omit<TranspileModuleOptions, "root" | "path" | "servePath" | "params">;
285+
284286
export async function renderModule(
285287
root: string,
286288
path: string,
287-
options?: Omit<TranspileModuleOptions, "root" | "path" | "servePath" | "params">
289+
{resolveImport = getModuleResolver(root, path), ...options}: RenderModuleOptions = {}
288290
): Promise<string> {
289291
const module = findModule(root, path);
290292
if (!module) throw enoent(path);
291-
const input = `${(await getModuleStaticImports(root, path))
292-
.map((i) => `import ${JSON.stringify(i)};\n`)
293-
.join("")}export * from ${JSON.stringify(path)};
294-
`;
295-
return await transpileModule(input, {root, path, servePath: path, params: module.params, ...options});
293+
const imports = new Set<string>();
294+
const resolutions = new Set<string>();
295+
for (const i of await getModuleStaticImports(root, path)) {
296+
const r = await resolveImport(i);
297+
if (!resolutions.has(r)) {
298+
resolutions.add(r);
299+
imports.add(i);
300+
}
301+
}
302+
const input = Array.from(imports, (i) => `import ${JSON.stringify(i)};\n`)
303+
.concat(`export * from ${JSON.stringify(path)};\n`)
304+
.join("");
305+
return await transpileModule(input, {root, path, servePath: path, params: module.params, resolveImport, ...options});
296306
}

0 commit comments

Comments
 (0)