13.2 构建配置与自动化
配置分三层:默认值、项目文件、命令行。合并顺序决定优先级,对象深合并、数组整体替换——数组是列表语义,逐项合并只会得到谁都不认识的东西。字符串支持 ${path} 变量展开,未定义变量原样保留,方便发现拼写错误。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 构建触发 | 本卷 13.1 | 13.1 消费配置;本节负责配置的产生与合并 |
| 资产管线 | 《资产管线》 | 卷二有导入选项;本节统一构建配置的层级 |
| 全卷脉络 | 本卷目录 | 本节承接构建章 |
从零实现:构建配置与自动化
深合并只对「两边都是普通对象」的键递归,其余情况直接用覆盖值;数组不做合并,整体替换。变量展开用正则匹配 ${name},只替换存在的变量,未知变量保持原样。解析入口按默认值、项目、命令行的顺序两次合并。
对标构建接口
Godot 命令行可按命名预设执行导出,预设定义于 export_presets.cfg。helio 按默认、项目和命令行三层合并,对象递归合并,数组整体替换。变量展开由独立函数完成。 Godot 导出项目
可运行产物:6 条用例
下面是 src/build-config.ts,层级 L1,完整可编译。运行命令 node --test test/13-2-build-config-automation.test.ts:
// file: src/build-config.ts
// tier: L1
// verify: node --test test/13-2-build-config-automation.test.ts
//
// 卷四(第十三章):构建配置与自动化。
// 配置分三层:默认值、项目文件、命令行。合并顺序决定优先级,对象深合并、
// 数组整体替换——数组是列表语义,逐项合并只会得到谁都不认识的东西。
// 字符串支持 ${path} 变量展开,未定义变量原样保留,方便发现拼写错误。
export type ConfigValue =
| string
| number
| boolean
| null
| ConfigValue[]
| { [key: string]: ConfigValue };
export type ConfigObject = { [key: string]: ConfigValue };
function isObject(value: ConfigValue): value is ConfigObject {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
export function deepMerge(base: ConfigObject, override: ConfigObject): ConfigObject {
const out: ConfigObject = { ...base };
for (const [key, value] of Object.entries(override)) {
const current = Object.hasOwn(out, key) ? out[key] : undefined;
const merged = isObject(current) && isObject(value) ? deepMerge(current, value) : value;
Object.defineProperty(out, key, { value: merged, enumerable: true, writable: true, configurable: true });
}
return out;
}
export function expandVars(template: string, vars: Record<string, string>): string {
return template.replace(/\$\{([^}]+)\}/g, (whole, name: string) =>
Object.prototype.hasOwnProperty.call(vars, name) ? vars[name] : whole,
);
}
export function resolveBuildConfig(
defaults: ConfigObject,
project: ConfigObject,
cli: ConfigObject,
): ConfigObject {
return deepMerge(deepMerge(defaults, project), cli);
}
数组整体替换是唯一不会产生歧义的选择:两个列表按索引合并,结果既不是 A 也不是 B,用户无法预测。变量保留未定义形式而不是替换成空串,让拼写错误在产物里显式暴露,而不是悄悄变成缺失路径。
下面是 test/13-2-build-config-automation.test.ts,层级 L1:
// file: test/13-2-build-config-automation.test.ts
// tier: L1
// verify: node --test test/13-2-build-config-automation.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { deepMerge, expandVars, resolveBuildConfig } from '../src/build-config.ts';
test('默认值在无覆盖时保留', () => {
const cfg = resolveBuildConfig({ target: 'web', minify: true }, {}, {});
assert.deepEqual(cfg, { target: 'web', minify: true });
});
test('项目配置覆盖默认值', () => {
const cfg = resolveBuildConfig({ target: 'web' }, { target: 'desktop' }, {});
assert.equal(cfg.target, 'desktop');
});
test('命令行覆盖优先级最高', () => {
const cfg = resolveBuildConfig({ target: 'web' }, { target: 'desktop' }, { target: 'mobile' });
assert.equal(cfg.target, 'mobile');
});
test('嵌套对象深合并', () => {
const cfg = deepMerge(
{ out: { dir: 'dist', clean: true } },
{ out: { dir: 'build' } },
);
assert.deepEqual(cfg, { out: { dir: 'build', clean: true } });
});
test('数组整体替换而非逐项合并', () => {
const cfg = deepMerge({ scenes: ['a', 'b'] }, { scenes: ['c'] });
assert.deepEqual(cfg.scenes, ['c']);
});
test('变量展开与未定义变量保留', () => {
assert.equal(
expandVars('${project}/${name}.zip', { project: 'island', name: 'game' }),
'island/game.zip',
);
assert.equal(expandVars('${missing}/x', {}), '${missing}/x');
});
本机实测(Node v26.5.0,node --test test/13-2-build-config-automation.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前三条钉三层优先级;第四条钉深合并;第五条钉数组替换;第六条钉变量展开。
小结
- 配置三层:默认值、项目、命令行,后者覆盖前者。
- 对象深合并,数组整体替换。
- 未定义变量保持原样,让拼写错误可见。
- 合并函数无副作用,输入对象不被修改。
阅读导航:上一节:13.1 从编辑器触发构建 · 下一节:13.3 发布检查与质量门禁 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。