| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import { resolve } from 'path'
- import Vue from '@vitejs/plugin-vue'
- import VueJsx from '@vitejs/plugin-vue-jsx'
- import progress from 'vite-plugin-progress'
- import EslintPlugin from 'vite-plugin-eslint'
- import PurgeIcons from 'vite-plugin-purge-icons'
- import { ViteEjsPlugin } from 'vite-plugin-ejs'
- // @ts-ignore
- import ElementPlus from 'unplugin-element-plus/vite'
- import AutoImport from 'unplugin-auto-import/vite'
- import Components from 'unplugin-vue-components/vite'
- import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
- import viteCompression from 'vite-plugin-compression'
- import topLevelAwait from 'vite-plugin-top-level-await'
- import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
- import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
- import UnoCSS from 'unocss/vite'
- import { visualizer } from 'rollup-plugin-visualizer'
- export function createVitePlugins() {
- const root = process.cwd()
- // 路径查找
- function pathResolve(dir: string) {
- return resolve(root, '.', dir)
- }
- const isBuild = process.argv.slice(2).includes('build')
- const plugins = [
- Vue(),
- VueJsx(),
- UnoCSS(),
- progress(),
- PurgeIcons(),
- ElementPlus({}),
- AutoImport({
- include: [
- /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
- /\.vue$/,
- /\.vue\?vue/, // .vue
- /\.md$/ // .md
- ],
- imports: [
- 'vue',
- 'vue-router',
- // 可额外添加需要 autoImport 的组件
- {
- '@/hooks/web/useI18n': ['useI18n'],
- '@/hooks/web/useMessage': ['useMessage'],
- '@/hooks/web/useTable': ['useTable'],
- '@/hooks/web/useCrudSchemas': ['useCrudSchemas'],
- '@/utils/formRules': ['required'],
- '@/utils/dict': ['DICT_TYPE']
- }
- ],
- dts: 'src/types/auto-imports.d.ts',
- resolvers: [ElementPlusResolver()],
- eslintrc: {
- enabled: false, // Default `false`
- filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
- globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
- }
- }),
- Components({
- // 生成自定义 `auto-components.d.ts` 全局声明
- dts: 'src/types/auto-components.d.ts',
- // 自定义组件的解析器
- resolvers: [ElementPlusResolver()],
- globs: ["src/components/**/**.{vue, md}", '!src/components/DiyEditor/components/mobile/**']
- }),
- VueI18nPlugin({
- runtimeOnly: true,
- compositionOnly: true,
- include: [pathResolve('src/locales/**')]
- }),
- createSvgIconsPlugin({
- iconDirs: [pathResolve('src/assets/svgs')],
- symbolId: 'icon-[dir]-[name]',
- svgoOptions: true
- }),
- viteCompression({
- verbose: true,
- disable: false,
- threshold: 10240,
- algorithm: 'gzip',
- ext: '.gz',
- deleteOriginFile: false
- }),
- viteCompression({
- verbose: false,
- disable: false,
- threshold: 10240,
- algorithm: 'brotliCompress',
- ext: '.br',
- deleteOriginFile: false
- }),
- ViteEjsPlugin(),
- topLevelAwait({
- promiseExportName: '__tla',
- promiseImportName: (i) => `__tla_${i}`
- })
- ]
- // ESLint 插件在开发和构建模式都禁用,避免阻塞热更新和构建(使用 IDE 内的 ESLint 即可)
- // if (!isBuild) {
- // plugins.push(
- // EslintPlugin({
- // cache: true,
- // include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx']
- // })
- // )
- // }
- // 仅在分析模式下启用 visualizer: ANALYZE=true pnpm build
- if (process.env.ANALYZE === 'true') {
- plugins.push(visualizer({ open: true, filename: 'visualizer.html' }))
- }
- return plugins
- }
|