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';
|
|
|
|
|
2024-05-25 22:43:22 +08:00
|
|
|
import { defineConfig, loadEnv, mergeConfig } from 'vite';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-07-07 00:17:44 +08:00
|
|
|
import { loadApplicationPlugins } from '../plugins';
|
2024-05-19 21:20:42 +08:00
|
|
|
import { getCommonConfig } from './common';
|
|
|
|
|
2024-06-02 23:50:58 +08:00
|
|
|
function defineApplicationConfig(options: DefineApplicationOptions = {}) {
|
2024-06-16 15:45:15 +08:00
|
|
|
return defineConfig(async (config) => {
|
|
|
|
const { command, mode } = config;
|
2024-06-02 23:50:58 +08:00
|
|
|
const { application = {}, vite = {} } = options;
|
2024-05-19 21:20:42 +08:00
|
|
|
const root = process.cwd();
|
|
|
|
const isBuild = command === 'build';
|
2024-05-25 22:43:22 +08:00
|
|
|
const env = loadEnv(mode, root);
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-07-07 00:17:44 +08:00
|
|
|
const plugins = await loadApplicationPlugins({
|
2024-05-19 21:20:42 +08:00
|
|
|
compress: false,
|
|
|
|
compressTypes: ['brotli', 'gzip'],
|
|
|
|
devtools: true,
|
2024-05-25 22:43:22 +08:00
|
|
|
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'
|
|
|
|
? application(config)
|
|
|
|
: 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' ? vite(config) : vite,
|
|
|
|
);
|
2024-05-19 21:20:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { defineApplicationConfig };
|