after-build.cjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 = {
  15. ...pkg,
  16. exports: Object.assign(
  17. {},
  18. ...modules.map((mod) => {
  19. let temp = {}
  20. let key = `./${mod}`
  21. if (mod === 'index') {
  22. key = '.'
  23. }
  24. temp[key] = {
  25. import: `./${mod}.js`,
  26. require: `./${mod}.cjs`
  27. }
  28. return temp
  29. }),
  30. // if for some reason in the future we manually add something in the `exports` field
  31. // this will ensure it doesn't get overwritten by the logic above
  32. { ...pkg.exports }
  33. )
  34. }
  35. writeFileSync('dist/package.json', JSON.stringify(outputPkg, undefined, 2))
  36. /**
  37. * copy necessary files like `CHANGELOG.md` and Licenses to `./dist`
  38. */
  39. const files = readdirSync('.').filter(
  40. (f) => f.startsWith('LICENSE')
  41. )
  42. files.forEach((f) => copyFileSync(f, `dist/${f}`))
  43. /**
  44. * copy typescript src files to `./dist`
  45. */
  46. copySync('src', 'dist')