pkg-dts.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const path = require('path')
  2. const bundleDts = require('@arshaw/dts-bundle').bundle
  3. const { pkgStructs } = require('./pkg-struct')
  4. exports.bundlPkgDefs = bundlPkgDefs
  5. /*
  6. Assumes granular .d.ts files have already been generated
  7. */
  8. function bundlPkgDefs() {
  9. for (let pkgStruct of pkgStructs) {
  10. if (!pkgStruct.isBundle) { // temporary?
  11. bundlePkgDef(pkgStruct)
  12. }
  13. }
  14. return Promise.resolve() // need to return a promise even tho bundlePkgDef is synchronous
  15. }
  16. function bundlePkgDef(pkgStruct) {
  17. bundleDts({ // synchronous
  18. name: pkgStruct.name,
  19. main: path.join('tmp/tsc-output', pkgStruct.srcDir, 'main.d.ts'),
  20. transformModuleBody: transformDefaultClassExports,
  21. out: path.join(process.cwd(), pkgStruct.distDir, 'main.d.ts') // needs to be absolute, or becomes rel to entry
  22. })
  23. }
  24. // changes the name of the default export to `Default` and exports it as a *named* export.
  25. // this allows ambient declaration merging to grab onto it.
  26. // workaround for https://github.com/Microsoft/TypeScript/issues/14080
  27. function transformDefaultClassExports(moduleBody) {
  28. // HACK
  29. // remove imports to style files.
  30. // has nothing to do with default class exports.
  31. moduleBody = moduleBody.replace(/import ['"][^'"]*\.s?css['"];/, '');
  32. return moduleBody.replace(/^(\s*)export default (abstract )?class ([\w]+)/mg, function(m0, m1, m2, m3) {
  33. return m1 + 'export { ' + m3 + ' as default, ' + m3 + ' };\n' +
  34. m1 + (m2 || '') + 'class ' + m3
  35. })
  36. }