admin-vben5/internal/vite-config/src/config/application.ts

80 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-05-19 21:20:42 +08:00
import type { UserConfig } from 'vite';
2024-06-08 19:49:06 +08:00
import type { DefineApplicationOptions } from '../typing';
import { defineConfig, loadEnv, mergeConfig } from 'vite';
2024-05-19 21:20:42 +08:00
import { loadApplicationPlugins } from '../plugins';
2024-05-19 21:20:42 +08:00
import { getCommonConfig } from './common';
function defineApplicationConfig(options: DefineApplicationOptions = {}) {
2024-06-16 15:45:15 +08:00
return defineConfig(async (config) => {
const { command, mode } = config;
const { application = {}, vite = {} } = options;
2024-05-19 21:20:42 +08:00
const root = process.cwd();
const isBuild = command === 'build';
const env = loadEnv(mode, root);
2024-05-19 21:20:42 +08:00
const plugins = await loadApplicationPlugins({
2024-05-19 21:20:42 +08:00
compress: false,
compressTypes: ['brotli', 'gzip'],
devtools: true,
env,
2024-05-19 21:20:42 +08:00
extraAppConfig: true,
html: true,
i18n: true,
injectAppLoading: true,
2024-06-23 20:05:22 +08:00
injectMetadata: true,
2024-05-19 21:20:42 +08:00
isBuild,
2024-06-23 19:45:40 +08:00
license: true,
2024-05-19 21:20:42 +08:00
mode,
2024-06-16 15:45:15 +08:00
pwa: true,
2024-05-19 21:20:42 +08:00
turboConsole: false,
2024-06-16 15:45:15 +08:00
...(typeof application === 'function'
? await application(config)
2024-06-16 15:45:15 +08:00
: application),
2024-05-19 21:20:42 +08:00
});
const applicationConfig: UserConfig = {
build: {
rollupOptions: {
output: {
assetFileNames: '[ext]/[name]-[hash].[ext]',
chunkFileNames: 'js/[name]-[hash].mjs',
entryFileNames: 'jse/index-[name]-[hash].mjs',
},
},
target: 'es2015',
},
esbuild: {
drop: isBuild
? [
// 'console',
'debugger',
]
: [],
legalComments: 'none',
},
plugins,
server: {
host: true,
warmup: {
// 预热文件
2024-05-25 09:47:15 +08:00
clientFiles: ['./index.html', './src/{views,layouts}/*'],
2024-05-19 21:20:42 +08:00
},
},
};
const mergedConfig = mergeConfig(
await getCommonConfig(),
applicationConfig,
);
2024-06-16 15:45:15 +08:00
return mergeConfig(
mergedConfig,
typeof vite === 'function' ? await vite(config) : vite,
2024-06-16 15:45:15 +08:00
);
2024-05-19 21:20:42 +08:00
});
}
export { defineApplicationConfig };