postbuild.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { readdir, readFile, writeFile } from 'node:fs/promises'
  2. /**
  3. * Adds `.js` when importing the ./src/index.js in the dts
  4. *
  5. * @return {Promise<void>}
  6. */
  7. async function patchRootDts() {
  8. const dts = 'dist/index.d.ts'
  9. const content = await readFile(dts, 'utf8')
  10. await writeFile(dts, content.replaceAll('./src/index\'', './src/index.js\''))
  11. }
  12. /**
  13. * Fix node16 issue: https://www.typescriptlang.org/tsconfig/#allowArbitraryExtensions
  14. * - node10 and bundler will check for d.vue.ts and vue.d.ts file when importing a vue file in a dts
  15. * - node16 will check only for d.vue.ts file, this function will just copy/paste the content of vue.d.ts to d.vue.ts
  16. *
  17. * @param path {String}
  18. * @return {Promise<void>}
  19. */
  20. async function patchVueDts(path) {
  21. const files = await readdir(path, { recursive: false })
  22. for (const file of files) {
  23. if (file.endsWith('.vue.d.ts')) {
  24. await writeFile(`${path}/${file.replace('.vue.d.ts', '.d.vue.ts')}`, await readFile(`${path}/${file}`, 'utf-8'), 'utf-8')
  25. }
  26. }
  27. }
  28. async function fixNode16() {
  29. await Promise.all([
  30. patchRootDts(),
  31. patchVueDts('dist/src/components'),
  32. patchVueDts('dist/src/composables/useLoader'),
  33. patchVueDts('dist/src/composables/useTexture'),
  34. ])
  35. }
  36. fixNode16()