fix(build): 修复 Web 构建组件注册和用户脚本打包问题 (#302)

* refactor(build): 重构 Web 构建管线,支持配置驱动的 Import Maps

- 重构 WebBuildPipeline 支持 split-bundles 和 single-bundle 两种构建模式
- 使用 module.json 的 isCore 字段识别核心模块,消除硬编码列表
- 动态生成 Import Map,从模块清单的 name 字段获取包名映射
- 动态扫描 module.json 文件,不再依赖固定模块列表
- 添加 HTTP 服务器启动脚本 (start-server.bat/sh) 支持 ESM 模块
- 更新 BuildSettingsPanel UI 支持新的构建模式选项
- 添加多语言支持 (zh/en/es)

* fix(build): 修复 Web 构建组件注册和用户脚本打包问题

主要修复:
- 修复组件反序列化时找不到类型的问题
- @ECSComponent 装饰器现在自动注册到 ComponentRegistry
- 添加未使用装饰器的组件警告
- 构建管线自动扫描用户脚本(无需入口文件)

架构改进:
- 解决 Decorators ↔ ComponentRegistry 循环依赖
- 新建 ComponentTypeUtils.ts 作为底层无依赖模块
- 移除冗余的防御性 register 调用
- 统一 ComponentType 定义位置

* refactor(build): 统一 WASM 配置架构,移除硬编码

- 新增 wasmConfig 统一配置替代 wasmPaths/wasmBindings
- wasmConfig.files 支持多候选源路径和明确目标路径
- wasmConfig.runtimePath 指定运行时加载路径
- 重构 _copyWasmFiles 使用统一配置
- HTML 生成使用配置中的 runtimePath
- 移除 physics-rapier2d 的冗余 WASM 配置(由 rapier2d 负责)
- IBuildFileSystem 新增 deleteFile 方法

* feat(build): 单文件构建模式完善和场景配置驱动

## 主要改动

### 单文件构建(single-file mode)
- 修复 WASM 初始化问题,支持 initSync 同步初始化
- 配置驱动的 WASM 识别,通过 wasmConfig.isEngineCore 标识核心引擎模块
- 从 wasmConfig.files 动态获取 JS 绑定路径,消除硬编码

### 场景配置
- 构建验证:必须选择至少一个场景才能构建
- 自动扫描:项目加载时扫描 scenes 目录
- 抽取 _filterScenesByWhitelist 公共方法统一过滤逻辑

### 构建面板优化
- availableScenes prop 传递场景列表
- 场景复选框可点击切换启用状态
- 移除动态 import,使用 prop 传入数据

* chore(build): 补充构建相关的辅助改动

- 添加 BuildFileSystemService 的 listFilesByExtension 优化
- 更新 module.json 添加 externalDependencies 配置
- BrowserRuntime 支持 wasmModule 参数传递
- GameRuntime 添加 loadSceneFromData 方法
- Rust 构建命令更新
- 国际化文案更新

* feat(build): 持久化构建设置到项目配置

## 设计架构

### ProjectService 扩展
- 新增 BuildSettingsConfig 接口定义构建配置字段
- ProjectConfig 添加 buildSettings 字段
- 新增 getBuildSettings / updateBuildSettings 方法

### BuildSettingsPanel
- 组件挂载时从 projectService 加载已保存配置
- 设置变化时自动保存(500ms 防抖)
- 场景选择状态与项目配置同步

### 配置保存位置
保存在项目的 ecs-editor.config.json 中:
- scenes: 选中的场景列表
- buildMode: 构建模式
- companyName/productName/version: 产品信息
- developmentBuild/sourceMap: 构建选项

* fix(editor): Ctrl+S 仅在主编辑区域触发保存场景

- 模态窗口打开时跳过(构建设置、设置、关于等)
- 焦点在 input/textarea/contenteditable 时跳过

* fix(tests): 修复 ECS 测试中 Component 注册问题

- 为所有测试 Component 类添加 @ECSComponent 装饰器
- 移除 beforeEach 中的 ComponentRegistry.reset() 调用
- 将内联 Component 类移到文件顶层以支持装饰器
- 更新测试预期值匹配新的组件类型名称
- 添加缺失的 HierarchyComponent 导入

所有 1388 个测试现已通过。
This commit is contained in:
YHH
2025-12-10 18:23:29 +08:00
committed by GitHub
parent 1b0d38edce
commit a716d8006c
67 changed files with 3671 additions and 1455 deletions

View File

@@ -108,26 +108,118 @@ export interface BuildConfig {
disabledModules?: string[];
}
/**
* Web build mode.
* Web 构建模式。
*/
export type WebBuildMode =
/** Split bundles: Core + plugins loaded on demand, best for production games
* 分包模式:核心包 + 插件按需加载,适合正式游戏 */
| 'split-bundles'
/** Single bundle: All code in one JS file, suitable for simple deployment
* 单包模式:所有代码打包到一个 JS 文件,适合简单部署 */
| 'single-bundle'
/** Single file: Everything inlined into one HTML file, best for playable ads
* 单文件模式:所有内容内联到一个 HTML 文件,适合可玩广告 */
| 'single-file';
/**
* Inline configuration for single-file builds.
* 单文件构建的内联配置。
*
* Single-file mode inlines EVERYTHING into one HTML file by default.
* These options allow disabling specific inlining for debugging purposes.
* 单文件模式默认将所有内容内联到一个 HTML 文件中。
* 这些选项允许为调试目的禁用特定的内联。
*/
export interface InlineConfig {
/**
* Inline JavaScript into HTML as <script> tag content.
* 将 JS 内联到 HTML 的 <script> 标签中。
* Default: true
*/
inlineJs?: boolean;
/**
* Inline WASM files as Base64.
* 将 WASM 文件转为 Base64 内联。
* Default: true
*/
inlineWasm?: boolean;
/**
* Inline asset files (images, audio, fonts) as Base64 data URLs.
* 将资产文件(图片、音频、字体)转为 Base64 data URL 内联。
* Default: true
*/
inlineAssets?: boolean;
/**
* Inline scene JSON files.
* 内联场景 JSON 文件。
* Default: true
*/
inlineScenes?: boolean;
}
/**
* Web platform build configuration.
* Web 平台构建配置。
*/
export interface WebBuildConfig extends BuildConfig {
platform: BuildPlatform.Web;
/** Output format | 输出格式 */
format: 'iife' | 'esm';
/**
* Whether to bundle all modules into a single JS file.
* 是否将所有模块打包成单个 JS 文件
* - true: Bundle into one runtime.browser.js (smaller total size, single request)
* - false: Keep modules separate (better caching, parallel loading)
* Build mode.
* 构建模式
* - 'split-bundles': Core + plugins loaded on demand, best for production (default)
* - 'single-bundle': All code in one JS file, suitable for simple deployment
* - 'single-file': Everything inlined into one HTML, best for playable ads
*/
buildMode: WebBuildMode;
/**
* Inline configuration for single-file builds.
* 单文件构建的内联配置。
* Only used when buildMode is 'single-file'.
*/
inlineConfig?: InlineConfig;
/**
* Whether to minify output.
* 是否压缩输出。
* Default: true for release builds
*/
minify?: boolean;
/**
* Whether to generate HTML file.
* 是否生成 HTML 文件。
*/
generateHtml: boolean;
/**
* HTML template path.
* HTML 模板路径。
*/
htmlTemplate?: string;
/**
* Asset loading strategy.
* 资产加载策略。
* - 'preload': Load all assets before game starts (best for small games)
* - 'on-demand': Load assets when needed (best for large games)
* Default: 'on-demand'
*/
assetLoadingStrategy?: 'preload' | 'on-demand';
/**
* Whether to generate asset catalog.
* 是否生成资产清单。
* Default: true
*/
bundleModules: boolean;
/** Whether to generate HTML file | 是否生成 HTML 文件 */
generateHtml: boolean;
/** HTML template path | HTML 模板路径 */
htmlTemplate?: string;
generateAssetCatalog?: boolean;
}
/**

View File

@@ -12,6 +12,8 @@ export {
type BuildProgress,
type BuildConfig,
type WebBuildConfig,
type WebBuildMode,
type InlineConfig,
type WeChatBuildConfig,
type BuildResult,
type BuildStep,

View File

@@ -49,6 +49,34 @@ export interface ModuleSettings {
disabledModules: string[];
}
/**
* 构建配置
* Build Settings Configuration
*
* Persisted build settings for the project.
* 项目的持久化构建设置。
*/
export interface BuildSettingsConfig {
/** Selected scenes for build | 构建选中的场景 */
scenes?: string[];
/** Scripting defines | 脚本定义 */
scriptingDefines?: string[];
/** Company name | 公司名 */
companyName?: string;
/** Product name | 产品名 */
productName?: string;
/** Version | 版本号 */
version?: string;
/** Development build | 开发构建 */
developmentBuild?: boolean;
/** Source map | 源码映射 */
sourceMap?: boolean;
/** Compression method | 压缩方式 */
compressionMethod?: 'Default' | 'LZ4' | 'LZ4HC';
/** Web build mode | Web 构建模式 */
buildMode?: 'split-bundles' | 'single-bundle' | 'single-file';
}
export interface ProjectConfig {
projectType?: ProjectType;
/** User scripts directory (default: 'scripts') | 用户脚本目录(默认:'scripts' */
@@ -65,6 +93,8 @@ export interface ProjectConfig {
plugins?: PluginSettings;
/** Module settings | 模块配置 */
modules?: ModuleSettings;
/** Build settings | 构建配置 */
buildSettings?: BuildSettingsConfig;
}
@Injectable()
@@ -521,6 +551,34 @@ export class ProjectService implements IService {
return !disabled.includes(moduleId);
}
// ==================== Build Settings ====================
/**
* 获取构建设置
* Get build settings
*/
public getBuildSettings(): BuildSettingsConfig | null {
return this.projectConfig?.buildSettings || null;
}
/**
* 更新构建设置
* Update build settings
*
* @param settings - Build settings to update (partial)
*/
public async updateBuildSettings(settings: Partial<BuildSettingsConfig>): Promise<void> {
const current = this.projectConfig?.buildSettings || {};
await this.updateConfig({
buildSettings: {
...current,
...settings
}
});
await this.messageHub.publish('project:buildSettingsChanged', { settings });
logger.info('Build settings saved');
}
public dispose(): void {
this.currentProject = null;
this.projectConfig = null;