123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import { type UserConfig, defineConfig, mergeConfig, loadEnv } from 'vite';
- import { resolve } from 'node:path';
- import { readPackageJSON } from 'pkg-types';
- import { generateModifyVars } from '../utils/modifyVars';
- import { commonConfig } from './common';
- import { createPlugins } from '../plugins';
- import dayjs from 'dayjs';
- interface DefineOptions {
- overrides?: UserConfig;
- options?: {};
- }
- function defineApplicationConfig(defineOptions: DefineOptions = {}) {
- const { overrides = {} } = defineOptions;
- return defineConfig(async ({ command, mode }) => {
- const root = process.cwd();
- const isBuild = command === 'build';
- const { VITE_USE_MOCK, VITE_BUILD_COMPRESS, VITE_ENABLE_ANALYZE } = loadEnv(mode, root);
- const defineData = await createDefineData(root);
- const plugins = await createPlugins({
- isBuild,
- root,
- enableAnalyze: VITE_ENABLE_ANALYZE === 'true',
- enableMock: VITE_USE_MOCK === 'true',
- compress: VITE_BUILD_COMPRESS,
- });
- const pathResolve = (pathname: string) => resolve(root, '.', pathname);
- const applicationConfig: UserConfig = {
- optimizeDeps: {
- include: [
- '@iconify/iconify',
- 'ant-design-vue/es/locale/zh_CN',
- 'ant-design-vue/es/locale/en_US',
- ],
- },
- resolve: {
- alias: [
- {
- find: 'vue-i18n',
- replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
- },
- // /@/xxxx => src/xxxx
- {
- find: /\/@\//,
- replacement: pathResolve('src') + '/',
- },
- // /#/xxxx => types/xxxx
- {
- find: /\/#\//,
- replacement: pathResolve('types') + '/',
- },
- // @/xxxx => src/xxxx
- {
- find: /@\//,
- replacement: pathResolve('src') + '/',
- },
- // #/xxxx => types/xxxx
- {
- find: /#\//,
- replacement: pathResolve('types') + '/',
- },
- ],
- },
- define: defineData,
- build: {
- target: 'es2015',
- cssTarget: 'chrome80',
- rollupOptions: {
- output: {
- manualChunks: {
- vue: ['vue', 'pinia', 'vue-router'],
- antdv: ['ant-design-vue', '@ant-design/icons-vue'],
- },
- },
- },
- },
- css: {
- preprocessorOptions: {
- less: {
- modifyVars: generateModifyVars(),
- javascriptEnabled: true,
- },
- },
- },
- plugins,
- };
- const mergedConfig = mergeConfig(commonConfig, applicationConfig);
- return mergeConfig(mergedConfig, overrides);
- });
- }
- async function createDefineData(root: string) {
- try {
- const pkgJson = await readPackageJSON(root);
- const { dependencies, devDependencies, name, version } = pkgJson;
- const __APP_INFO__ = {
- pkg: { dependencies, devDependencies, name, version },
- lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
- };
- return {
- __APP_INFO__: JSON.stringify(__APP_INFO__),
- };
- } catch (error) {
- return {};
- }
- }
- export { defineApplicationConfig };
|