2024-02-20 00:27:09 +08:00

67 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { PluginOption } from 'vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import vue from '@vitejs/plugin-vue';
import vueSetupExtend from 'vite-plugin-vue-setup-extend';
import WindiCSS from 'vite-plugin-windicss';
import { configHtmlPlugin } from './html';
import { configMockPlugin } from './mock';
import { configCompressPlugin } from './compress';
import { configVisualizerConfig } from './visualizer';
import { configSvgIconsPlugin } from './svgSprite';
/**
* 配置 vite 插件
* @param viteEnv vite 环境变量配置文件键值队 object
* @param isBuild 是否是 build 环境 true/false
* @returns vitePlugins[]
*/
export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean, prodMock: boolean) {
// VITE_BUILD_COMPRESS 是否启用 gzip 压缩或 brotli 压缩
// 可选: gzip | brotli | none
// 如果你需要多种形式,你可以用','来分隔
// VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE 打包使用压缩时是否删除原始文件,默认为 false
const { VITE_USE_MOCK, VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv;
const vitePlugins: (PluginOption | PluginOption[])[] = [
// have to
vue(),
// support name https://github.com/vbenjs/vite-plugin-vue-setup-extend
vueSetupExtend(),
// 按需引入VantUi且自动创建组件声明
Components({
dts: true,
resolvers: [VantResolver()],
types: [],
}),
];
// vite-plugin-windicss
vitePlugins.push(WindiCSS());
// 加载 html 插件 vite-plugin-html
vitePlugins.push(configHtmlPlugin(viteEnv, isBuild));
// rollup-plugin-visualizer
vitePlugins.push(configVisualizerConfig());
// vite-plugin-mock
VITE_USE_MOCK && vitePlugins.push(configMockPlugin(isBuild, prodMock));
// vite-plugin-svg-icons
vitePlugins.push(configSvgIconsPlugin(isBuild));
if (isBuild) {
// rollup-plugin-gzip
// 加载 gzip 打包
vitePlugins.push(
configCompressPlugin(VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE)
);
}
return vitePlugins;
}