ThomasLambert
ThomasLambert
  • 发布:2022-09-19 14:06
  • 更新:2025-07-01 14:28
  • 阅读:302

ext.json差异化编译问题

分类:HBuilderX

新版本做了支付宝小程序、百度小程序、快手小程序、字节小程序平台 优化 支持自动拷贝 ext.json 文件,但是没有进行差异化处理,现在编译完了还要根据各平台自己手动设置一下,建议增加ext.json的差异化编译,

2022-09-19 14:06 负责人:无 分享
已邀请:
ThomasLambert

ThomasLambert (作者)

参考三年后楼上的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":{  

        }  
    }  
        ...其他平台依次处理  
}
respr

respr

顶上去,马上都一年了,还不解决

chinahappybeer

chinahappybeer - china happy beer

顶上去,马上都三年了,还不解决

DCloud_UNI_JBB

DCloud_UNI_JBB

感谢反馈,后续会讨论下是否支持条件编译

可以参考下面的代码临时解决问题

// vite.config.js  
import {      
    defineConfig      
} from 'vite';      
import uni from '@dcloudio/vite-plugin-uni';      

const platform = process.env.UNI_PLATFORM      
function extJsonPlugin() {      
    return {      
        name: 'uni:mp-ext-json',      
        enforce: 'pre',      
        buildStart() {      
            const data = platform === 'mp-weixin' ? {      
                message: "This is fixed JSON data generated early",      
                version: "1.0.0",      
            } : {      
                message: "This is fixed JSON data generated early",      
                version: "2.0.0",      
            }      
            this.emitFile({      
                type: 'asset',      
                fileName: 'ext.json',      
                source: JSON.stringify(data, null, 2)      
            })      
        }      
    }      
}      

export default defineConfig({      
    plugins: [extJsonPlugin(), uni()],      
});

要回复问题请先登录注册