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

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

5 6 7
import { modifyVars } from './build/config/lessModifyVars';
import { createProxy } from './build/vite/proxy';
import globbyTransform from './build/vite/plugin/context/transform';
V
vben 已提交
8
import dynamicImportTransform from './build/vite/plugin/dynamicImport/index';
9 10

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

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

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

16 17
const viteEnv = loadEnv();

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

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

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

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

V
vben 已提交
126
  // Local cross-domain proxy
B
bin 已提交
127
  proxy: createProxy(VITE_PROXY),
128
  plugins: createVitePlugins(viteEnv),
陈文彬 已提交
129
  rollupInputOptions: {
130 131
    // TODO
    // external: VITE_USE_CDN ? externals : [],
132
    plugins: createRollupPlugin(),
陈文彬 已提交
133 134 135
  },
};

V
vben 已提交
136 137
export default {
  ...viteConfig,
V
vben 已提交
138
  transforms: [globbyTransform(viteConfig), dynamicImportTransform(viteEnv)],
V
vben 已提交
139
} as UserConfig;