fix(ecs): resolve ESM require is not defined error (#402)

- Add RuntimeConfig module as standalone runtime environment storage
- Core.runtimeEnvironment and Scene.runtimeEnvironment now read from RuntimeConfig
- Remove require() call in Scene.ts to fix Node.js ESM compatibility

Fixes ReferenceError: require is not defined when using scene.isServer in ESM environment
This commit is contained in:
YHH
2025-12-30 20:52:29 +08:00
committed by GitHub
parent ebcb4d00a8
commit 3e5b7783be
5 changed files with 74 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ import { IPlugin } from './Core/Plugin';
import { WorldManager } from './ECS/WorldManager';
import { DebugConfigService } from './Utils/Debug/DebugConfigService';
import { createInstance } from './Core/DI/Decorators';
import { RuntimeConfig } from './RuntimeConfig';
/**
* @zh 游戏引擎核心类
@@ -86,14 +87,20 @@ export class Core {
* Core.create({ runtimeEnvironment: 'server' });
* ```
*/
public static runtimeEnvironment: RuntimeEnvironment = 'standalone';
public static get runtimeEnvironment(): RuntimeEnvironment {
return RuntimeConfig.runtimeEnvironment;
}
public static set runtimeEnvironment(value: RuntimeEnvironment) {
RuntimeConfig.runtimeEnvironment = value;
}
/**
* @zh 是否在服务端运行
* @en Whether running on server
*/
public static get isServer(): boolean {
return Core.runtimeEnvironment === 'server';
return RuntimeConfig.isServer;
}
/**
@@ -101,7 +108,7 @@ export class Core {
* @en Whether running on client
*/
public static get isClient(): boolean {
return Core.runtimeEnvironment === 'client';
return RuntimeConfig.isClient;
}
/**

View File

@@ -13,6 +13,7 @@ import { QuerySystem } from './Core/QuerySystem';
import { TypeSafeEventSystem } from './Core/EventSystem';
import { ReferenceTracker } from './Core/ReferenceTracker';
import { IScene, ISceneConfig, RuntimeEnvironment } from './IScene';
import { RuntimeConfig } from '../RuntimeConfig';
import { getComponentInstanceTypeName, getSystemInstanceTypeName, getSystemMetadata, getSystemInstanceMetadata } from './Decorators';
import { TypedQueryBuilder } from './Core/Query/TypedQuery';
import {
@@ -200,10 +201,7 @@ export class Scene implements IScene {
if (this._runtimeEnvironmentOverride) {
return this._runtimeEnvironmentOverride;
}
// 动态导入避免循环依赖
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { Core } = require('../Core') as typeof import('../Core');
return Core.runtimeEnvironment;
return RuntimeConfig.runtimeEnvironment;
}
/**

View File

@@ -0,0 +1,50 @@
import type { RuntimeEnvironment } from './Types';
/**
* @zh 全局运行时配置
* @en Global runtime configuration
*
* @zh 独立模块,避免 Core 和 Scene 之间的循环依赖
* @en Standalone module to avoid circular dependency between Core and Scene
*/
class RuntimeConfigClass {
private _runtimeEnvironment: RuntimeEnvironment = 'standalone';
/**
* @zh 获取运行时环境
* @en Get runtime environment
*/
get runtimeEnvironment(): RuntimeEnvironment {
return this._runtimeEnvironment;
}
/**
* @zh 设置运行时环境
* @en Set runtime environment
*/
set runtimeEnvironment(value: RuntimeEnvironment) {
this._runtimeEnvironment = value;
}
/**
* @zh 是否在服务端运行
* @en Whether running on server
*/
get isServer(): boolean {
return this._runtimeEnvironment === 'server';
}
/**
* @zh 是否在客户端运行
* @en Whether running on client
*/
get isClient(): boolean {
return this._runtimeEnvironment === 'client';
}
}
/**
* @zh 全局运行时配置单例
* @en Global runtime configuration singleton
*/
export const RuntimeConfig = new RuntimeConfigClass();

View File

@@ -5,6 +5,7 @@
// 核心模块
export { Core } from './Core';
export { RuntimeConfig } from './RuntimeConfig';
export { ServiceContainer, ServiceLifetime } from './Core/ServiceContainer';
export type { IService, ServiceType, ServiceIdentifier } from './Core/ServiceContainer';