Feature/editor optimization (#251)

* refactor: 编辑器/运行时架构拆分与构建系统升级

* feat(core): 层级系统重构与UI变换矩阵修复

* refactor: 移除 ecs-components 聚合包并修复跨包组件查找问题

* fix(physics): 修复跨包组件类引用问题

* feat: 统一运行时架构与浏览器运行支持

* feat(asset): 实现浏览器运行时资产加载系统

* fix: 修复文档、CodeQL安全问题和CI类型检查错误

* fix: 修复文档、CodeQL安全问题和CI类型检查错误

* fix: 修复文档、CodeQL安全问题、CI类型检查和测试错误

* test: 补齐核心模块测试用例,修复CI构建配置

* fix: 修复测试用例中的类型错误和断言问题

* fix: 修复 turbo build:npm 任务的依赖顺序问题

* fix: 修复 CI 构建错误并优化构建性能
This commit is contained in:
YHH
2025-12-01 22:28:51 +08:00
committed by GitHub
parent 189714c727
commit b42a7b4e43
468 changed files with 18301 additions and 9075 deletions

View File

@@ -0,0 +1,100 @@
/**
* Block Editor Plugin
* 阻止编辑器代码泄漏插件
*
* 在运行时构建中检测并阻止编辑器代码被打包
* Detects and blocks editor code from being bundled in runtime builds
*/
import type { Plugin } from 'vite';
export interface BlockEditorOptions {
/** 是否为运行时构建 */
bIsRuntimeBuild: boolean;
/** 要阻止的模块模式 */
blockedPatterns?: (string | RegExp)[];
/** 是否只警告而不报错 */
bWarnOnly?: boolean;
}
const DEFAULT_BLOCKED_PATTERNS: (string | RegExp)[] = [
// React 相关
/^react$/,
/^react-dom$/,
/^react\/jsx-runtime$/,
/^lucide-react$/,
// 编辑器包
/@esengine\/editor-core/,
/@esengine\/node-editor/,
// 编辑器子路径
/\/editor$/,
/\/editor\//,
];
/**
* 创建阻止编辑器代码泄漏的插件
*
* @example
* ```typescript
* import { blockEditorPlugin } from '@esengine/build-config/plugins';
*
* // 在运行时构建中使用
* export default defineConfig({
* plugins: [
* blockEditorPlugin({ bIsRuntimeBuild: true })
* ]
* });
* ```
*/
export function blockEditorPlugin(options: BlockEditorOptions): Plugin {
const {
bIsRuntimeBuild,
blockedPatterns = DEFAULT_BLOCKED_PATTERNS,
bWarnOnly = false
} = options;
if (!bIsRuntimeBuild) {
// 非运行时构建不需要此插件
return { name: 'esengine:block-editor-noop' };
}
const isBlocked = (source: string): boolean => {
return blockedPatterns.some(pattern => {
if (typeof pattern === 'string') {
return source === pattern || source.startsWith(pattern + '/');
}
return pattern.test(source);
});
};
return {
name: 'esengine:block-editor',
enforce: 'pre',
resolveId(source: string, importer: string | undefined) {
if (isBlocked(source)) {
const message = `[block-editor] Editor dependency detected in runtime build:\n` +
` Source: ${source}\n` +
` Importer: ${importer || 'entry'}\n` +
`\n` +
` This usually means:\n` +
` 1. A runtime module is importing from a non-/runtime path\n` +
` 2. An editor-only dependency leaked into the dependency chain\n` +
`\n` +
` Fix: Change the import to use /runtime subpath, e.g.:\n` +
` import { X } from '@esengine/ui/runtime' // ✓\n` +
` import { X } from '@esengine/ui' // ✗`;
if (bWarnOnly) {
console.warn('\x1b[33m' + message + '\x1b[0m');
return { id: source, external: true };
} else {
throw new Error(message);
}
}
return null;
}
};
}

View File

@@ -0,0 +1,71 @@
/**
* CSS Inject Plugin
* CSS 注入插件
*
* 将 CSS 内联到 JS 中,避免单独的 CSS 文件
* Inlines CSS into JS to avoid separate CSS files
*/
import type { Plugin } from 'vite';
import type { OutputBundle, NormalizedOutputOptions, OutputAsset, OutputChunk } from 'rollup';
/**
* 创建 CSS 注入插件
*
* @example
* ```typescript
* import { cssInjectPlugin } from '@esengine/build-config/plugins';
*
* export default defineConfig({
* plugins: [cssInjectPlugin()]
* });
* ```
*/
export function cssInjectPlugin(): Plugin {
return {
name: 'esengine:css-inject',
apply: 'build',
generateBundle(_options: NormalizedOutputOptions, bundle: OutputBundle) {
// 收集所有 CSS 内容
const cssChunks: string[] = [];
const cssFileNames: string[] = [];
for (const [fileName, chunk] of Object.entries(bundle)) {
if (fileName.endsWith('.css') && chunk.type === 'asset') {
cssChunks.push(chunk.source as string);
cssFileNames.push(fileName);
}
}
if (cssChunks.length === 0) return;
// 合并所有 CSS
const combinedCSS = cssChunks.join('\n');
// 创建注入代码
const injectCode = `
(function() {
if (typeof document === 'undefined') return;
var style = document.createElement('style');
style.setAttribute('data-esengine', 'true');
style.textContent = ${JSON.stringify(combinedCSS)};
document.head.appendChild(style);
})();
`;
// 找到主入口 JS 文件并注入
for (const chunk of Object.values(bundle)) {
if (chunk.type === 'chunk' && chunk.isEntry) {
chunk.code = injectCode + chunk.code;
break;
}
}
// 删除独立的 CSS 文件
for (const fileName of cssFileNames) {
delete bundle[fileName];
}
}
};
}

View File

@@ -0,0 +1,7 @@
/**
* Shared Vite Plugins
* 共享 Vite 插件
*/
export { cssInjectPlugin } from './css-inject';
export { blockEditorPlugin, type BlockEditorOptions } from './block-editor';