zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import vue from '@vitejs/plugin-vue';
 
import vueJsx from '@vitejs/plugin-vue-jsx';
import { enhancePlugin } from 'vite-plugin-vue-enhance';
import removeConsole from 'vite-plugin-remove-console';
 
import { viteBuildInfo } from './info';
 
import liveReload from 'vite-plugin-live-reload';
 
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'; // 把svg 打包放一起
 
import svgLoader from 'vite-svg-loader'; // svg?component
 
import legacy from '@vitejs/plugin-legacy';
 
import { visualizer } from 'rollup-plugin-visualizer';
 
import { createHtmlPlugin } from 'vite-plugin-html';
 
// import ElementPlus from 'unplugin-element-plus/vite'
 
import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import';
 
// import Icons from 'unplugin-icons/vite'
// import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
 
// import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
 
import { viteMockServe } from 'vite-plugin-mock';
// import vitePluginImportus from 'vite-plugin-importus';
 
import path from 'path';
 
import Config, { getAppSetting } from '../config/config';
import { ConfigEnv } from 'vite';
import * as hooks from '../src/hooks/global';
import { configCompressPlugin } from './compress';
import { SubMenuManager } from '@bole-core/theme';
 
type Options = {
  VITE_LEGACY?: ViteEnv['VITE_LEGACY'];
  VITE_COMPRESSION?: ViteEnv['VITE_COMPRESSION'];
  VITE_AppType?: ViteEnv['VITE_AppType'];
};
 
export function getPluginsList(command: ConfigEnv['command'], options: Options = {}) {
  const { VITE_LEGACY, VITE_COMPRESSION, VITE_AppType } = options;
 
  // const prodMock = true;
  const lifecycle = process.env.npm_lifecycle_event;
  console.log(lifecycle, command);
 
  return [
    enhancePlugin(),
    vue(),
    // jsx、tsx语法支持
    vueJsx(),
 
    // https://github.com/vbenjs/vite-plugin-html/tree/main#readme
    createHtmlPlugin({
      minify: true,
      /**
       * Data that needs to be injected into the index.html ejs template
       */
      inject: {
        data: {
          title: getAppSetting(VITE_AppType).title,
          subMenuIconCssPath: SubMenuManager.SubMenuIconCssPath,
        },
      },
    }),
 
    AutoImport({
      resolvers: [ElementPlusResolver()],
      imports: [
        'vue',
        'pinia',
        'vue-router',
        {
          '@/hooks/global': Object.keys(hooks),
        },
        { '@/constants': ['CommonAnnexTableColumns'] },
      ],
      eslintrc: {
        enabled: true, // Default `false`
        filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
        globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
      },
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
 
    configCompressPlugin(VITE_COMPRESSION),
 
    // vitePluginImportus([
    //   {
    //     libraryName: 'lodash',
    //     libraryDirectory: '',
    //     camel2DashComponentName: false,
    //     customName: function (formated: string) {
    //       return `lodash/${formated}`;
    //     },
    //   },
    // ]),
 
    // 线上环境删除console
    removeConsole(),
    // buildInfo terminal output
    viteBuildInfo(),
    // 修改layout文件夹下的文件时自动重载浏览器
    liveReload(['src/layout/**/*', 'src/router/**/*']),
 
    createSvgIconsPlugin({
      // Specify the icon folder to be cached
      iconDirs: [
        path.resolve(process.cwd(), 'src/assets/svgIcons/'),
        // path.resolve(process.cwd(), 'src/TUIKit/assets/icon'),
      ],
      // Specify symbolId format
      symbolId: 'icon-[dir]-[name]',
 
      /**
       * custom insert position
       * inject?: 'body-last' | 'body-first'
       * @default: body-last
       */
      inject: 'body-last',
 
      /**
       * custom dom id
       * customDomId: '__svg__icons__dom__',
       * @default: __svg__icons__dom__
       */
      customDomId: '__svg__icons__dom__',
    }),
 
    // svg组件化支持
    svgLoader({
      defaultImport: 'url',
    }),
 
    createStyleImportPlugin({
      resolves: [ElementPlusResolve()],
      libs: [
        // If you don’t have the resolve you need, you can write it directly in the lib, or you can provide us with PR
        // {
        //   libraryName: 'ant-design-vue',
        //   esModule: true,
        //   resolveStyle: (name) => {
        //     return `ant-design-vue/es/${name}/style/index`
        //   },
        // },
      ],
    }),
 
    // 是否为打包后的文件提供传统浏览器兼容性支持
    // VITE_LEGACY ? legacy() : null,
    // 打包分析
    lifecycle === 'report'
      ? visualizer({ open: true, brotliSize: true, filename: 'report.html' })
      : null,
 
    viteMockServe({
      mockPath: 'mock',
      enable: true,
      watchFiles: false,
      logger: false,
    }),
  ];
}