vite.config.ts 3.8 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 11
import {
  // externals,
  cdnConf,
} from './build/config/vite/cdn';

陈文彬 已提交
12 13 14
import { createProxy } from './build/config/vite/proxy';
import { createMockServer } from 'vite-plugin-mock';
import PurgeIcons from 'vite-plugin-purge-icons';
15

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

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

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

const rollupPlugins: any[] = [];
陈文彬 已提交
33 34 35 36 37 38 39 40 41
const vitePlugins: VitePlugin[] = [];

(() => {
  if (isReportMode() && isProdFn()) {
    // report
    rollupPlugins.push(
      visualizer({ filename: './node_modules/.cache/stats.html', open: true }) as Plugin
    );
  }
B
bin 已提交
42
  if (isDevFn() && VITE_USE_MOCK) {
陈文彬 已提交
43 44 45 46 47 48 49 50 51 52 53
    // open mock
    vitePlugins.push(
      createMockServer({
        ignore: /^\_/,
        mockPath: 'mock',
      })
    );
  }
})();

const viteConfig: UserConfig = {
B
bin 已提交
54 55 56 57 58
  /**
   * 端口号
   * @default '3000'
   */
  port: VITE_PORT,
陈文彬 已提交
59 60 61 62 63 64
  /**
   * 服务地址
   * @default 'localhost'
   */
  hostname: 'localhost',
  /**
陈文彬 已提交
65
   * 运行自动打开浏览器·
陈文彬 已提交
66 67 68 69 70 71 72 73
   * @default 'false'
   */
  open: false,
  /**
   * 压缩代码
   *  boolean | 'terser' | 'esbuild'
   * @default 'terser'
   */
V
vben 已提交
74
  minify: 'terser',
陈文彬 已提交
75
  /**
B
bin 已提交
76
   * 基本公共路径
陈文彬 已提交
77 78
   * @default '/'
   */
B
bin 已提交
79
  base: VITE_PUBLIC_PATH,
陈文彬 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  /**
   * 打包输入路径
   * @default 'dist'
   */
  outDir: 'dist',
  /**
   * @default 'false'
   */
  sourcemap: false,
  /**
   * 资源输出路径
   * @default '_assets'
   */
  assetsDir: '_assets',
  /**
   * 静态资源小于该大小将会内联,默认4096kb
   * @default '4096'
   */
  assetsInlineLimit: 4096,
  /**
   * esbuild转换目标。
陈文彬 已提交
102
   * @default 'es2019'
陈文彬 已提交
103
   */
陈文彬 已提交
104
  esbuildTarget: 'es2019',
B
bin 已提交
105
  silent: false,
陈文彬 已提交
106 107 108 109
  // 别名
  alias: {
    '/@/': pathResolve('src'),
  },
110 111 112
  define: {
    __VERSION__: pkg.version,
  },
陈文彬 已提交
113 114 115 116 117 118 119 120 121 122
  // css预处理
  cssPreprocessOptions: {
    less: {
      modifyVars: modifyVars,
      javascriptEnabled: true,
    },
  },
  // 配置Dep优化行为
  // 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules 下的 .
  optimizeDeps: {
123 124 125 126 127 128 129
    include: [
      'echarts',
      'echarts/map/js/china',
      'ant-design-vue/es/locale/zh_CN',
      '@ant-design/icons-vue',
      'moment/locale/zh-cn',
    ],
陈文彬 已提交
130 131
  },
  // 本地跨域代理
B
bin 已提交
132
  proxy: createProxy(VITE_PROXY),
陈文彬 已提交
133 134 135 136

  plugins: [PurgeIcons(), ...vitePlugins],
  rollupOutputOptions: {},
  rollupInputOptions: {
137 138
    // TODO
    // external: VITE_USE_CDN ? externals : [],
陈文彬 已提交
139 140 141 142
    plugins: rollupPlugins,
  },
};

143 144 145 146 147 148 149 150 151 152 153 154 155
// 用于打包部署站点使用。实际项目可以删除
const isSite = process.env.SITE === 'true';
// 扩展配置, 往打包后的html注入内容
// 只针对生产环境
// TODO 目前只是简单手动注入实现,后续vite应该会提供配置项
export const htmlConfig: {
  title: string;
  addHm?: boolean;
  cdnConf?: {
    css?: string[];
    js?: string[];
  };
  useCdn: boolean;
N
nebv 已提交
156 157 158 159 160 161 162
  minify: {
    enable: boolean;
    removeComments: boolean;
    collapseWhitespace: boolean;
    minifyJS: boolean;
    minifyCSS: boolean;
  };
163 164 165 166 167 168 169 170 171 172 173
} = {
  // html title
  title: VITE_GLOB_APP_TITLE,
  // 百度统计,不需要可以删除
  addHm: isSite,
  // 使用cdn打包
  // TODO Cdn esm使用方式需要只能支持google,暂时关闭,后续查询更好的方式
  useCdn: false,
  // useCdn: VITE_USE_CDN,
  // cdn列表
  cdnConf,
N
nebv 已提交
174 175 176 177 178 179 180
  minify: {
    enable: true,
    removeComments: true,
    collapseWhitespace: true,
    minifyJS: true,
    minifyCSS: true,
  },
181
};
陈文彬 已提交
182
export default viteConfig;