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