after-build.cjs 1.3 KB

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