From 08fd197d4a32750907744347d04280274ec7d975 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 29 Mar 2023 18:19:24 +0800 Subject: [PATCH] build: fix dts rewrite to avoid exporting non-exported types --- rollup.dts.config.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/rollup.dts.config.js b/rollup.dts.config.js index 84a6138b44b..ca811d349d5 100644 --- a/rollup.dts.config.js +++ b/rollup.dts.config.js @@ -88,7 +88,21 @@ function patchTypes(pkg) { return false } + const isExported = new Set() const shouldRemoveExport = new Set() + + // pass 0: check all exported types + for (const node of ast.program.body) { + if (node.type === 'ExportNamedDeclaration' && !node.source) { + for (let i = 0; i < node.specifiers.length; i++) { + const spec = node.specifiers[i] + if (spec.type === 'ExportSpecifier') { + isExported.add(spec.local.name) + } + } + } + } + // pass 1: remove internals + add exports for (const node of ast.program.body) { if ( @@ -96,10 +110,13 @@ function patchTypes(pkg) { node.type === 'TSInterfaceDeclaration') && !node.id.name.startsWith(`_`) ) { - shouldRemoveExport.add(node.id.name) + const name = node.id.name + shouldRemoveExport.add(name) if (!removeInternal(node)) { - // @ts-ignore - s.prependLeft(node.start, `export `) + if (isExported.has(name)) { + // @ts-ignore + s.prependLeft(node.start, `export `) + } // traverse further for internal properties if (node.type === 'TSInterfaceDeclaration') { node.body.body.forEach(removeInternal)