2024-08-03 09:49:46 +08:00
|
|
|
|
import fs from 'node:fs';
|
2024-08-03 10:12:45 +08:00
|
|
|
|
import fsp from 'node:fs/promises';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
import { join } from 'node:path';
|
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
|
2024-08-03 09:49:46 +08:00
|
|
|
|
import { readPackageJSON } from '@vben/node-utils';
|
2024-06-08 19:49:06 +08:00
|
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
|
import { type PluginOption } from 'vite';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用于生成将loading样式注入到项目中
|
|
|
|
|
* 为多app提供loading样式,无需在每个 app -> index.html单独引入
|
|
|
|
|
*/
|
2024-05-25 22:43:22 +08:00
|
|
|
|
async function viteInjectAppLoadingPlugin(
|
2024-06-16 15:45:15 +08:00
|
|
|
|
isBuild: boolean,
|
|
|
|
|
env: Record<string, any> = {},
|
2024-07-10 22:44:48 +08:00
|
|
|
|
loadingTemplate = 'loading.html',
|
2024-05-25 22:43:22 +08:00
|
|
|
|
): Promise<PluginOption | undefined> {
|
2024-07-10 22:44:48 +08:00
|
|
|
|
const loadingHtml = await getLoadingRawByHtmlTemplate(loadingTemplate);
|
2024-07-28 14:29:05 +08:00
|
|
|
|
const { version } = await readPackageJSON(process.cwd());
|
2024-05-25 22:43:22 +08:00
|
|
|
|
const envRaw = isBuild ? 'prod' : 'dev';
|
2024-07-28 14:29:05 +08:00
|
|
|
|
const cacheName = `'${env.VITE_APP_NAMESPACE}-${version}-${envRaw}-preferences-theme'`;
|
2024-05-25 22:43:22 +08:00
|
|
|
|
|
|
|
|
|
// 获取缓存的主题
|
|
|
|
|
// 保证黑暗主题下,刷新页面时,loading也是黑暗主题
|
|
|
|
|
const injectScript = `
|
2024-06-02 23:50:58 +08:00
|
|
|
|
<script data-app-loading="inject-js">
|
2024-05-25 22:43:22 +08:00
|
|
|
|
var theme = localStorage.getItem(${cacheName});
|
2024-06-02 23:50:58 +08:00
|
|
|
|
document.documentElement.classList.toggle('dark', /dark/.test(theme));
|
2024-05-25 22:43:22 +08:00
|
|
|
|
</script>
|
|
|
|
|
`;
|
2024-05-19 21:20:42 +08:00
|
|
|
|
|
|
|
|
|
if (!loadingHtml) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
enforce: 'pre',
|
|
|
|
|
name: 'vite:inject-app-loading',
|
|
|
|
|
transformIndexHtml: {
|
|
|
|
|
handler(html) {
|
2024-06-02 23:50:58 +08:00
|
|
|
|
const re = /<body\s*>/;
|
|
|
|
|
html = html.replace(re, `<body>${injectScript}${loadingHtml}`);
|
2024-05-19 21:20:42 +08:00
|
|
|
|
return html;
|
|
|
|
|
},
|
|
|
|
|
order: 'pre',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用于获取loading的html模板
|
|
|
|
|
*/
|
2024-07-10 22:44:48 +08:00
|
|
|
|
async function getLoadingRawByHtmlTemplate(loadingTemplate: string) {
|
2024-05-19 21:20:42 +08:00
|
|
|
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
2024-07-10 22:44:48 +08:00
|
|
|
|
const defaultLoadingPath = join(__dirname, './default-loading.html');
|
|
|
|
|
// 支持在app内自定义loading模板,模版参考default-loading.html即可
|
|
|
|
|
const appLoadingPath = join(process.cwd(), loadingTemplate);
|
|
|
|
|
let loadingPath = defaultLoadingPath;
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(appLoadingPath)) {
|
|
|
|
|
loadingPath = appLoadingPath;
|
2024-05-19 21:20:42 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-03 10:12:45 +08:00
|
|
|
|
const htmlRaw = await fsp.readFile(loadingPath, 'utf8');
|
2024-05-19 21:20:42 +08:00
|
|
|
|
return htmlRaw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { viteInjectAppLoadingPlugin };
|