You've already forked template-MP
IFLOW描述文件更新;
优化 编译配置增加动态修改manifest.json文件;
This commit is contained in:
@@ -1,9 +1,47 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import uni from '@dcloudio/vite-plugin-uni'
|
||||
import { configDotenv } from 'dotenv'
|
||||
import { resolve } from 'path'
|
||||
import { readFileSync, writeFileSync } from 'fs'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
// 自定义插件:替换 manifest.json 中的 appid
|
||||
function replaceManifestAppid() {
|
||||
return {
|
||||
name: 'replace-manifest-appid',
|
||||
buildStart() {
|
||||
// 获取环境变量,明确指定路径为项目根目录
|
||||
dotenv.config({ path: resolve(__dirname, '.env') })
|
||||
const appid = process.env.VITE_APPID
|
||||
const uni_appId = process.env.VITE_UNI_APPID
|
||||
|
||||
if (appid && uni_appId) {
|
||||
// 读取 manifest.json 文件
|
||||
const manifestPath = resolve(__dirname, 'manifest.json')
|
||||
let manifestContent = readFileSync(manifestPath, 'utf-8')
|
||||
|
||||
// 解析 JSON
|
||||
const manifest = JSON.parse(manifestContent)
|
||||
|
||||
// 替换
|
||||
manifest.appid = uni_appId
|
||||
if (manifest['mp-weixin']) {
|
||||
manifest['mp-weixin'].appid = appid
|
||||
}
|
||||
|
||||
// 写回文件
|
||||
writeFileSync(manifestPath, JSON.stringify(manifest, null, 4))
|
||||
|
||||
console.log(`Manifest appid 已更新为: ${uni_appId}`)
|
||||
console.log(`Manifest mp-weixin appid 已更新为: ${appid}`)
|
||||
} else {
|
||||
console.warn('未找到 VITE_APPID 和 VITE_UNI_APPID 环境变量,使用默认值')
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [uni(), configDotenv()],
|
||||
plugins: [replaceManifestAppid(), uni()],
|
||||
build: {
|
||||
minify: 'terser',
|
||||
terserOptions: {
|
||||
|
||||
Reference in New Issue
Block a user