83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import type { ConfigEnv, PluginOption } from 'vite';
|
||
import path from 'node:path';
|
||
import vue from '@vitejs/plugin-vue';
|
||
import { visualizer } from 'rollup-plugin-visualizer';
|
||
import UnoCSS from 'unocss/vite';
|
||
import AutoImport from 'unplugin-auto-import/vite';
|
||
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
|
||
|
||
import Components from 'unplugin-vue-components/vite';
|
||
import viteCompression from 'vite-plugin-compression';
|
||
|
||
import envTyped from 'vite-plugin-env-typed';
|
||
import fontAwesomePlugin from './fontawesome';
|
||
import gitHashPlugin from './git-hash';
|
||
import preloadPlugin from './preload';
|
||
import createSvgIcon from './svg-icon';
|
||
import versionHtmlPlugin from './version-html';
|
||
|
||
const root = path.resolve(__dirname, '../../');
|
||
|
||
function plugins({ mode, command }: ConfigEnv): PluginOption[] {
|
||
return [
|
||
versionHtmlPlugin(), // 最先处理 HTML 版本号
|
||
gitHashPlugin(),
|
||
preloadPlugin(),
|
||
UnoCSS(),
|
||
fontAwesomePlugin(),
|
||
envTyped({
|
||
mode,
|
||
envDir: root,
|
||
envPrefix: 'VITE_',
|
||
filePath: path.join(root, 'types', 'import_meta.d.ts'),
|
||
}),
|
||
vue(),
|
||
AutoImport({
|
||
imports: ['vue'],
|
||
eslintrc: {
|
||
enabled: true,
|
||
},
|
||
resolvers: [ElementPlusResolver()],
|
||
dts: path.join(root, 'types', 'auto-imports.d.ts'),
|
||
}),
|
||
Components({
|
||
resolvers: [
|
||
ElementPlusResolver(),
|
||
// 自动导入 FontAwesomeIcon 组件
|
||
(componentName) => {
|
||
if (componentName === 'FontAwesomeIcon') {
|
||
return {
|
||
name: 'FontAwesomeIcon',
|
||
from: '@/components/FontAwesomeIcon/index.vue',
|
||
};
|
||
}
|
||
},
|
||
],
|
||
dts: path.join(root, 'types', 'components.d.ts'),
|
||
}),
|
||
createSvgIcon(command === 'build'),
|
||
|
||
// ✅ Gzip 构建产物压缩(仅生产构建)
|
||
command === 'build'
|
||
&& viteCompression({
|
||
verbose: true,
|
||
disable: false,
|
||
threshold: 10240,
|
||
algorithm: 'gzip',
|
||
ext: '.gz',
|
||
}),
|
||
|
||
// ✅ 构建分析图(仅生产构建)
|
||
command === 'build'
|
||
&& visualizer({
|
||
filename: './dist/stats.html',
|
||
open: false, // 打包后自动打开分析图(true 可开启)
|
||
gzipSize: true,
|
||
brotliSize: true,
|
||
}),
|
||
|
||
];
|
||
}
|
||
|
||
export default plugins;
|