rollup.dts.config.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // @ts-check
  2. import { existsSync, readdirSync, readFileSync } from 'fs'
  3. import dts from 'rollup-plugin-dts'
  4. if (!existsSync('temp/packages')) {
  5. console.warn(
  6. 'no temp dts files found. run `tsc -p tsconfig.build.json` first.'
  7. )
  8. process.exit(1)
  9. }
  10. export default readdirSync('temp/packages').map(pkg => {
  11. return {
  12. input: `./temp/packages/${pkg}/src/index.d.ts`,
  13. output: {
  14. file: `packages/${pkg}/dist/${pkg}.d.ts`,
  15. format: 'es'
  16. },
  17. plugins: [dts(), patchTypes(pkg)],
  18. onwarn(warning, warn) {
  19. // during dts rollup, everything is externalized by default
  20. if (
  21. warning.code === 'UNRESOLVED_IMPORT' &&
  22. !warning.exporter.startsWith('.')
  23. ) {
  24. return
  25. }
  26. warn(warning)
  27. }
  28. }
  29. })
  30. /**
  31. * @returns {import('rollup').Plugin}
  32. */
  33. function patchTypes(pkg) {
  34. return {
  35. name: 'patch-types',
  36. renderChunk(code) {
  37. // 1. TODO remove entries marked with @private
  38. // 2. append pkg specific types
  39. const additionalTypeDir = `packages/${pkg}/types`
  40. if (existsSync(additionalTypeDir)) {
  41. code +=
  42. '\n' +
  43. readdirSync(additionalTypeDir)
  44. .map(file => readFileSync(`${additionalTypeDir}/${file}`, 'utf-8'))
  45. .join('\n')
  46. }
  47. return code
  48. }
  49. }
  50. }