rollup-dts.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const dts = require('rollup-plugin-dts').default
  2. const { isScssPath, isRelPath } = require('./rollup-util')
  3. const { pkgStructs } = require('./pkg-struct')
  4. const { mapHashViaPair } = require('./util')
  5. module.exports = function() {
  6. return {
  7. input: mapHashViaPair(pkgStructs, (pkgStruct) => [
  8. pkgStruct.distDir, // the key. the [name] in entryFileNames
  9. './' + pkgStruct.tscMain + '.d.ts' // the value
  10. ]),
  11. output: {
  12. format: 'es',
  13. dir: '.',
  14. entryFileNames: '[name]/main.d.ts'
  15. },
  16. plugins: [
  17. dts(),
  18. {
  19. resolveId(id, source) {
  20. if (isScssPath(id)) {
  21. return false
  22. }
  23. if (!isRelPath(id)) {
  24. return { id, external: true }
  25. }
  26. return null
  27. },
  28. renderChunk(code, chunk) {
  29. if (chunk.fileName === 'packages/core/dist/main.d.ts') {
  30. return fixCode(code)
  31. }
  32. return code
  33. }
  34. }
  35. ],
  36. // // uncomment to see all circular dependency warnings
  37. // onwarn(warning, warn) {
  38. // console.log('WARNING', warning)
  39. // warn(warning)
  40. // }
  41. }
  42. }
  43. // for a problem like this: https://github.com/Swatinem/rollup-plugin-dts/issues/39
  44. function fixCode(code) {
  45. let replacements = {}
  46. code = code.replace(/import \{(.*?)\} from '@fullcalendar\/core';?/, function(m0, m1) {
  47. let re = /(\w+) as (\w+\$\d+)/g
  48. let match
  49. while ((match = re.exec(m1))) {
  50. replacements[match[2]] = match[1]
  51. }
  52. return ''
  53. })
  54. for (let find in replacements) {
  55. let replacement = replacements[find]
  56. find = find.replace('$', '\\$') // escape for regexp
  57. code = code.replace(new RegExp(find, 'g'), replacement)
  58. }
  59. return code
  60. }