Jacob
2026-10-12 4a4bc757602a4554b7fc0d38f9402663ddca9192
scripts/set-env.js
....@@ -6,9 +6,12 @@
66 * npm run env:prod 等价于 node scripts/set-env.js prod
77 *
88 * 说明:
9
- * appid 是编译期值(manifest.json 为 HBuilderX 固定输入、微信开发者工具只认
10
- * project.config.json 的 appid),无法像接口地址那样运行时切换,所以这里做
11
- * “一次命令写回”。改完必须重新编译一次,dist 下的 project.config.json 才会更新。
9
+ * appid 是编译期值(HBuilderX 的编译输入就是 manifest.json),无法像接口地址那样
10
+ * 运行时切换,所以这里做“一次命令写回”。改完必须重新编译一次,产物里的
11
+ * project.config.json 才会更新。
12
+ *
13
+ * 日常推荐:mp-weixin.appid 保持 null,只让发行脚本回写产物 appid(git 零噪音)。
14
+ * 本脚本用于需要在 HBuilderX 里直接运行调试、或要出 App 包(顶层 appid)的场景。
1215 *
1316 * 唯一数据源:config/env/<env>.js 中的 UNI_APPID(DCloud 标识)、APPID(小程序 appid)
1417 */
....@@ -18,9 +21,9 @@
1821 const ROOT = path.resolve(__dirname, '..');
1922 const ENV_DIR = path.join(ROOT, 'config', 'env');
2023
24
+// 根目录 project.config.json 不参与 mp-weixin 编译且已不入库,这里只维护 manifest.json
2125 const files = {
22
- manifest: path.join(ROOT, 'manifest.json'),
23
- projectConfig: path.join(ROOT, 'project.config.json')
26
+ manifest: path.join(ROOT, 'manifest.json')
2427 };
2528
2629 function fail(msg) {
....@@ -28,10 +31,10 @@
2831 process.exit(1);
2932 }
3033
31
-/** 只替换第一处 "appid" : "xxx",保留原有空格/缩进风格 */
34
+/** 只替换第一处 "appid" : <字符串或 null>,保留原有空格/缩进风格 */
3235 function replaceAppId(text, value) {
3336 let done = false;
34
- const result = text.replace(/"appid"(\s*:\s*)"[^"]*"/, function(match, sep) {
37
+ const result = text.replace(/"appid"(\s*:\s*)(?:"[^"]*"|null)/, function(match, sep) {
3538 done = true;
3639 return '"appid"' + sep + '"' + value + '"';
3740 });
....@@ -48,18 +51,17 @@
4851 // 2) mp-weixin 下的 appid(微信小程序 appid),只在该区块内替换第一处
4952 const idx = text.indexOf('"mp-weixin"');
5053 if (idx < 0) fail('manifest.json 中未找到 "mp-weixin" 配置块');
51
- const scoped = replaceAppId(text.slice(idx), env.APPID);
52
- if (!scoped.done) fail('manifest.json 的 "mp-weixin" 中未找到 "appid" 字段');
53
- text = text.slice(0, idx) + scoped.text;
54
+ const head = text.slice(0, idx);
55
+ const body = text.slice(idx);
56
+ const scoped = replaceAppId(body, env.APPID);
57
+ if (scoped.done) return head + scoped.text;
5458
55
- return text;
56
-}
57
-
58
-/** 替换 project.config.json 的 appid(保持原格式,避免整文件重排) */
59
-function patchProjectConfig(text, env) {
60
- const res = replaceAppId(text, env.APPID);
61
- if (!res.done) fail('project.config.json 中未找到 "appid" 字段');
62
- return res.text;
59
+ // 3) 字段被删掉时,按同级子项缩进补一行
60
+ const m = body.match(/^("mp-weixin"\s*:\s*\{)(\r?\n)([ \t]*)/);
61
+ if (!m) fail('manifest.json 的 "mp-weixin" 块格式异常,无法写入 appid');
62
+ const nl = m[2];
63
+ const indent = m[3];
64
+ return head + m[1] + nl + indent + '"appid" : "' + env.APPID + '",' + nl + indent + body.slice(m[0].length);
6365 }
6466
6567 function writeIfChanged(file, next) {
....@@ -96,9 +98,7 @@
9698 console.log(' 小程序 appid : ' + env.APPID);
9799 console.log(' 接口地址 : ' + env.HTTP_REQUEST_URL);
98100
99
- const manifestChanged = writeIfChanged(files.manifest, patchManifest(fs.readFileSync(files.manifest, 'utf8'), env));
100
- const projectChanged = writeIfChanged(files.projectConfig, patchProjectConfig(fs.readFileSync(files.projectConfig, 'utf8'), env));
101
- const changed = manifestChanged || projectChanged;
101
+ const changed = writeIfChanged(files.manifest, patchManifest(fs.readFileSync(files.manifest, 'utf8'), env));
102102
103103 console.log(changed
104104 ? '[set-env] 完成,请在 HBuilderX 中重新编译一次,并在微信开发者工具中重新打开项目'