rollup.dts.config.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // @ts-check
  2. import { parse } from '@babel/parser'
  3. import { existsSync, readdirSync, readFileSync } from 'fs'
  4. import MagicString from 'magic-string'
  5. import dts from 'rollup-plugin-dts'
  6. import { walk } from 'estree-walker'
  7. if (!existsSync('temp/packages')) {
  8. console.warn(
  9. 'no temp dts files found. run `tsc -p tsconfig.build.json` first.'
  10. )
  11. process.exit(1)
  12. }
  13. export default readdirSync('temp/packages').map(pkg => {
  14. return {
  15. input: `./temp/packages/${pkg}/src/index.d.ts`,
  16. output: {
  17. file: `packages/${pkg}/dist/${pkg}.d.ts`,
  18. format: 'es'
  19. },
  20. plugins: [dts(), patchTypes(pkg)],
  21. onwarn(warning, warn) {
  22. // during dts rollup, everything is externalized by default
  23. if (
  24. warning.code === 'UNRESOLVED_IMPORT' &&
  25. !warning.exporter.startsWith('.')
  26. ) {
  27. return
  28. }
  29. warn(warning)
  30. }
  31. }
  32. })
  33. /**
  34. * Patch the dts generated by rollup-plugin-dts
  35. * 1. remove exports marked as @internal
  36. * 2. Convert all types to inline exports
  37. * and remove them from the big export {} declaration
  38. * otherwise it gets weird in vitepress `defineComponent` call with
  39. * "the inferred type cannot be named without a reference"
  40. * 3. Append custom agumentations (jsx, macros)
  41. * @returns {import('rollup').Plugin}
  42. */
  43. function patchTypes(pkg) {
  44. return {
  45. name: 'patch-types',
  46. renderChunk(code) {
  47. const s = new MagicString(code)
  48. const ast = parse(code, {
  49. plugins: ['typescript'],
  50. sourceType: 'module'
  51. })
  52. /**
  53. * @param {import('@babel/types').Node} node
  54. * @returns {boolean}
  55. */
  56. function removeInternal(node) {
  57. if (
  58. node.leadingComments &&
  59. node.leadingComments.some(c => {
  60. return c.type === 'CommentBlock' && /@internal\b/.test(c.value)
  61. })
  62. ) {
  63. /** @type {any} */
  64. const n = node
  65. let id
  66. if (n.id && n.id.type === 'Identifier') {
  67. id = n.id.name
  68. } else if (n.key && n.key.type === 'Identifier') {
  69. id = n.key.name
  70. }
  71. if (id) {
  72. s.overwrite(
  73. // @ts-ignore
  74. node.leadingComments[0].start,
  75. node.end,
  76. `/* removed internal: ${id} */`
  77. )
  78. } else {
  79. // @ts-ignore
  80. s.remove(node.leadingComments[0].start, node.end)
  81. }
  82. return true
  83. }
  84. return false
  85. }
  86. const shouldRemoveExport = new Set()
  87. // pass 1: remove internals + add exports
  88. for (const node of ast.program.body) {
  89. if (
  90. (node.type === 'TSTypeAliasDeclaration' ||
  91. node.type === 'TSInterfaceDeclaration') &&
  92. !node.id.name.startsWith(`_`)
  93. ) {
  94. shouldRemoveExport.add(node.id.name)
  95. if (!removeInternal(node)) {
  96. // @ts-ignore
  97. s.prependLeft(node.start, `export `)
  98. // traverse further for internal properties
  99. if (node.type === 'TSInterfaceDeclaration') {
  100. node.body.body.forEach(removeInternal)
  101. } else if (node.type === 'TSTypeAliasDeclaration') {
  102. // @ts-ignore
  103. walk(node.typeAnnotation, {
  104. enter(node) {
  105. // @ts-ignore
  106. if (removeInternal(node)) this.skip()
  107. }
  108. })
  109. }
  110. }
  111. } else if (removeInternal(node)) {
  112. if (node.type === 'VariableDeclaration') {
  113. // declare const x
  114. for (const decl of node.declarations) {
  115. // @ts-ignore
  116. shouldRemoveExport.add(decl.id.name)
  117. }
  118. } else if (
  119. node.type === 'TSDeclareFunction' ||
  120. node.type === 'TSEnumDeclaration'
  121. ) {
  122. // declare function
  123. // @ts-ignore
  124. shouldRemoveExport.add(node.id.name)
  125. } else {
  126. throw new Error(
  127. `unhandled export type marked as @internal: ${node.type}`
  128. )
  129. }
  130. }
  131. }
  132. // pass 2: remove exports
  133. for (const node of ast.program.body) {
  134. if (node.type === 'ExportNamedDeclaration' && !node.source) {
  135. for (let i = 0; i < node.specifiers.length; i++) {
  136. const spec = node.specifiers[i]
  137. if (
  138. spec.type === 'ExportSpecifier' &&
  139. shouldRemoveExport.has(spec.local.name)
  140. ) {
  141. const next = node.specifiers[i + 1]
  142. if (next) {
  143. // @ts-ignore
  144. s.remove(spec.start, next.start)
  145. } else {
  146. // last one
  147. const prev = node.specifiers[i - 1]
  148. // @ts-ignore
  149. s.remove(prev ? prev.end : spec.start, spec.end)
  150. }
  151. }
  152. }
  153. }
  154. }
  155. code = s.toString()
  156. // append pkg specific types
  157. const additionalTypeDir = `packages/${pkg}/types`
  158. if (existsSync(additionalTypeDir)) {
  159. code +=
  160. '\n' +
  161. readdirSync(additionalTypeDir)
  162. .map(file => readFileSync(`${additionalTypeDir}/${file}`, 'utf-8'))
  163. .join('\n')
  164. }
  165. return code
  166. }
  167. }
  168. }