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-08-10 16:26:00 +08:00
|
|
|
import path, { relative } from 'node:path';
|
2024-07-17 22:30:18 +08:00
|
|
|
|
|
|
|
import { findMonorepoRoot } from '@vben/node-utils';
|
|
|
|
|
2024-05-25 22:43:22 +08:00
|
|
|
import { defineConfig, loadEnv, mergeConfig } from 'vite';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
2024-08-04 22:35:20 +08:00
|
|
|
import { defaultImportmapOptions, getDefaultPwaOptions } from '../options';
|
2024-07-07 00:17:44 +08:00
|
|
|
import { loadApplicationPlugins } from '../plugins';
|
2024-07-29 00:19:26 +08:00
|
|
|
import { loadAndConvertEnv } from '../utils/env';
|
2024-05-19 21:20:42 +08:00
|
|
|
import { getCommonConfig } from './common';
|
|
|
|
|
2024-07-29 22:11:22 +08:00
|
|
|
function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
|
2024-06-16 15:45:15 +08:00
|
|
|
return defineConfig(async (config) => {
|
2024-07-13 21:00:31 +08:00
|
|
|
const options = await userConfigPromise?.(config);
|
2024-08-04 05:42:59 +08:00
|
|
|
const { appTitle, base, port, ...envConfig } = await loadAndConvertEnv();
|
2024-06-16 15:45:15 +08:00
|
|
|
const { command, mode } = config;
|
2024-07-13 21:00:31 +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-08-29 21:47:00 +08:00
|
|
|
archiver: true,
|
|
|
|
archiverPluginOptions: {},
|
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,
|
2024-08-04 22:35:20 +08:00
|
|
|
importmapOptions: defaultImportmapOptions,
|
2024-05-19 21:20:42 +08:00
|
|
|
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-07-20 08:31:05 +08:00
|
|
|
nitroMock: !isBuild,
|
|
|
|
nitroMockOptions: {},
|
|
|
|
print: !isBuild,
|
|
|
|
printInfoMap: {
|
2024-08-02 06:39:05 +08:00
|
|
|
'Vben Admin Docs': 'https://doc.vben.pro',
|
2024-07-20 08:31:05 +08:00
|
|
|
},
|
2024-06-16 15:45:15 +08:00
|
|
|
pwa: true,
|
2024-07-29 00:19:26 +08:00
|
|
|
pwaOptions: getDefaultPwaOptions(appTitle),
|
2024-10-04 23:05:28 +08:00
|
|
|
vxeTableLazyImport: true,
|
2024-07-29 00:19:26 +08:00
|
|
|
...envConfig,
|
2024-07-13 21:00:31 +08:00
|
|
|
...application,
|
2024-05-19 21:20:42 +08:00
|
|
|
});
|
|
|
|
|
2024-07-17 22:30:18 +08:00
|
|
|
const { injectGlobalScss = true } = application;
|
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
const applicationConfig: UserConfig = {
|
2024-07-29 00:19:26 +08:00
|
|
|
base,
|
2024-05-19 21:20:42 +08:00
|
|
|
build: {
|
|
|
|
rollupOptions: {
|
|
|
|
output: {
|
|
|
|
assetFileNames: '[ext]/[name]-[hash].[ext]',
|
|
|
|
chunkFileNames: 'js/[name]-[hash].mjs',
|
|
|
|
entryFileNames: 'jse/index-[name]-[hash].mjs',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: 'es2015',
|
|
|
|
},
|
2024-07-17 22:30:18 +08:00
|
|
|
css: createCssOptions(injectGlobalScss),
|
2024-05-19 21:20:42 +08:00
|
|
|
esbuild: {
|
|
|
|
drop: isBuild
|
|
|
|
? [
|
|
|
|
// 'console',
|
|
|
|
'debugger',
|
|
|
|
]
|
|
|
|
: [],
|
|
|
|
legalComments: 'none',
|
|
|
|
},
|
|
|
|
plugins,
|
|
|
|
server: {
|
|
|
|
host: true,
|
2024-07-29 00:19:26 +08:00
|
|
|
port,
|
2024-05-19 21:20:42 +08:00
|
|
|
warmup: {
|
|
|
|
// 预热文件
|
2024-09-10 21:48:51 +08:00
|
|
|
clientFiles: [
|
|
|
|
'./index.html',
|
|
|
|
'./bootstrap.ts',
|
|
|
|
'./src/{views,layouts,router,store,api}/*',
|
|
|
|
],
|
2024-05-19 21:20:42 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-08-04 05:42:59 +08:00
|
|
|
const mergedCommonConfig = mergeConfig(
|
2024-05-19 21:20:42 +08:00
|
|
|
await getCommonConfig(),
|
|
|
|
applicationConfig,
|
|
|
|
);
|
2024-08-04 05:42:59 +08:00
|
|
|
return mergeConfig(mergedCommonConfig, vite);
|
2024-05-19 21:20:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-17 22:30:18 +08:00
|
|
|
function createCssOptions(injectGlobalScss = true) {
|
|
|
|
const root = findMonorepoRoot();
|
|
|
|
return {
|
|
|
|
preprocessorOptions: injectGlobalScss
|
|
|
|
? {
|
|
|
|
scss: {
|
|
|
|
additionalData: (content: string, filepath: string) => {
|
|
|
|
const relativePath = relative(root, filepath);
|
|
|
|
// apps下的包注入全局样式
|
2024-08-10 16:26:00 +08:00
|
|
|
if (relativePath.startsWith(`apps${path.sep}`)) {
|
2024-08-03 04:40:06 +08:00
|
|
|
return `@import "@vben/styles/global";\n${content}`;
|
2024-07-17 22:30:18 +08:00
|
|
|
}
|
|
|
|
return content;
|
|
|
|
},
|
2024-09-18 22:44:46 +08:00
|
|
|
api: 'modern-compiler',
|
2024-07-17 22:30:18 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
export { defineApplicationConfig };
|