Files
EasyFlow/easyflow-ui-admin/internal/vite-config/src/plugins/inject-app-loading/index.ts
陈子默 cff4fe8da9 build: 适配管理端 flow 基路径
- 调整开发与分析环境的 base 和接口前缀为 /flow

- 更新 Vite 代理配置,兼容 /flow/api 与 /flow/userCenter

- 修复基路径下的 Logo 与启动加载图资源地址
2026-03-18 21:56:36 +08:00

77 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { PluginOption } from 'vite';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readPackageJSON } from '@easyflow/node-utils';
/**
* 用于生成将loading样式注入到项目中
* 为多app提供loading样式无需在每个 app -> index.html单独引入
*/
async function viteInjectAppLoadingPlugin(
isBuild: boolean,
env: Record<string, any> = {},
loadingTemplate = 'loading.html',
): Promise<PluginOption | undefined> {
const loadingHtml = await getLoadingRawByHtmlTemplate(loadingTemplate);
const { version } = await readPackageJSON(process.cwd());
const envRaw = isBuild ? 'prod' : 'dev';
const cacheName = `'${env.VITE_APP_NAMESPACE}-${version}-${envRaw}-preferences-theme'`;
const appBase = JSON.stringify(ensureTrailingSlash(env.VITE_BASE || '/'));
// 获取缓存的主题
// 保证黑暗主题下刷新页面时loading也是黑暗主题
const injectScript = `
<script data-app-loading="inject-js">
var theme = localStorage.getItem(${cacheName});
document.documentElement.classList.toggle('dark', /dark/.test(theme));
setTimeout(() => {
if (/dark/.test(theme)) {
document.querySelector('#__app-loading__ img').src = ${appBase} + 'logoDark.svg';
}
})
</script>
`;
if (!loadingHtml) {
return;
}
return {
enforce: 'pre',
name: 'vite:inject-app-loading',
transformIndexHtml: {
handler(html) {
const re = /<body\s*>/;
html = html.replace(re, `<body>${injectScript}${loadingHtml}`);
return html;
},
order: 'pre',
},
};
}
/**
* 用于获取loading的html模板
*/
async function getLoadingRawByHtmlTemplate(loadingTemplate: string) {
// 支持在app内自定义loading模板模版参考default-loading.html即可
let appLoadingPath = join(process.cwd(), loadingTemplate);
if (!fs.existsSync(appLoadingPath)) {
const __dirname = fileURLToPath(new URL('.', import.meta.url));
appLoadingPath = join(__dirname, './default-loading-progress.html');
}
return await fsp.readFile(appLoadingPath, 'utf8');
}
function ensureTrailingSlash(path: string) {
return path.endsWith('/') ? path : `${path}/`;
}
export { viteInjectAppLoadingPlugin };