瀏覽代碼

!23 fix: 解决在App环境打包时,manifest.config.ts中配置需要启用的模块不生效的问题。比如微信支付、支付宝支付在manif…
Merge pull request !23 from mengxiaofei/main-manifest-enhance

菲鸽 7 月之前
父節點
當前提交
a6ca8aced3
共有 2 個文件被更改,包括 66 次插入0 次删除
  1. 64 0
      vite-plugins/sync-manifest-plugins.ts
  2. 2 0
      vite.config.ts

+ 64 - 0
vite-plugins/sync-manifest-plugins.ts

@@ -0,0 +1,64 @@
+import type { Plugin } from 'vite'
+import fs from 'fs'
+import path from 'path'
+
+interface ManifestType {
+  plus?: {
+    distribute?: {
+      plugins?: Record<string, any>
+    }
+  }
+  'app-plus'?: {
+    distribute?: {
+      plugins?: Record<string, any>
+    }
+  }
+}
+
+export default function syncManifestPlugin(): Plugin {
+  return {
+    name: 'sync-manifest',
+    apply: 'build',
+    enforce: 'post',
+    writeBundle: {
+      order: 'post',
+      handler() {
+        const srcManifestPath = path.resolve(process.cwd(), './src/manifest.json')
+        const distAppPath = path.resolve(process.cwd(), './dist/dev/app/manifest.json')
+
+        try {
+          // 读取源文件
+          const srcManifest = JSON.parse(fs.readFileSync(srcManifestPath, 'utf8')) as ManifestType
+
+          // 确保目标目录存在
+          const distAppDir = path.dirname(distAppPath)
+          if (!fs.existsSync(distAppDir)) {
+            fs.mkdirSync(distAppDir, { recursive: true })
+          }
+
+          // 读取目标文件(如果存在)
+          let distManifest: ManifestType = {}
+          if (fs.existsSync(distAppPath)) {
+            distManifest = JSON.parse(fs.readFileSync(distAppPath, 'utf8'))
+          }
+
+          // 如果源文件存在 plugins
+          if (srcManifest['app-plus']?.distribute?.plugins) {
+            // 确保目标文件中有必要的对象结构
+            if (!distManifest.plus) distManifest.plus = {}
+            if (!distManifest.plus.distribute) distManifest.plus.distribute = {}
+
+            // 复制 plugins 内容
+            distManifest.plus.distribute.plugins = srcManifest['app-plus'].distribute.plugins
+
+            // 写入更新后的内容
+            fs.writeFileSync(distAppPath, JSON.stringify(distManifest, null, 2))
+            console.log('✅ Manifest plugins 同步成功')
+          }
+        } catch (error) {
+          console.error('❌ 同步 manifest plugins 失败:', error)
+        }
+      },
+    },
+  }
+}

+ 2 - 0
vite.config.ts

@@ -25,6 +25,7 @@ import AutoImport from 'unplugin-auto-import/vite'
 import { defineConfig, loadEnv } from 'vite'
 import ViteRestart from 'vite-plugin-restart'
 import openDevTools from './scripts/open-dev-tools'
+import syncManifestPlugin from './vite-plugins/sync-manifest-plugins'
 
 // https://vitejs.dev/config/
 export default defineConfig(({ command, mode }) => {
@@ -125,6 +126,7 @@ export default defineConfig(({ command, mode }) => {
       }),
       // 只有在 app 平台时才启用 copyNativeRes 插件
       // UNI_PLATFORM === 'app' && copyNativeRes(),
+      syncManifestPlugin(),
       Components({
         extensions: ['vue'],
         deep: true, // 是否递归扫描子目录,