vite.config.ts 3.8 KB
Newer Older
1 2
import type { UserConfig } from 'vite';

陈文彬 已提交
3 4
import { resolve } from 'path';

5 6
import { modifyVars } from './build/config/lessModifyVars';
import { createProxy } from './build/vite/proxy';
V
vben 已提交
7 8 9

import globbyTransform from './build/vite/plugin/transform/globby';
import dynamicImportTransform from './build/vite/plugin/transform/dynamic-import';
10 11

import { isDevFn, loadEnv } from './build/utils';
陈文彬 已提交
12

13
import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
14 15

const pkg = require('./package.json');
陈文彬 已提交
16

17 18
const viteEnv = loadEnv();

19 20 21 22
const {
  VITE_PORT,
  VITE_PUBLIC_PATH,
  VITE_PROXY,
V
vben 已提交
23
  VITE_DROP_CONSOLE,
24
  // VITE_USE_CDN,
25
} = viteEnv;
陈文彬 已提交
26 27 28 29

function pathResolve(dir: string) {
  return resolve(__dirname, '.', dir);
}
B
bin 已提交
30

陈文彬 已提交
31
const viteConfig: UserConfig = {
V
vben 已提交
32 33 34 35 36
  /**
   * 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'
   */
V
vben 已提交
37
  // TODO build error
V
vben 已提交
38
  // entry: 'public/index.html',
B
bin 已提交
39
  /**
V
vben 已提交
40
   * port
B
bin 已提交
41 42 43
   * @default '3000'
   */
  port: VITE_PORT,
陈文彬 已提交
44 45 46 47 48
  /**
   * @default 'localhost'
   */
  hostname: 'localhost',
  /**
V
vben 已提交
49
   * Run to open the browser automatically
陈文彬 已提交
50 51 52 53
   * @default 'false'
   */
  open: false,
  /**
V
vben 已提交
54 55
   * Set to `false` to disable minification, or specify the minifier to use.
   * Available options are 'terser' or 'esbuild'.
陈文彬 已提交
56 57
   * @default 'terser'
   */
V
vben 已提交
58
  minify: isDevFn() ? 'esbuild' : 'terser',
陈文彬 已提交
59
  /**
V
vben 已提交
60
   * Base public path when served in production.
陈文彬 已提交
61 62
   * @default '/'
   */
B
bin 已提交
63
  base: VITE_PUBLIC_PATH,
陈文彬 已提交
64 65

  /**
V
vben 已提交
66 67
   * Directory relative from `root` where build output will be placed. If the
   * directory exists, it will be removed before the build.
陈文彬 已提交
68 69 70 71
   * @default 'dist'
   */
  outDir: 'dist',
  /**
V
vben 已提交
72 73
   * Whether to generate sourcemap
   * @default false
陈文彬 已提交
74 75 76
   */
  sourcemap: false,
  /**
V
vben 已提交
77 78
   * Directory relative from `outDir` where the built js/css/image assets will
   * be placed.
陈文彬 已提交
79 80 81 82
   * @default '_assets'
   */
  assetsDir: '_assets',
  /**
V
vben 已提交
83 84 85
   * 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
陈文彬 已提交
86 87 88
   */
  assetsInlineLimit: 4096,
  /**
V
vben 已提交
89
   * Transpile target for esbuild.
V
vben 已提交
90
   * @default 'es2020'
陈文彬 已提交
91
   */
V
vben 已提交
92
  esbuildTarget: 'es2020',
V
vben 已提交
93 94 95 96
  /**
   * Whether to log asset info to console
   * @default false
   */
B
bin 已提交
97
  silent: false,
V
vben 已提交
98 99 100 101 102 103
  /**
   * 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**.
   * ```
   */
陈文彬 已提交
104 105 106
  alias: {
    '/@/': pathResolve('src'),
  },
V
vben 已提交
107
  // terser options
V
vben 已提交
108
  terserOptions: {
V
vben 已提交
109 110 111 112
    compress: {
      drop_console: VITE_DROP_CONSOLE,
    },
  },
113 114
  define: {
    __VERSION__: pkg.version,
V
vben 已提交
115 116 117 118 119
    // use vue-i18-next
    // Suppress warning
    __VUE_I18N_LEGACY_API__: false,
    __VUE_I18N_FULL_INSTALL__: false,
    __INTLIFY_PROD_DEVTOOLS__: false,
120
  },
陈文彬 已提交
121 122 123 124 125 126
  cssPreprocessOptions: {
    less: {
      modifyVars: modifyVars,
      javascriptEnabled: true,
    },
  },
V
vben 已提交
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
陈文彬 已提交
128
  optimizeDeps: {
V
vben 已提交
129
    include: ['echarts/map/js/china', 'ant-design-vue/es/locale/zh_CN', '@ant-design/icons-vue'],
陈文彬 已提交
130
  },
V
vben 已提交
131

V
vben 已提交
132
  // Local cross-domain proxy
B
bin 已提交
133
  proxy: createProxy(VITE_PROXY),
134
  plugins: createVitePlugins(viteEnv),
陈文彬 已提交
135
  rollupInputOptions: {
136 137
    // TODO
    // external: VITE_USE_CDN ? externals : [],
138
    plugins: createRollupPlugin(),
陈文彬 已提交
139 140 141
  },
};

V
vben 已提交
142 143
export default {
  ...viteConfig,
V
vben 已提交
144 145 146 147 148 149 150 151 152
  transforms: [
    globbyTransform({
      resolvers: viteConfig.resolvers,
      root: viteConfig.root,
      alias: viteConfig.alias,
      includes: [resolve('src/router'), resolve('src/locales')],
    }),
    dynamicImportTransform(viteEnv),
  ],
V
vben 已提交
153
} as UserConfig;