gf-vben-admin/vite.config.ts

130 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
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';
import { setupBasicEnv } from './build/config/vite/env';
import { createProxy } from './build/config/vite/proxy';
import { createMockServer } from 'vite-plugin-mock';
import PurgeIcons from 'vite-plugin-purge-icons';
import { isDevFn, isReportMode, isProdFn, loadEnv } from './build/utils';
setupBasicEnv();
2020-10-10 21:28:43 +08:00
const { VITE_USE_MOCK, VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = loadEnv();
2020-09-28 20:19:10 +08:00
function pathResolve(dir: string) {
return resolve(__dirname, '.', dir);
}
2020-10-10 21:28:43 +08:00
const rollupPlugins: any[] = [];
2020-09-28 20:19:10 +08:00
const vitePlugins: VitePlugin[] = [];
(() => {
if (isReportMode() && isProdFn()) {
// report
rollupPlugins.push(
visualizer({ filename: './node_modules/.cache/stats.html', open: true }) as Plugin
);
}
2020-10-10 21:28:43 +08:00
if (isDevFn() && VITE_USE_MOCK) {
2020-09-28 20:19:10 +08:00
// open mock
vitePlugins.push(
createMockServer({
ignore: /^\_/,
mockPath: 'mock',
})
);
}
})();
const viteConfig: UserConfig = {
2020-10-10 21:28:43 +08:00
/**
*
* @default '3000'
*/
port: VITE_PORT,
2020-09-28 20:19:10 +08:00
/**
*
* @default 'localhost'
*/
hostname: 'localhost',
/**
2020-10-01 00:24:14 +08:00
* ·
2020-09-28 20:19:10 +08:00
* @default 'false'
*/
open: false,
/**
*
* boolean | 'terser' | 'esbuild'
* @default 'terser'
*/
minify: isDevFn() ? false : 'terser',
/**
2020-10-10 21:28:43 +08:00
*
2020-09-28 20:19:10 +08:00
* @default '/'
*/
2020-10-10 21:28:43 +08:00
base: VITE_PUBLIC_PATH,
2020-09-28 20:19:10 +08:00
/**
*
* @default 'dist'
*/
outDir: 'dist',
/**
* @default 'false'
*/
sourcemap: false,
/**
*
* @default '_assets'
*/
assetsDir: '_assets',
/**
* 4096kb
* @default '4096'
*/
assetsInlineLimit: 4096,
/**
* esbuild转换目标
2020-10-01 00:24:14 +08:00
* @default 'es2019'
2020-09-28 20:19:10 +08:00
*/
2020-10-01 00:24:14 +08:00
esbuildTarget: 'es2019',
2020-10-10 21:28:43 +08:00
silent: false,
2020-09-28 20:19:10 +08:00
// 别名
alias: {
'/@/': pathResolve('src'),
},
// define: {
// __ENV__: 'value',
// },
// css预处理
cssPreprocessOptions: {
less: {
modifyVars: modifyVars,
javascriptEnabled: true,
},
},
// 配置Dep优化行为
// 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules 下的 .
optimizeDeps: {
include: [
'echarts',
'echarts/map/js/china',
'ant-design-vue/es/locale/zh_CN',
'@ant-design/icons-vue',
'moment/locale/zh-cn',
],
2020-09-28 20:19:10 +08:00
},
// 本地跨域代理
2020-10-10 21:28:43 +08:00
proxy: createProxy(VITE_PROXY),
2020-09-28 20:19:10 +08:00
plugins: [PurgeIcons(), ...vitePlugins],
rollupOutputOptions: {},
rollupInputOptions: {
plugins: rollupPlugins,
},
};
export default viteConfig;