115 lines
3.0 KiB
TypeScript
115 lines
3.0 KiB
TypeScript
import type { UserConfig } from 'vite';
|
|
|
|
import type { DefineApplicationOptions } from '../typing';
|
|
|
|
import { relative } from 'node:path';
|
|
|
|
import { findMonorepoRoot } from '@vben/node-utils';
|
|
|
|
import { defineConfig, loadEnv, mergeConfig } from 'vite';
|
|
|
|
import { getDefaultPwaOptions } from '../options';
|
|
import { loadApplicationPlugins } from '../plugins';
|
|
import { loadAndConvertEnv } from '../utils/env';
|
|
import { getCommonConfig } from './common';
|
|
|
|
function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
|
|
return defineConfig(async (config) => {
|
|
const { appTitle, base, port, ...envConfig } = await loadAndConvertEnv();
|
|
const options = await userConfigPromise?.(config);
|
|
const { command, mode } = config;
|
|
const { application = {}, vite = {} } = options || {};
|
|
const root = process.cwd();
|
|
const isBuild = command === 'build';
|
|
const env = loadEnv(mode, root);
|
|
|
|
const plugins = await loadApplicationPlugins({
|
|
compress: false,
|
|
compressTypes: ['brotli', 'gzip'],
|
|
devtools: true,
|
|
env,
|
|
extraAppConfig: true,
|
|
html: true,
|
|
i18n: true,
|
|
injectAppLoading: true,
|
|
injectMetadata: true,
|
|
isBuild,
|
|
license: true,
|
|
mode,
|
|
nitroMock: !isBuild,
|
|
nitroMockOptions: {},
|
|
print: !isBuild,
|
|
printInfoMap: {
|
|
'Vben Admin Docs': 'https://docs.vben.pro',
|
|
},
|
|
pwa: true,
|
|
pwaOptions: getDefaultPwaOptions(appTitle),
|
|
...envConfig,
|
|
...application,
|
|
});
|
|
|
|
const { injectGlobalScss = true } = application;
|
|
|
|
const applicationConfig: UserConfig = {
|
|
base,
|
|
build: {
|
|
rollupOptions: {
|
|
output: {
|
|
assetFileNames: '[ext]/[name]-[hash].[ext]',
|
|
chunkFileNames: 'js/[name]-[hash].mjs',
|
|
entryFileNames: 'jse/index-[name]-[hash].mjs',
|
|
},
|
|
},
|
|
target: 'es2015',
|
|
},
|
|
css: createCssOptions(injectGlobalScss),
|
|
esbuild: {
|
|
drop: isBuild
|
|
? [
|
|
// 'console',
|
|
'debugger',
|
|
]
|
|
: [],
|
|
legalComments: 'none',
|
|
},
|
|
plugins,
|
|
server: {
|
|
host: true,
|
|
port,
|
|
warmup: {
|
|
// 预热文件
|
|
clientFiles: ['./index.html', './src/{views,layouts}/*'],
|
|
},
|
|
},
|
|
};
|
|
|
|
const mergedConfig = mergeConfig(
|
|
await getCommonConfig(),
|
|
applicationConfig,
|
|
);
|
|
return mergeConfig(mergedConfig, vite);
|
|
});
|
|
}
|
|
|
|
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/')) {
|
|
return `@import (reference) "@vben/styles/global";\n${content}`;
|
|
}
|
|
return content;
|
|
},
|
|
},
|
|
}
|
|
: {},
|
|
};
|
|
}
|
|
|
|
export { defineApplicationConfig };
|