copyNativeRes.ts 1.1 KB

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