1***@qq.com
1***@qq.com
  • 发布:2025-11-23 16:36
  • 更新:2025-11-23 16:40
  • 阅读:25

【报Bug】HBuilderX 编译时会将 theme.json 转换为 Unicode 编码,导致微信开发者工具报错

分类:uni-app

产品分类: uniapp/小程序/微信

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: 22H2

HBuilderX类型: 正式

HBuilderX版本号: 4.85

第三方开发者工具版本号: 稳定版 Stable Build (1.06.2504060)

基础库版本号: 3.11.2

项目创建方式: HBuilderX

示例代码:

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);
}

操作步骤:

每一次运行打包都会出现

预期结果:

正常运行和发行使用

实际结果:

微信开发工具报错:

[] Cannot read properties of undefined (reading 'navigationBarTextStyle')
TypeError: Cannot read properties of undefined (reading 'navigationBarTextStyle')
at D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\js\common\miniprogram-builder\modules\corecompiler\original\json\theme.js:2:2949
at Array.forEach (<anonymous>)
at mergeThemeJSONToAppJSON (D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\js\common\miniprogram-builder\modules\corecompiler\original\json\theme.js:2:2757)
at checkAppJSON (D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\js\common\miniprogram-builder\modules\corecompiler\original\json\app\appJSON.js:2:1258)
at D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\js\common\miniprogram-builder\modules\corecompiler\original\json\app\appJSON.js:2:4242
at i.scheduler (D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\js\common\miniprogram-builder\modules\corecompiler\original\json\reactiveCache.js:2:1702)
at r (D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\node_modules.wxvpkg\@vue\reactivity\dist\reactivity.cjs.js:1:708)
at effect (D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\node_modules.wxvpkg\@vue\reactivity\dist\reactivity.cjs.js:1:403)
at ReactiveJSONCompiler.registerOrGet (D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\js\common\miniprogram-builder\modules\corecompiler\original\json\reactiveCache.js:2:1682)
at D:\Program Files (x86)\Tencent\微信web开发者工具\code\package.nw\js\common\miniprogram-builder\modules\corecompiler\original\json\reactiveCache.js:2:3413(env: Windows,mp,1.06.2504060; lib: 3.11.2)

bug描述:

错误原因
HBuilderX 编译时会将 theme.json 转换为 Unicode 编码,导致微信开发者工具在编译阶段就无法读取,所以报错。
具体流程:
HBuilderX 编译项目
将 theme.json 转换为 Unicode 编码(\u0000l\u0000i\u0000g\u0000h\u0000t)
微信开发者工具尝试读取 theme.json
发现是 Unicode 编码,无法解析
报错:Cannot read properties of undefined
解决方法
运行修复脚本:
npm run fix-theme

2025-11-23 16:36 负责人:无 分享
已邀请:
DCloud_UNI_JBB

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也不会进行转化吧,你是什么设备测试的?什么时候遇到的这个问题?

    1 分钟前

要回复问题请先登录注册