Skip to content

Commit 0a8be45

Browse files
authored
build: use stripInternal (#9379)
Unlike our custom plugin, TypeScript won't automatically remove re-exports of internal types, so we need to explicitly mark them as `@internal`.
1 parent 2857a59 commit 0a8be45

File tree

3 files changed

+18
-74
lines changed

3 files changed

+18
-74
lines changed

packages/runtime-core/src/index.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ export {
7373
defineSlots,
7474
defineModel,
7575
withDefaults,
76-
useModel,
77-
// internal
76+
useModel
77+
} from './apiSetupHelpers'
78+
79+
/**
80+
* @internal
81+
*/
82+
export {
7883
mergeDefaults,
7984
mergeModels,
8085
createPropsRestProxy,
@@ -111,7 +116,9 @@ export { useSSRContext, ssrContextKey } from './helpers/useSsrContext'
111116

112117
export { createRenderer, createHydrationRenderer } from './renderer'
113118
export { queuePostFlushCb } from './scheduler'
114-
export { warn, assertNumber } from './warning'
119+
export { warn } from './warning'
120+
/** @internal */
121+
export { assertNumber } from './warning'
115122
export {
116123
handleError,
117124
callWithErrorHandling,

rollup.dts.config.js

+6-70
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { parse } from '@babel/parser'
33
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs'
44
import MagicString from 'magic-string'
55
import dts from 'rollup-plugin-dts'
6-
import { walk } from 'estree-walker'
76

87
if (!existsSync('temp/packages')) {
98
console.warn(
@@ -41,12 +40,11 @@ export default targetPackages.map(pkg => {
4140

4241
/**
4342
* Patch the dts generated by rollup-plugin-dts
44-
* 1. remove exports marked as @internal
45-
* 2. Convert all types to inline exports
43+
* 1. Convert all types to inline exports
4644
* and remove them from the big export {} declaration
4745
* otherwise it gets weird in vitepress `defineComponent` call with
4846
* "the inferred type cannot be named without a reference"
49-
* 3. Append custom augmentations (jsx, macros)
47+
* 2. Append custom augmentations (jsx, macros)
5048
* @returns {import('rollup').Plugin}
5149
*/
5250
function patchTypes(pkg) {
@@ -73,64 +71,12 @@ function patchTypes(pkg) {
7371
return
7472
}
7573
shouldRemoveExport.add(name)
76-
if (!removeInternal(parentDecl || node)) {
77-
if (isExported.has(name)) {
78-
// @ts-ignore
79-
s.prependLeft((parentDecl || node).start, `export `)
80-
}
81-
// traverse further for internal properties
82-
if (
83-
node.type === 'TSInterfaceDeclaration' ||
84-
node.type === 'ClassDeclaration'
85-
) {
86-
node.body.body.forEach(removeInternal)
87-
} else if (node.type === 'TSTypeAliasDeclaration') {
88-
// @ts-ignore
89-
walk(node.typeAnnotation, {
90-
enter(node) {
91-
// @ts-ignore
92-
if (removeInternal(node)) this.skip()
93-
}
94-
})
95-
}
74+
if (isExported.has(name)) {
75+
// @ts-ignore
76+
s.prependLeft((parentDecl || node).start, `export `)
9677
}
9778
}
9879

99-
/**
100-
* @param {import('@babel/types').Node} node
101-
* @returns {boolean}
102-
*/
103-
function removeInternal(node) {
104-
if (
105-
node.leadingComments &&
106-
node.leadingComments.some(c => {
107-
return c.type === 'CommentBlock' && /@internal\b/.test(c.value)
108-
})
109-
) {
110-
/** @type {any} */
111-
const n = node
112-
let id
113-
if (n.id && n.id.type === 'Identifier') {
114-
id = n.id.name
115-
} else if (n.key && n.key.type === 'Identifier') {
116-
id = n.key.name
117-
}
118-
if (id) {
119-
s.overwrite(
120-
// @ts-ignore
121-
node.leadingComments[0].start,
122-
node.end,
123-
`/* removed internal: ${id} */`
124-
)
125-
} else {
126-
// @ts-ignore
127-
s.remove(node.leadingComments[0].start, node.end)
128-
}
129-
return true
130-
}
131-
return false
132-
}
133-
13480
const isExported = new Set()
13581
const shouldRemoveExport = new Set()
13682

@@ -146,7 +92,7 @@ function patchTypes(pkg) {
14692
}
14793
}
14894

149-
// pass 1: remove internals + add exports
95+
// pass 1: add exports
15096
for (const node of ast.program.body) {
15197
if (node.type === 'VariableDeclaration') {
15298
processDeclaration(node.declarations[0], node)
@@ -167,10 +113,6 @@ function patchTypes(pkg) {
167113
node.type === 'ClassDeclaration'
168114
) {
169115
processDeclaration(node)
170-
} else if (removeInternal(node)) {
171-
throw new Error(
172-
`unhandled export type marked as @internal: ${node.type}`
173-
)
174116
}
175117
}
176118

@@ -213,12 +155,6 @@ function patchTypes(pkg) {
213155
}
214156
code = s.toString()
215157

216-
if (/@internal/.test(code)) {
217-
throw new Error(
218-
`unhandled @internal declarations detected in ${chunk.fileName}.`
219-
)
220-
}
221-
222158
// append pkg specific types
223159
const additionalTypeDir = `packages/${pkg}/types`
224160
if (existsSync(additionalTypeDir)) {

tsconfig.build.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
44
"declaration": true,
5-
"emitDeclarationOnly": true
5+
"emitDeclarationOnly": true,
6+
"stripInternal": true
67
},
78
"exclude": [
89
"packages/*/__tests__",

0 commit comments

Comments
 (0)