after-build.cjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. const { readFileSync, readdirSync, writeFileSync, copyFileSync } = require('fs')
  5. const { copySync } = require('fs-extra')
  6. /**
  7. * append our api modules to `exports` in `package.json` then write it to `./dist`
  8. */
  9. const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
  10. const modules = readdirSync('src').map((mod) => mod.replace('.ts', ''))
  11. if (!pkg.exports) {
  12. pkg.exports = {}
  13. }
  14. const outputPkg = Object.assign(
  15. pkg.exports,
  16. ...modules.map((mod) => {
  17. let temp = {}
  18. let key = `./${mod}`
  19. if (mod === 'index') {
  20. key = '.'
  21. }
  22. temp[key] = {
  23. import: `./${mod}.js`,
  24. require: `./${mod}.cjs`
  25. }
  26. return temp
  27. }),
  28. // if for some reason in the future we manually add something in the `exports` field
  29. // this will ensure it doesn't get overwritten by the logic above
  30. { ...pkg.exports }
  31. )
  32. writeFileSync('dist/package.json', JSON.stringify(outputPkg, undefined, 2))
  33. /**
  34. * copy necessary files like `CHANGELOG.md` and Licenses to `./dist`
  35. */
  36. const files = readdirSync('.').filter(
  37. (f) => f === 'CHANGELOG.md' || f.startsWith('LICENSE')
  38. )
  39. files.forEach((f) => copyFileSync(f, `dist/${f}`))
  40. /**
  41. * copy typescript src files to `./dist`
  42. */
  43. copySync('src', 'dist')