gf-vben-admin/vite.config.ts

135 lines
2.7 KiB
TypeScript
Raw Normal View History

import type { UserConfig } from 'vite';
2020-09-28 20:19:10 +08:00
import { resolve } from 'path';
import { modifyVars } from './build/config/lessModifyVars';
import { createProxy } from './build/vite/proxy';
import globbyTransform from './build/vite/plugin/context/transform';
import { isDevFn, loadEnv } from './build/utils';
2020-09-28 20:19:10 +08:00
import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
const pkg = require('./package.json');
2020-09-28 20:19:10 +08:00
const viteEnv = loadEnv();
const {
VITE_PORT,
VITE_PUBLIC_PATH,
VITE_PROXY,
2020-10-16 22:03:44 +08:00
VITE_DROP_CONSOLE,
// VITE_USE_CDN,
} = viteEnv;
2020-09-28 20:19:10 +08:00
function pathResolve(dir: string) {
return resolve(__dirname, '.', dir);
}
2020-10-10 21:28:43 +08:00
2020-09-28 20:19:10 +08:00
const viteConfig: UserConfig = {
2020-10-27 00:17:11 +08:00
/**
* 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'
*/
2020-10-27 01:57:39 +08:00
// TODO build error
// entry: './public/index.html',
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'
*/
2020-10-21 21:44:57 +08:00
minify: isDevFn() ? 'esbuild' : 'terser',
2020-09-28 20:19:10 +08:00
/**
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-16 22:03:44 +08:00
* @default 'es2020'
2020-09-28 20:19:10 +08:00
*/
2020-10-16 22:03:44 +08:00
esbuildTarget: 'es2020',
2020-10-10 21:28:43 +08:00
silent: false,
2020-09-28 20:19:10 +08:00
// 别名
alias: {
'/@/': pathResolve('src'),
},
2020-10-16 22:03:44 +08:00
// terser配置
2020-10-24 01:46:29 +08:00
terserOptions: {
2020-10-16 22:03:44 +08:00
compress: {
// 是否删除console
drop_console: VITE_DROP_CONSOLE,
},
},
define: {
__VERSION__: pkg.version,
},
2020-09-28 20:19:10 +08:00
// css预处理
cssPreprocessOptions: {
less: {
modifyVars: modifyVars,
javascriptEnabled: true,
},
},
// 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules/.vite_opt_cache
2020-09-28 20:19:10 +08:00
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-16 22:03:44 +08:00
2020-09-28 20:19:10 +08:00
// 本地跨域代理
2020-10-10 21:28:43 +08:00
proxy: createProxy(VITE_PROXY),
plugins: createVitePlugins(viteEnv),
2020-09-28 20:19:10 +08:00
rollupInputOptions: {
// TODO
// external: VITE_USE_CDN ? externals : [],
plugins: createRollupPlugin(),
2020-09-28 20:19:10 +08:00
},
};
2020-10-16 22:03:44 +08:00
export default {
...viteConfig,
transforms: [globbyTransform(viteConfig)],
} as UserConfig;