Feature/physics and tilemap enhancement (#247)
* feat(behavior-tree,tilemap): 修复编辑器连线缩放问题并增强插件系统 * feat(node-editor,blueprint): 新增通用节点编辑器和蓝图可视化脚本系统 * feat(editor,tilemap): 优化编辑器UI样式和Tilemap编辑器功能 * fix: 修复CodeQL安全警告和CI类型检查错误 * fix: 修复CodeQL安全警告和CI类型检查错误 * fix: 修复CodeQL安全警告和CI类型检查错误
This commit is contained in:
@@ -3,51 +3,60 @@ import { resolve } from 'path';
|
||||
import dts from 'vite-plugin-dts';
|
||||
import react from '@vitejs/plugin-react';
|
||||
|
||||
// 自定义插件:将 CSS 内联到 JS 中
|
||||
function inlineCSS(): any {
|
||||
/**
|
||||
* 自定义插件:将 CSS 转换为自执行的样式注入代码
|
||||
* Custom plugin: Convert CSS to self-executing style injection code
|
||||
*
|
||||
* 当用户写 `import './styles.css'` 时,这个插件会:
|
||||
* 1. 在构建时将 CSS 内容转换为 JS 代码
|
||||
* 2. JS 代码在模块导入时自动执行,将样式注入到 DOM
|
||||
* 3. 使用唯一 ID 防止重复注入
|
||||
*
|
||||
* When user writes `import './styles.css'`, this plugin will:
|
||||
* 1. Convert CSS content to JS code during build
|
||||
* 2. JS code auto-executes when module is imported, injecting styles to DOM
|
||||
* 3. Uses unique ID to prevent duplicate injection
|
||||
*/
|
||||
function injectCSSPlugin(): any {
|
||||
const cssIdMap = new Map<string, string>();
|
||||
let cssCounter = 0;
|
||||
|
||||
return {
|
||||
name: 'inline-css',
|
||||
name: 'inject-css-plugin',
|
||||
enforce: 'post' as const,
|
||||
// 在生成 bundle 时注入 CSS
|
||||
generateBundle(_options: any, bundle: any) {
|
||||
const bundleKeys = Object.keys(bundle);
|
||||
|
||||
// 找到 CSS 文件
|
||||
const cssFile = bundleKeys.find(key => key.endsWith('.css'));
|
||||
if (!cssFile || !bundle[cssFile]) {
|
||||
return;
|
||||
// 找到所有 CSS 文件
|
||||
const cssFiles = bundleKeys.filter(key => key.endsWith('.css'));
|
||||
|
||||
for (const cssFile of cssFiles) {
|
||||
const cssChunk = bundle[cssFile];
|
||||
if (!cssChunk || !cssChunk.source) continue;
|
||||
|
||||
const cssContent = cssChunk.source;
|
||||
const styleId = `esengine-tilemap-style-${cssCounter++}`;
|
||||
cssIdMap.set(cssFile, styleId);
|
||||
|
||||
// 生成样式注入代码
|
||||
const injectCode = `(function(){if(typeof document!=='undefined'){var s=document.createElement('style');s.id='${styleId}';if(!document.getElementById(s.id)){s.textContent=${JSON.stringify(cssContent)};document.head.appendChild(s);}}})();`;
|
||||
|
||||
// 找到引用此 CSS 的 JS chunk 并注入代码
|
||||
for (const jsKey of bundleKeys) {
|
||||
if (!jsKey.endsWith('.js')) continue;
|
||||
const jsChunk = bundle[jsKey];
|
||||
if (!jsChunk || jsChunk.type !== 'chunk' || !jsChunk.code) continue;
|
||||
|
||||
// 检查是否引用了这个 CSS(通过检查是否有相关的 import)
|
||||
// 对于 vite 生成的代码,CSS 导入会被转换,所以我们直接注入到 editor/index.js
|
||||
if (jsKey === 'editor/index.js' || jsKey.match(/^index-[^/]+\.js$/)) {
|
||||
jsChunk.code = injectCode + '\n' + jsChunk.code;
|
||||
}
|
||||
}
|
||||
|
||||
// 删除独立的 CSS 文件
|
||||
delete bundle[cssFile];
|
||||
}
|
||||
|
||||
const cssContent = bundle[cssFile].source;
|
||||
if (!cssContent) return;
|
||||
|
||||
// 找到包含编辑器代码的主要 JS 文件
|
||||
// 优先查找 editor/index.js,然后是带 hash 的 index-*.js
|
||||
const mainJsFile = bundleKeys.find(key =>
|
||||
(key === 'editor/index.js' || key.includes('index-')) &&
|
||||
key.endsWith('.js') &&
|
||||
bundle[key].type === 'chunk' &&
|
||||
bundle[key].code
|
||||
);
|
||||
|
||||
if (mainJsFile && bundle[mainJsFile]) {
|
||||
const injectCode = `
|
||||
(function() {
|
||||
if (typeof document !== 'undefined') {
|
||||
var style = document.createElement('style');
|
||||
style.id = 'esengine-tilemap-styles';
|
||||
if (!document.getElementById(style.id)) {
|
||||
style.textContent = ${JSON.stringify(cssContent)};
|
||||
document.head.appendChild(style);
|
||||
}
|
||||
}
|
||||
})();
|
||||
`;
|
||||
bundle[mainJsFile].code = injectCode + bundle[mainJsFile].code;
|
||||
}
|
||||
|
||||
// 删除独立的 CSS 文件(已内联)
|
||||
delete bundle[cssFile];
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -60,7 +69,7 @@ export default defineConfig({
|
||||
outDir: 'dist',
|
||||
rollupTypes: false
|
||||
}),
|
||||
inlineCSS()
|
||||
injectCSSPlugin()
|
||||
],
|
||||
esbuild: {
|
||||
jsx: 'automatic',
|
||||
|
||||
Reference in New Issue
Block a user