You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

2 months ago
  1. const fs = require('fs')
  2. const path = require('path')
  3. const dir = path.resolve(__dirname, '..', 'lib')
  4. function loadModule(name) {
  5. try {
  6. return require(name)
  7. } catch (e) {
  8. return undefined
  9. }
  10. }
  11. function copy(name, version, vue) {
  12. vue = vue || 'vue'
  13. const src = path.join(dir, `v${version}`, name)
  14. const dest = path.join(dir, name)
  15. let content = fs.readFileSync(src, 'utf-8')
  16. content = content.replace(/'vue'/g, `'${vue}'`)
  17. // unlink for pnpm, #92
  18. try {
  19. fs.unlinkSync(dest)
  20. } catch (error) { }
  21. fs.writeFileSync(dest, content, 'utf-8')
  22. }
  23. function updateVue2API() {
  24. const ignoreList = ['version', 'default']
  25. const VCA = loadModule('@vue/composition-api')
  26. if (!VCA) {
  27. console.warn('[vue-demi] Composition API plugin is not found. Please run "npm install @vue/composition-api" to install.')
  28. return
  29. }
  30. const exports = Object.keys(VCA).filter(i => !ignoreList.includes(i))
  31. const esmPath = path.join(dir, 'index.mjs')
  32. let content = fs.readFileSync(esmPath, 'utf-8')
  33. content = content.replace(
  34. /\/\*\*VCA-EXPORTS\*\*\/[\s\S]+\/\*\*VCA-EXPORTS\*\*\//m,
  35. `/**VCA-EXPORTS**/
  36. export { ${exports.join(', ')} } from '@vue/composition-api/dist/vue-composition-api.mjs'
  37. /**VCA-EXPORTS**/`
  38. )
  39. fs.writeFileSync(esmPath, content, 'utf-8')
  40. }
  41. function switchVersion(version, vue) {
  42. copy('index.cjs', version, vue)
  43. copy('index.mjs', version, vue)
  44. copy('index.d.ts', version, vue)
  45. if (version === 2)
  46. updateVue2API()
  47. }
  48. module.exports.loadModule = loadModule
  49. module.exports.switchVersion = switchVersion