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

124 lines
3.3 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 path, { relative } from 'node:path';
import { findMonorepoRoot } from '@vben/node-utils';
import { defineConfig, loadEnv, mergeConfig } from 'vite';
2024-05-19 21:20:42 +08:00
import { defaultImportmapOptions, getDefaultPwaOptions } from '../options';
import { loadApplicationPlugins } from '../plugins';
import { loadAndConvertEnv } from '../utils/env';
2024-05-19 21:20:42 +08:00
import { getCommonConfig } from './common';
function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
2024-06-16 15:45:15 +08:00
return defineConfig(async (config) => {
const options = await userConfigPromise?.(config);
const { appTitle, base, port, ...envConfig } = await loadAndConvertEnv();
2024-06-16 15:45:15 +08:00
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({
archiver: true,
archiverPluginOptions: {},
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,
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,
nitroMock: !isBuild,
nitroMockOptions: {},
print: !isBuild,
printInfoMap: {
2024-08-02 06:39:05 +08:00
'Vben Admin Docs': 'https://doc.vben.pro',
},
2024-06-16 15:45:15 +08:00
pwa: true,
pwaOptions: getDefaultPwaOptions(appTitle),
vxeTableLazyImport: true,
...envConfig,
...application,
2024-05-19 21:20:42 +08:00
});
const { injectGlobalScss = true } = application;
2024-05-19 21:20:42 +08:00
const applicationConfig: UserConfig = {
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',
},
css: createCssOptions(injectGlobalScss),
2024-05-19 21:20:42 +08:00
esbuild: {
drop: isBuild
? [
// 'console',
'debugger',
]
: [],
legalComments: 'none',
},
plugins,
server: {
host: true,
port,
2024-05-19 21:20:42 +08:00
warmup: {
// 预热文件
clientFiles: [
'./index.html',
'./bootstrap.ts',
'./src/{views,layouts,router,store,api}/*',
],
2024-05-19 21:20:42 +08:00
},
},
};
const mergedCommonConfig = mergeConfig(
2024-05-19 21:20:42 +08:00
await getCommonConfig(),
applicationConfig,
);
return mergeConfig(mergedCommonConfig, vite);
2024-05-19 21:20:42 +08:00
});
}
function createCssOptions(injectGlobalScss = true) {
const root = findMonorepoRoot();
return {
preprocessorOptions: injectGlobalScss
? {
scss: {
additionalData: (content: string, filepath: string) => {
const relativePath = relative(root, filepath);
// apps下的包注入全局样式
if (relativePath.startsWith(`apps${path.sep}`)) {
return `@import "@vben/styles/global";\n${content}`;
}
return content;
},
api: 'modern-compiler',
},
}
: {},
};
}
2024-05-19 21:20:42 +08:00
export { defineApplicationConfig };