vite.config.ts 4.5 KB
Newer Older
陈文彬 已提交
1 2 3 4 5 6
import { resolve } from 'path';

import type { UserConfig, Plugin as VitePlugin } from 'vite';

import visualizer from 'rollup-plugin-visualizer';
import { modifyVars } from './build/config/glob/lessModifyVars';
7 8 9 10
import {
  // externals,
  cdnConf,
} from './build/config/vite/cdn';
陈文彬 已提交
11 12 13
import { createProxy } from './build/config/vite/proxy';
import { createMockServer } from 'vite-plugin-mock';
import PurgeIcons from 'vite-plugin-purge-icons';
V
vben 已提交
14
import gzipPlugin from './build/plugin/gzip/index';
V
vben 已提交
15
import globbyTransform from './build/plugin/vite-plugin-context/transform';
16

V
vben 已提交
17
import { isDevFn, isReportMode, isProdFn, loadEnv, isBuildGzip, isSiteMode } from './build/utils';
18
const pkg = require('./package.json');
陈文彬 已提交
19

20 21 22 23 24 25
const {
  VITE_USE_MOCK,
  VITE_PORT,
  VITE_PUBLIC_PATH,
  VITE_PROXY,
  VITE_GLOB_APP_TITLE,
V
vben 已提交
26
  VITE_DROP_CONSOLE,
27 28
  // VITE_USE_CDN,
} = loadEnv();
陈文彬 已提交
29 30 31 32

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

const rollupPlugins: any[] = [];
陈文彬 已提交
35 36 37
const vitePlugins: VitePlugin[] = [];

(() => {
V
vben 已提交
38 39 40 41
  if (isProdFn()) {
    if (isReportMode()) {
      // report
      rollupPlugins.push(
V
vben 已提交
42
        visualizer({ filename: './build/.cache/stats.html', open: true }) as Plugin
V
vben 已提交
43 44 45 46 47
      );
    }
    if (isBuildGzip() || isSiteMode()) {
      rollupPlugins.push(gzipPlugin());
    }
陈文彬 已提交
48
  }
V
vben 已提交
49

B
bin 已提交
50
  if (isDevFn() && VITE_USE_MOCK) {
陈文彬 已提交
51 52 53 54 55 56 57 58 59 60 61
    // open mock
    vitePlugins.push(
      createMockServer({
        ignore: /^\_/,
        mockPath: 'mock',
      })
    );
  }
})();

const viteConfig: UserConfig = {
V
vben 已提交
62 63 64 65 66
  /**
   * 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 已提交
67 68
  // TODO build error
  // entry: './public/index.html',
B
bin 已提交
69 70 71 72 73
  /**
   * 端口号
   * @default '3000'
   */
  port: VITE_PORT,
陈文彬 已提交
74 75 76 77 78 79
  /**
   * 服务地址
   * @default 'localhost'
   */
  hostname: 'localhost',
  /**
陈文彬 已提交
80
   * 运行自动打开浏览器·
陈文彬 已提交
81 82 83 84 85 86 87 88
   * @default 'false'
   */
  open: false,
  /**
   * 压缩代码
   *  boolean | 'terser' | 'esbuild'
   * @default 'terser'
   */
V
vben 已提交
89
  minify: isDevFn() ? 'esbuild' : 'terser',
陈文彬 已提交
90
  /**
B
bin 已提交
91
   * 基本公共路径
陈文彬 已提交
92 93
   * @default '/'
   */
B
bin 已提交
94
  base: VITE_PUBLIC_PATH,
陈文彬 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

  /**
   * 打包输入路径
   * @default 'dist'
   */
  outDir: 'dist',
  /**
   * @default 'false'
   */
  sourcemap: false,
  /**
   * 资源输出路径
   * @default '_assets'
   */
  assetsDir: '_assets',
  /**
   * 静态资源小于该大小将会内联,默认4096kb
   * @default '4096'
   */
  assetsInlineLimit: 4096,
  /**
   * esbuild转换目标。
V
vben 已提交
117
   * @default 'es2020'
陈文彬 已提交
118
   */
V
vben 已提交
119
  esbuildTarget: 'es2020',
B
bin 已提交
120
  silent: false,
陈文彬 已提交
121 122 123 124
  // 别名
  alias: {
    '/@/': pathResolve('src'),
  },
V
vben 已提交
125
  // terser配置
V
vben 已提交
126
  terserOptions: {
V
vben 已提交
127 128 129 130 131
    compress: {
      // 是否删除console
      drop_console: VITE_DROP_CONSOLE,
    },
  },
132 133 134
  define: {
    __VERSION__: pkg.version,
  },
陈文彬 已提交
135 136 137 138 139 140 141
  // css预处理
  cssPreprocessOptions: {
    less: {
      modifyVars: modifyVars,
      javascriptEnabled: true,
    },
  },
V
vben 已提交
142
  // 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules/.vite_opt_cache
陈文彬 已提交
143
  optimizeDeps: {
144 145 146 147 148 149 150
    include: [
      'echarts',
      'echarts/map/js/china',
      'ant-design-vue/es/locale/zh_CN',
      '@ant-design/icons-vue',
      'moment/locale/zh-cn',
    ],
陈文彬 已提交
151
  },
V
vben 已提交
152

陈文彬 已提交
153
  // 本地跨域代理
B
bin 已提交
154
  proxy: createProxy(VITE_PROXY),
陈文彬 已提交
155 156 157 158

  plugins: [PurgeIcons(), ...vitePlugins],
  rollupOutputOptions: {},
  rollupInputOptions: {
159 160
    // TODO
    // external: VITE_USE_CDN ? externals : [],
陈文彬 已提交
161 162 163 164
    plugins: rollupPlugins,
  },
};

165 166 167 168 169 170 171 172 173 174 175
// 扩展配置, 往打包后的html注入内容
// 只针对生产环境
// TODO 目前只是简单手动注入实现,后续vite应该会提供配置项
export const htmlConfig: {
  title: string;
  addHm?: boolean;
  cdnConf?: {
    css?: string[];
    js?: string[];
  };
  useCdn: boolean;
N
nebv 已提交
176 177 178 179 180 181 182
  minify: {
    enable: boolean;
    removeComments: boolean;
    collapseWhitespace: boolean;
    minifyJS: boolean;
    minifyCSS: boolean;
  };
183 184 185 186
} = {
  // html title
  title: VITE_GLOB_APP_TITLE,
  // 百度统计,不需要可以删除
V
vben 已提交
187 188
  // 用于打包部署站点使用。实际项目可以删除
  addHm: isSiteMode(),
189 190 191 192 193 194
  // 使用cdn打包
  // TODO Cdn esm使用方式需要只能支持google,暂时关闭,后续查询更好的方式
  useCdn: false,
  // useCdn: VITE_USE_CDN,
  // cdn列表
  cdnConf,
N
nebv 已提交
195 196 197 198 199 200 201
  minify: {
    enable: true,
    removeComments: true,
    collapseWhitespace: true,
    minifyJS: true,
    minifyCSS: true,
  },
202
};
V
vben 已提交
203 204 205 206
export default {
  ...viteConfig,
  transforms: [globbyTransform(viteConfig)],
} as UserConfig;