From 845e3ac662d84875fa2d7679708d28289cdd50dc Mon Sep 17 00:00:00 2001 From: Kevin Deng Date: Mon, 22 Dec 2025 21:10:28 +0800 Subject: [PATCH] fix: shared chunk --- dts.snapshot.json | 2 +- src/api.ts | 26 ++++++++++++++------------ src/index.ts | 7 +++++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/dts.snapshot.json b/dts.snapshot.json index 136b52d..c9dd07f 100644 --- a/dts.snapshot.json +++ b/dts.snapshot.json @@ -1,6 +1,6 @@ { "api.d.mts": { - "snapshot": "declare function snapshot(_: string, _: string): Record", + "snapshot": "declare function snapshot(_: string, _: string, _: { applyExportRename?: boolean }): Record", "#exports": [ "snapshot" ] diff --git a/src/api.ts b/src/api.ts index daaba54..350d740 100644 --- a/src/api.ts +++ b/src/api.ts @@ -10,6 +10,7 @@ const singlelineCommentsRE = /\/\/.*$/gm export function snapshot( code: string, fileName: string = 'dummy.d.ts', + { applyExportRename = true }: { applyExportRename?: boolean } = {}, ): Record { code = code .replaceAll(multilineCommentsRE, '') @@ -81,21 +82,22 @@ export function snapshot( } } - for (const stmt of program.body) { - if ( - stmt.type === 'ExportNamedDeclaration' && - stmt.declaration === null && - stmt.specifiers.length - ) { - for (const specifier of stmt.specifiers) { - const exported = nodeToString(specifier.exported) - const local = nodeToString(specifier.local) - if (local !== exported) { - result[exported] = result[local] + if (applyExportRename) + for (const stmt of program.body) { + if ( + stmt.type === 'ExportNamedDeclaration' && + stmt.declaration === null && + stmt.specifiers.length + ) { + for (const specifier of stmt.specifiers) { + const exported = nodeToString(specifier.exported) + const local = nodeToString(specifier.local) + if (local !== exported) { + result[exported] = result[local] + } } } } - } return result diff --git a/src/index.ts b/src/index.ts index 8f72d2c..70ba6d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,8 +43,10 @@ export function DtsSnapshot({ if (chunk.type === 'asset' || !filter(chunk.fileName)) continue const map: Record = (result[ - chunk.fileName - ] = snapshot(chunk.code, chunk.fileName)) + chunk.preliminaryFileName + ] = snapshot(chunk.code, chunk.fileName, { + applyExportRename: chunk.isEntry, + })) if (chunk.isEntry) { if (excludeNonExport) { @@ -54,6 +56,7 @@ export function DtsSnapshot({ } } } + map['#exports'] = chunk.exports } } -- 2.51.2