import { resolve } from 'node:path'; import dayjs from 'dayjs'; import { readPackageJSON } from 'pkg-types'; import { defineConfig, loadEnv, mergeConfig, type UserConfig } from 'vite'; import { createPlugins } from '../plugins'; import { commonConfig } from './common'; 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_PUBLIC_PATH, VITE_USE_MOCK, VITE_BUILD_COMPRESS, VITE_ENABLE_ANALYZE, VITE_BUILD_LEGACY, VITE_DISABLED_UNOCSS, } = 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, enableLegacy: VITE_BUILD_LEGACY === 'true', enableUnocss: VITE_DISABLED_UNOCSS !== 'true', }); const pathResolve = (pathname: string) => resolve(root, '.', pathname); const applicationConfig: UserConfig = { base: VITE_PUBLIC_PATH, esbuild: { drop: mode === 'production' ? ['console', 'debugger'] : [], }, resolve: { alias: [ { find: 'vue', replacement: 'vue/dist/vue.runtime.esm-bundler.js', }, { 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') + '/', }, ], }, define: defineData, build: { rollupOptions: { treeshake: true, output: { chunkFileNames: 'js/[name]-[hash].js', entryFileNames: 'js/_entry-[name]-[hash].js', assetFileNames: '[ext]/[name]-[hash].[ext]', manualChunks: { vue: ['vue', 'vue-router'], icon: ['virtual:svg-icons-register'], antd: ['ant-design-vue'], design: ['@eciol/design'], // shared: ['@eciol/shared', '@eciol/hooks'], // vben: [ // '@eciol/assets', // '@eciol/design', // '@eciol/directives', // '@eciol/router', // '@eciol/hooks', // '@eciol/locale', // '@eciol/request', // '@eciol/shared', // '@eciol/store', // '@eciol/types', // '@eciol/ant-ui', // '@eciol/share-ui', // '@eciol/layout', // ], }, }, }, }, server: { warmup: { clientFiles: ['./**/*.html'], }, }, plugins: [...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__, }; } catch (error) { return {}; } } export { defineApplicationConfig };