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:
71
packages/build-config/src/plugins/css-inject.ts
Normal file
71
packages/build-config/src/plugins/css-inject.ts
Normal 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];
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user