Skip to content

Commit

Permalink
build: fix dts rewrite to avoid exporting non-exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 29, 2023
1 parent ba9a2d5 commit 08fd197
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions rollup.dts.config.js
Expand Up @@ -88,18 +88,35 @@ 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 (
(node.type === 'TSTypeAliasDeclaration' ||
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)
Expand Down

0 comments on commit 08fd197

Please sign in to comment.