vben
2020-11-28 54d14056462566521f2528480c13fb24279156ae
提交 | 用户 | age
173d40 1 import type { UserConfig } from 'vite';
V 2
2f6253 3 import { resolve } from 'path';
4
173d40 5 import { modifyVars } from './build/config/lessModifyVars';
V 6 import { createProxy } from './build/vite/proxy';
8882d4 7
V 8 import globbyTransform from './build/vite/plugin/transform/globby';
9 import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import';
2f6253 10
173d40 11 import { isDevFn, loadEnv } from './build/utils';
2f6253 12
173d40 13 import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
V 14
bb3b8f 15 const pkg = require('./package.json');
N 16
173d40 17 const viteEnv = loadEnv();
V 18
bb3b8f 19 const {
N 20   VITE_PORT,
21   VITE_PUBLIC_PATH,
22   VITE_PROXY,
8a1bfd 23   VITE_DROP_CONSOLE,
bb3b8f 24   // VITE_USE_CDN,
173d40 25 } = viteEnv;
2f6253 26
27 function pathResolve(dir: string) {
28   return resolve(__dirname, '.', dir);
29 }
30
31 const viteConfig: UserConfig = {
e8aede 32   /**
ba068b 33    * Entry. Use this to specify a js entry file in setting cases where an
9ee0a5 34    * `index.html` does not exist (e.g. serving vite assets from a different host)
V 35    * @default 'index.html'
36    */
7bd0b8 37   // TODO build error
4ff6b7 38   // entry: 'public/index.html',
9ee0a5 39   /**
2f1fbf 40    * port
e8aede 41    * @default '3000'
B 42    */
43   port: VITE_PORT,
2f6253 44   /**
45    * @default 'localhost'
46    */
47   hostname: 'localhost',
48   /**
2f1fbf 49    * Run to open the browser automatically
2f6253 50    * @default 'false'
51    */
52   open: false,
53   /**
ba068b 54    * Set to `false` to disable minification, or specify the minifier to setting.
2f1fbf 55    * Available options are 'terser' or 'esbuild'.
2f6253 56    * @default 'terser'
57    */
54d140 58   minify: isDevFn() ? false : 'terser',
2f6253 59   /**
2f1fbf 60    * Base public path when served in production.
2f6253 61    * @default '/'
62    */
e8aede 63   base: VITE_PUBLIC_PATH,
2f6253 64
65   /**
2f1fbf 66    * Directory relative from `root` where build output will be placed. If the
V 67    * directory exists, it will be removed before the build.
2f6253 68    * @default 'dist'
69    */
70   outDir: 'dist',
71   /**
2f1fbf 72    * Whether to generate sourcemap
V 73    * @default false
2f6253 74    */
75   sourcemap: false,
76   /**
2f1fbf 77    * Directory relative from `outDir` where the built js/css/image assets will
V 78    * be placed.
2f6253 79    * @default '_assets'
80    */
81   assetsDir: '_assets',
82   /**
2f1fbf 83    * Static asset files smaller than this number (in bytes) will be inlined as
V 84    * base64 strings. Default limit is `4096` (4kb). Set to `0` to disable.
85    * @default 4096
2f6253 86    */
87   assetsInlineLimit: 4096,
88   /**
2f1fbf 89    * Transpile target for esbuild.
8a1bfd 90    * @default 'es2020'
2f6253 91    */
498278 92   esbuildTarget: 'es2019',
2f1fbf 93   /**
V 94    * Whether to log asset info to console
95    * @default false
96    */
e8aede 97   silent: false,
2f1fbf 98   /**
V 99    * Import alias. The entries can either be exact request -> request mappings
100    * (exact, no wildcard syntax), or request path -> fs directory mappings.
101    * When using directory mappings, the key **must start and end with a slash**.
102    * ```
103    */
2f6253 104   alias: {
105     '/@/': pathResolve('src'),
8a1bfd 106   },
2f1fbf 107   // terser options
a9c2a2 108   terserOptions: {
8a1bfd 109     compress: {
V 110       drop_console: VITE_DROP_CONSOLE,
111     },
2f6253 112   },
bb3b8f 113   define: {
N 114     __VERSION__: pkg.version,
ba068b 115     // setting vue-i18-next
8882d4 116     // Suppress warning
V 117     __VUE_I18N_LEGACY_API__: false,
118     __VUE_I18N_FULL_INSTALL__: false,
119     __INTLIFY_PROD_DEVTOOLS__: false,
bb3b8f 120   },
2f6253 121   cssPreprocessOptions: {
122     less: {
123       modifyVars: modifyVars,
124       javascriptEnabled: true,
125     },
126   },
2f1fbf 127   // 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
2f6253 128   optimizeDeps: {
737b1b 129     include: [
V 130       'echarts/map/js/china',
131       'ant-design-vue/es/locale/zh_CN',
132       'ant-design-vue/es/locale/en_US',
133       '@ant-design/icons-vue',
134     ],
2f6253 135   },
8a1bfd 136
2f1fbf 137   // Local cross-domain proxy
e8aede 138   proxy: createProxy(VITE_PROXY),
173d40 139   plugins: createVitePlugins(viteEnv),
2f6253 140   rollupInputOptions: {
bb3b8f 141     // TODO
N 142     // external: VITE_USE_CDN ? externals : [],
173d40 143     plugins: createRollupPlugin(),
2f6253 144   },
145 };
146
8a1bfd 147 export default {
V 148   ...viteConfig,
8882d4 149   transforms: [
V 150     globbyTransform({
151       resolvers: viteConfig.resolvers,
152       root: viteConfig.root,
153       alias: viteConfig.alias,
154       includes: [resolve('src/router'), resolve('src/locales')],
155     }),
156     dynamicImportTransform(viteEnv),
157   ],
8a1bfd 158 } as UserConfig;