参考三年后楼上的vite.config.js的方案,做了个vue.config.js的:
// vue.config.js
const path = require('path')
const fs = require('fs')
const platform = process.env.UNI_PLATFORM
// 自定义插件:生成 ext.json 文件
class ExtJsonPlugin {
apply(compiler) {
compiler.hooks.emit.tap('ExtJsonPlugin', (compilation) => {
const extJsonPath = path.resolve(__dirname, 'ext.json')
// 如果项目根目录存在ext.json
if (fs.existsSync(extJsonPath)) {
try {
let extJson = {}
// 读取内容
const data = JSON.parse(fs.readFileSync(extJsonPath, 'utf8'))
// 将key开头不为mp-的赋值给extJson
Object.keys(data).forEach(key => {
if (!key.startsWith('')) {
extJson[key] = data[key]
}
})
// 将ext.json中当前platform的合并到extJson
if (data[platform]) {
Object.assign(extJson, data[platform]);
}
const jsonContent = JSON.stringify(extJson, null, 2)
// 添加文件到输出
compilation.assets['ext.json'] = {
source: () => jsonContent,
size: () => jsonContent.length
}
} catch (e) {
console.error('Failed to parse ext.json:', e)
}
}
})
}
}
module.exports = {
// 配置configureWebpack
configureWebpack: {
plugins: [
new ExtJsonPlugin()
],
}
}
ext.json中做出如下修改,将不同平台的参数用对应的平台值分离:
{
"extEnable": true,
"directCommit": false,
"mp-weixin": {
"extAppid": "appid",
"ext": {
}
},
"mp-alipay": {
"ext": {
}
},
"mp-toutiao": {
"extAppid": "appid",
"ext":{
}
}
...其他平台依次处理
}
4 个回复
ThomasLambert (作者)
参考三年后楼上的vite.config.js的方案,做了个vue.config.js的:
ext.json中做出如下修改,将不同平台的参数用对应的平台值分离:
respr
顶上去,马上都一年了,还不解决
chinahappybeer - china happy beer
顶上去,马上都三年了,还不解决
DCloud_UNI_JBB
感谢反馈,后续会讨论下是否支持条件编译
可以参考下面的代码临时解决问题