after-build.cjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // append our api modules to `exports` in `package.json` then write it to `./dist`
  7. const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
  8. const modules = readdirSync('src')
  9. .filter((e) => e !== 'helpers')
  10. .map((mod) => mod.replace('.ts', ''))
  11. const outputPkg = {
  12. ...pkg,
  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}`))
  40. // copy typescript src files to `./dist`
  41. copySync('src', 'dist')