fix-theme:
// 修复 uni-app 编译后的 theme.json 文件
// 解决 Unicode 编码和 BOM 问题
const fs = require('fs');
const path = require('path');
// 主题配置(直接在脚本中定义,避免编码问题)
const themeConfig = {
"light": {
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8",
"backgroundTextStyle": "light",
"tabBarColor": "#666666",
"tabBarSelectedColor": "#2a82e4",
"tabBarBackgroundColor": "#ffffff",
"tabBarBorderStyle": "black"
},
"dark": {
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#1a1a1a",
"backgroundColor": "#121212",
"backgroundTextStyle": "dark",
"tabBarColor": "#888888",
"tabBarSelectedColor": "#64b5f6",
"tabBarBackgroundColor": "#1a1a1a",
"tabBarBorderStyle": "white"
}
};
// 目标文件路径
const targetTheme = path.join(__dirname, '../unpackage/dist/dev/mp-weixin/theme.json');
try {
// 检查目标目录是否存在
const targetDir = path.dirname(targetTheme);
if (!fs.existsSync(targetDir)) {
console.error('❌ 目标目录不存在,请先编译项目');
process.exit(1);
}
// 将配置转换为 JSON 字符串(无 BOM,UTF-8)
const themeContent = JSON.stringify(themeConfig, null, 2);
// 写入目标文件(确保无 BOM)
fs.writeFileSync(targetTheme, themeContent, { encoding: 'utf8', flag: 'w' });
console.log('✅ theme.json 已成功修复');
console.log('? 目标文件:', targetTheme);
console.log('? 内容预览:', themeContent.substring(0, 100) + '...');
} catch (error) {
console.error('❌ 修复失败:', error.message);
process.exit(1);
}
2 个回复
最佳回复
DCloud_UNI_JBB
经排查,问题产生的原因是 ide 未使用 utf-8 格式编辑 json 文件,导致了不符合预期的编码内容
hbuilderx 右下角提供了编码方式修改入口,改成 utf-8 即可
DCloud_UNI_JBB
theme.json 文件发一下
1***@qq.com (作者)
{
"light": {
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8",
"backgroundTextStyle": "light",
"tabBarColor": "#666666",
"tabBarSelectedColor": "#2a82e4",
"tabBarBackgroundColor": "#ffffff",
"tabBarBorderStyle": "black"
},
"dark": {
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#1a1a1a",
"backgroundColor": "#121212",
"backgroundTextStyle": "dark",
"tabBarColor": "#888888",
"tabBarSelectedColor": "#64b5f6",
"tabBarBackgroundColor": "#1a1a1a",
"tabBarBorderStyle": "white"
}
}
2025-11-23 16:43
DCloud_UNI_JBB
theme.json也不会进行转化吧,你是什么设备测试的?什么时候遇到的这个问题?
2025-11-23 18:08
1***@qq.com (作者)
回复 DCloud_UNI_JBB: 如果需要可以远程查看我电脑相关配置以及运行日志
2025-11-23 19:08
1***@qq.com (作者)
回复 DCloud_UNI_JBB: 我是使用的win10系统,就是上面的相关版本,我是给微信小程序适配的深色模式,然后在运行和发布版本的时候都会有这个问题
2025-11-23 23:39
DCloud_UNI_JBB
回复 1***@qq.com: im里面私聊发我一个产物和报错截图
2025-11-24 10:04
1***@qq.com (作者)
回复 DCloud_UNI_JBB: OK
2025-11-24 14:37