after-build.cjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. // append our api modules to `exports` in `package.json` then write it to `./dist`
  6. const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
  7. const modules = readdirSync('src')
  8. .filter((e) => e !== 'helpers')
  9. .map((mod) => mod.replace('.ts', ''))
  10. const outputPkg = {
  11. ...pkg,
  12. exports: Object.assign(
  13. {},
  14. ...modules.map((mod) => {
  15. let temp = {}
  16. let key = `./${mod}`
  17. if (mod === 'index') {
  18. key = '.'
  19. }
  20. temp[key] = {
  21. import: `./${mod}.js`,
  22. require: `./${mod}.cjs`
  23. }
  24. return temp
  25. }),
  26. // if for some reason in the future we manually add something in the `exports` field
  27. // this will ensure it doesn't get overwritten by the logic above
  28. { ...(pkg.exports || {}) }
  29. )
  30. }
  31. writeFileSync('dist/package.json', JSON.stringify(outputPkg, undefined, 2))
  32. // copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
  33. const dir = readdirSync('.')
  34. const files = [
  35. ...dir.filter((f) => f.startsWith('LICENSE')),
  36. ...dir.filter((f) => f.endsWith('.md'))
  37. ]
  38. files.forEach((f) => copyFileSync(f, `dist/${f}`))