From 8a14069e71d5393cfa5b758f46a1c5c001fa172b Mon Sep 17 00:00:00 2001
From: Vben <anncwb@126.com>
Date: 星期三, 31 三月 2021 23:52:11 +0800
Subject: [PATCH] refactor: refactor dashboard

---
 vite.config.ts |  151 ++++++++++++++++++++------------------------------
 1 files changed, 61 insertions(+), 90 deletions(-)

diff --git a/vite.config.ts b/vite.config.ts
index 662f42f..2934a94 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,120 +1,91 @@
-import type { UserConfig, Resolver } from 'vite';
-import { resolve } from 'path';
+import type { UserConfig, ConfigEnv } from 'vite';
 
 import { loadEnv } from 'vite';
+import { resolve } from 'path';
 
-import { modifyVars } from './build/config/lessModifyVars';
+import { generateModifyVars } from './build/config/themeConfig';
 import { createProxy } from './build/vite/proxy';
-import { configManualChunk } from './build/vite/optimizer';
-
-import globbyTransform from './build/vite/plugin/transform/globby';
-import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import';
-
+import { createAlias } from './build/vite/alias';
 import { wrapperEnv } from './build/utils';
+import { createVitePlugins } from './build/vite/plugin';
+import { OUTPUT_DIR } from './build/constant';
 
-import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
+export default ({ command, mode }: ConfigEnv): UserConfig => {
+  const root = process.cwd();
 
-const pkg = require('./package.json');
-
-function pathResolve(dir: string) {
-  return resolve(__dirname, '.', dir);
-}
-
-const alias: Record<string, string> = {
-  '/@/': pathResolve('src'),
-};
-
-const root: string = process.cwd();
-
-const resolvers: Resolver[] = [];
-
-export default (mode: 'development' | 'production'): UserConfig => {
   const env = loadEnv(mode, root);
+
+  // The boolean type read by loadEnv is a string. This function can be converted to boolean type
   const viteEnv = wrapperEnv(env);
-  const {
-    VITE_PORT,
-    VITE_PUBLIC_PATH,
-    VITE_PROXY,
-    VITE_DROP_CONSOLE,
-    VITE_DYNAMIC_IMPORT,
-  } = viteEnv;
+
+  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
+
+  const isBuild = command === 'build';
+
   return {
-    root,
-    alias,
-    /**
-     * port
-     * @default '3000'
-     */
-    port: VITE_PORT,
-
-    /**
-     * Base public path when served in production.
-     * @default '/'
-     */
     base: VITE_PUBLIC_PATH,
-
-    /**
-     * Transpile target for esbuild.
-     * @default 'es2020'
-     */
-    esbuildTarget: 'es2019',
-
-    // terser options
-    terserOptions: {
-      compress: {
-        keep_infinity: true,
-        drop_console: VITE_DROP_CONSOLE,
-      },
+    root,
+    resolve: {
+      alias: createAlias([
+        // /@/xxxx => src/xxxx
+        ['/@/', 'src'],
+        // /#/xxxx => types/xxxx
+        ['/#/', 'types'],
+      ]),
     },
-
+    server: {
+      port: VITE_PORT,
+      // Load proxy configuration from .env
+      proxy: createProxy(VITE_PROXY),
+    },
+    build: {
+      target: 'es2015',
+      outDir: OUTPUT_DIR,
+      terserOptions: {
+        compress: {
+          keep_infinity: true,
+          // Used to delete console in production environment
+          drop_console: VITE_DROP_CONSOLE,
+        },
+      },
+      // Turning off brotliSize display can slightly reduce packaging time
+      brotliSize: false,
+      chunkSizeWarningLimit: 1200,
+    },
     define: {
-      __VERSION__: pkg.version,
       // setting vue-i18-next
       // Suppress warning
       __VUE_I18N_LEGACY_API__: false,
       __VUE_I18N_FULL_INSTALL__: false,
       __INTLIFY_PROD_DEVTOOLS__: false,
     },
-
-    cssPreprocessOptions: {
-      less: {
-        modifyVars: modifyVars,
-        javascriptEnabled: true,
+    css: {
+      preprocessorOptions: {
+        less: {
+          modifyVars: {
+            // Used for global import to avoid the need to import each style file separately
+            // reference:  Avoid repeated references
+            hack: `true; @import (reference) "${resolve('src/design/config.less')}";`,
+            ...generateModifyVars(),
+          },
+          javascriptEnabled: true,
+        },
       },
     },
 
-    // The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache
+    // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
+    plugins: createVitePlugins(viteEnv, isBuild),
+
     optimizeDeps: {
+      // @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
       include: [
-        'qs',
-        'echarts/map/js/china',
+        '@iconify/iconify',
         'ant-design-vue/es/locale/zh_CN',
+        'moment/dist/locale/zh-cn',
         'ant-design-vue/es/locale/en_US',
-        '@ant-design/icons-vue',
+        'moment/dist/locale/eu',
       ],
-    },
-
-    transforms: [
-      globbyTransform({
-        resolvers: resolvers,
-        root: root,
-        alias: alias,
-        includes: [resolve('src/router'), resolve('src/locales')],
-      }),
-      dynamicImportTransform(VITE_DYNAMIC_IMPORT),
-    ],
-
-    proxy: createProxy(VITE_PROXY),
-
-    plugins: createVitePlugins(viteEnv, mode),
-
-    rollupInputOptions: {
-      plugins: createRollupPlugin(),
-    },
-
-    rollupOutputOptions: {
-      compact: true,
-      manualChunks: configManualChunk,
+      exclude: ['vue-demi'],
     },
   };
 };

--
Gitblit v1.8.0