copyNativeRes.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import path from 'node:path'
  2. import process from 'node:process'
  3. import fs from 'fs-extra'
  4. export function copyNativeRes() {
  5. const waitPath = path.resolve(__dirname, '../src/nativeResources')
  6. const buildPath = path.resolve(
  7. __dirname,
  8. '../dist',
  9. process.env.NODE_ENV === 'production' ? 'build' : 'dev',
  10. process.env.UNI_PLATFORM!,
  11. 'nativeResources',
  12. )
  13. return {
  14. enforce: 'post',
  15. async writeBundle() {
  16. try {
  17. // 检查源目录是否存在
  18. const sourceExists = await fs.pathExists(waitPath)
  19. if (!sourceExists) {
  20. console.warn(`[copyNativeRes] 警告:源目录 "${waitPath}" 不存在,跳过复制操作。`)
  21. return
  22. }
  23. // 确保目标目录及中间目录存在
  24. await fs.ensureDir(buildPath)
  25. console.log(`[copyNativeRes] 确保目标目录存在:${buildPath}`)
  26. // 执行文件夹复制
  27. await fs.copy(waitPath, buildPath)
  28. console.log(
  29. `[copyNativeRes] 成功将 nativeResources 目录中的资源移动到构建目录:${buildPath}`,
  30. )
  31. }
  32. catch (error) {
  33. console.error(`[copyNativeRes] 复制资源失败:`, error)
  34. }
  35. },
  36. }
  37. }