setup-vue-office.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @vue-office/* 安装后须执行 lib/script/postinstall.js 生成 lib/index.js(Vue3 入口)。
  3. * pnpm 默认可能跳过依赖 lifecycle,导致 vite build 无法解析 @vue-office/pdf 等包。
  4. */
  5. import fs from 'node:fs'
  6. import path from 'node:path'
  7. import { spawnSync } from 'node:child_process'
  8. const root = process.cwd()
  9. const packages = ['pdf', 'docx', 'excel']
  10. let hadMissing = false
  11. for (const name of packages) {
  12. const pkgRoot = path.join(root, 'node_modules/@vue-office', name)
  13. const indexJs = path.join(pkgRoot, 'lib/index.js')
  14. const postinstall = path.join(pkgRoot, 'lib/script/postinstall.js')
  15. if (!fs.existsSync(postinstall)) {
  16. console.warn(`[setup-vue-office] 跳过 @vue-office/${name}:未找到 postinstall 脚本`)
  17. continue
  18. }
  19. if (!fs.existsSync(indexJs)) {
  20. hadMissing = true
  21. const result = spawnSync(process.execPath, [postinstall], {
  22. cwd: pkgRoot,
  23. stdio: 'inherit'
  24. })
  25. if (result.status !== 0) {
  26. console.error(`[setup-vue-office] @vue-office/${name} postinstall 失败`)
  27. process.exit(result.status ?? 1)
  28. }
  29. }
  30. if (!fs.existsSync(indexJs)) {
  31. console.error(`[setup-vue-office] @vue-office/${name} 仍缺少 lib/index.js`)
  32. process.exit(1)
  33. }
  34. }
  35. if (hadMissing) {
  36. console.log('[setup-vue-office] 已生成 @vue-office/* 的 lib/index.js(Vue3 入口)')
  37. } else {
  38. console.log('[setup-vue-office] @vue-office/* 入口文件已就绪')
  39. }