shuishen
2025-10-11 3fc27febccd04e2fcffbd2cdd16d2daafd0b3ca3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import uniPlugin from '@dcloudio/vite-plugin-uni'
import ViteRestart from 'vite-plugin-restart'
import { AutoImportDeps } from './autoImport'
// import { ConfigImageminPlugin } from './imagemin';
// import { ReplaceUrlPlugin } from './replaceUrl';
import { AutoRegistryComponents } from './component'
import { MCPPlugin } from './mcp'
import { ConfigUnoCSSPlugin } from './unocss'
 
export default function createVitePlugins (isBuild) {
  const vitePlugins = [
    // UnoCSS配置
    ConfigUnoCSSPlugin(),
    // 自动按需引入依赖
    AutoImportDeps(),
    // 自动按需引入组件(注意:需注册至 uni 之前,否则不会生效)
    AutoRegistryComponents(),
    // uni支持(兼容性写法,当type为module时,必须要这样写)
    uniPlugin.default(),
    ViteRestart({
      // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
      restart: ['vite.config.js'],
    }),
    // 为项目开启 MCP Server
    MCPPlugin(),
  ]
 
  if (isBuild) {
    const buildPlugins = [
      // 图片资源自动转换为网络资源
      // ReplaceUrlPlugin(),
      // 自动清除本地图片
      // CleanImagePlugin(),
      // 打包视图分析
      // VisualizerPlugin(),
    ]
    vitePlugins.push(...buildPlugins)
  }
 
  return vitePlugins
}