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';
|
2020-10-13 01:40:21 +08:00
|
|
|
|
import {
|
|
|
|
|
// externals,
|
|
|
|
|
cdnConf,
|
|
|
|
|
} from './build/config/vite/cdn';
|
2020-09-28 20:19:10 +08:00
|
|
|
|
import { createProxy } from './build/config/vite/proxy';
|
|
|
|
|
import { createMockServer } from 'vite-plugin-mock';
|
|
|
|
|
import PurgeIcons from 'vite-plugin-purge-icons';
|
2020-10-16 22:03:44 +08:00
|
|
|
|
import gzipPlugin from './build/plugin/gzip/index';
|
2020-10-18 21:55:21 +08:00
|
|
|
|
import globbyTransform from './build/plugin/vite-plugin-context/transform';
|
2020-10-13 01:40:21 +08:00
|
|
|
|
|
2020-10-16 22:03:44 +08:00
|
|
|
|
import { isDevFn, isReportMode, isProdFn, loadEnv, isBuildGzip, isSiteMode } from './build/utils';
|
2020-10-13 01:40:21 +08:00
|
|
|
|
const pkg = require('./package.json');
|
2020-09-28 20:19:10 +08:00
|
|
|
|
|
2020-10-13 01:40:21 +08:00
|
|
|
|
const {
|
|
|
|
|
VITE_USE_MOCK,
|
|
|
|
|
VITE_PORT,
|
|
|
|
|
VITE_PUBLIC_PATH,
|
|
|
|
|
VITE_PROXY,
|
|
|
|
|
VITE_GLOB_APP_TITLE,
|
2020-10-16 22:03:44 +08:00
|
|
|
|
VITE_DROP_CONSOLE,
|
2020-10-13 01:40:21 +08:00
|
|
|
|
// VITE_USE_CDN,
|
|
|
|
|
} = 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[] = [];
|
|
|
|
|
|
|
|
|
|
(() => {
|
2020-10-16 22:03:44 +08:00
|
|
|
|
if (isProdFn()) {
|
|
|
|
|
if (isReportMode()) {
|
|
|
|
|
// report
|
|
|
|
|
rollupPlugins.push(
|
2020-10-16 23:36:52 +08:00
|
|
|
|
visualizer({ filename: './build/.cache/stats.html', open: true }) as Plugin
|
2020-10-16 22:03:44 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (isBuildGzip() || isSiteMode()) {
|
|
|
|
|
rollupPlugins.push(gzipPlugin());
|
|
|
|
|
}
|
2020-09-28 20:19:10 +08:00
|
|
|
|
}
|
2020-10-16 22:03:44 +08:00
|
|
|
|
|
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'
|
|
|
|
|
*/
|
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,
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-10-13 01:40:21 +08:00
|
|
|
|
define: {
|
|
|
|
|
__VERSION__: pkg.version,
|
|
|
|
|
},
|
2020-09-28 20:19:10 +08:00
|
|
|
|
// css预处理
|
|
|
|
|
cssPreprocessOptions: {
|
|
|
|
|
less: {
|
|
|
|
|
modifyVars: modifyVars,
|
|
|
|
|
javascriptEnabled: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-10-20 21:06:12 +08:00
|
|
|
|
// 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules/.vite_opt_cache
|
2020-09-28 20:19:10 +08:00
|
|
|
|
optimizeDeps: {
|
2020-10-11 14:53:04 +08:00
|
|
|
|
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),
|
2020-09-28 20:19:10 +08:00
|
|
|
|
|
|
|
|
|
plugins: [PurgeIcons(), ...vitePlugins],
|
|
|
|
|
rollupOutputOptions: {},
|
|
|
|
|
rollupInputOptions: {
|
2020-10-13 01:40:21 +08:00
|
|
|
|
// TODO
|
|
|
|
|
// external: VITE_USE_CDN ? externals : [],
|
2020-09-28 20:19:10 +08:00
|
|
|
|
plugins: rollupPlugins,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-13 01:40:21 +08:00
|
|
|
|
// 扩展配置, 往打包后的html注入内容
|
|
|
|
|
// 只针对生产环境
|
|
|
|
|
// TODO 目前只是简单手动注入实现,后续vite应该会提供配置项
|
|
|
|
|
export const htmlConfig: {
|
|
|
|
|
title: string;
|
|
|
|
|
addHm?: boolean;
|
|
|
|
|
cdnConf?: {
|
|
|
|
|
css?: string[];
|
|
|
|
|
js?: string[];
|
|
|
|
|
};
|
|
|
|
|
useCdn: boolean;
|
2020-10-13 23:52:01 +08:00
|
|
|
|
minify: {
|
|
|
|
|
enable: boolean;
|
|
|
|
|
removeComments: boolean;
|
|
|
|
|
collapseWhitespace: boolean;
|
|
|
|
|
minifyJS: boolean;
|
|
|
|
|
minifyCSS: boolean;
|
|
|
|
|
};
|
2020-10-13 01:40:21 +08:00
|
|
|
|
} = {
|
|
|
|
|
// html title
|
|
|
|
|
title: VITE_GLOB_APP_TITLE,
|
|
|
|
|
// 百度统计,不需要可以删除
|
2020-10-16 22:03:44 +08:00
|
|
|
|
// 用于打包部署站点使用。实际项目可以删除
|
|
|
|
|
addHm: isSiteMode(),
|
2020-10-13 01:40:21 +08:00
|
|
|
|
// 使用cdn打包
|
|
|
|
|
// TODO Cdn esm使用方式需要只能支持google,暂时关闭,后续查询更好的方式
|
|
|
|
|
useCdn: false,
|
|
|
|
|
// useCdn: VITE_USE_CDN,
|
|
|
|
|
// cdn列表
|
|
|
|
|
cdnConf,
|
2020-10-13 23:52:01 +08:00
|
|
|
|
minify: {
|
|
|
|
|
enable: true,
|
|
|
|
|
removeComments: true,
|
|
|
|
|
collapseWhitespace: true,
|
|
|
|
|
minifyJS: true,
|
|
|
|
|
minifyCSS: true,
|
|
|
|
|
},
|
2020-10-13 01:40:21 +08:00
|
|
|
|
};
|
2020-10-16 22:03:44 +08:00
|
|
|
|
export default {
|
|
|
|
|
...viteConfig,
|
|
|
|
|
transforms: [globbyTransform(viteConfig)],
|
|
|
|
|
} as UserConfig;
|