plugins.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { resolve } from 'path'
  2. import { PluginOption } from 'vite'
  3. import { VitePWA } from 'vite-plugin-pwa'
  4. import { visualizer } from 'rollup-plugin-visualizer'
  5. import autoImport from 'unplugin-auto-import/vite'
  6. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  7. import vue from '@vitejs/plugin-vue'
  8. import vueJsx from '@vitejs/plugin-vue-jsx'
  9. import eslintPlugin from 'vite-plugin-eslint'
  10. import viteCompression from 'vite-plugin-compression'
  11. import simpleHtmlPlugin from 'vite-plugin-simple-html'
  12. import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite'
  13. import { prismjsPlugin } from 'vite-plugin-prismjs'
  14. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  15. import IconsResolver from 'unplugin-icons/resolver'
  16. /**
  17. * 创建 vite 插件
  18. * @param viteEnv
  19. */
  20. export const createVitePlugins = (viteEnv: ViteEnv): (PluginOption | PluginOption[])[] => {
  21. const { VITE_GLOB_APP_TITLE, VITE_REPORT, VITE_PWA } = viteEnv
  22. return [
  23. vue(),
  24. // vue 可以使用 jsx/tsx 语法
  25. vueJsx(),
  26. // esLint 报错信息显示在浏览器界面上
  27. eslintPlugin(),
  28. // name 可以写在 script 标签上
  29. vueSetupExtend({}),
  30. // vue api
  31. createAutoImport(),
  32. // 创建打包压缩配置
  33. createCompression(viteEnv),
  34. // 注入变量到 html 文件
  35. simpleHtmlPlugin({
  36. minify: true,
  37. inject: {
  38. data: { title: VITE_GLOB_APP_TITLE }
  39. }
  40. }),
  41. // 使用 svg 图标
  42. createSvgIconsPlugin({
  43. iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
  44. symbolId: 'icon-[dir]-[name]'
  45. }),
  46. // 代码高亮
  47. prismjsPlugin({
  48. languages: ['json', 'js', 'xml', 'java', 'sql', 'ts'], // languages: 'all',
  49. plugins: ['line-numbers', 'copy-to-clipboard', 'inline-color'], //配置显示行号插件
  50. theme: 'tomorrow', //主题名称
  51. css: true
  52. }),
  53. // vitePWA
  54. VITE_PWA && createVitePwa(viteEnv),
  55. // 是否生成包预览,分析依赖包大小做优化处理
  56. VITE_REPORT && (visualizer({ filename: 'stats.html', gzipSize: true, brotliSize: true }) as PluginOption)
  57. ]
  58. }
  59. /**
  60. * @description 根据 compress 配置,生成不同的压缩规则
  61. * @param viteEnv
  62. */
  63. const createCompression = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
  64. const { VITE_BUILD_COMPRESS = 'none', VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE } = viteEnv
  65. const compressList = VITE_BUILD_COMPRESS.split(',')
  66. const plugins: PluginOption[] = []
  67. if (compressList.includes('gzip')) {
  68. plugins.push(
  69. viteCompression({
  70. ext: '.gz',
  71. algorithm: 'gzip',
  72. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
  73. })
  74. )
  75. }
  76. if (compressList.includes('brotli')) {
  77. plugins.push(
  78. viteCompression({
  79. ext: '.br',
  80. algorithm: 'brotliCompress',
  81. deleteOriginFile: VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
  82. })
  83. )
  84. }
  85. return plugins
  86. }
  87. /**
  88. * @description VitePwa
  89. * @param viteEnv
  90. */
  91. const createVitePwa = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
  92. const { VITE_GLOB_APP_TITLE } = viteEnv
  93. return VitePWA({
  94. registerType: 'autoUpdate',
  95. manifest: {
  96. name: VITE_GLOB_APP_TITLE,
  97. short_name: VITE_GLOB_APP_TITLE,
  98. theme_color: '#ffffff',
  99. icons: [
  100. {
  101. src: '/logo.png',
  102. sizes: '192x192',
  103. type: 'image/png'
  104. },
  105. {
  106. src: '/logo.png',
  107. sizes: '512x512',
  108. type: 'image/png'
  109. },
  110. {
  111. src: '/logo.png',
  112. sizes: '512x512',
  113. type: 'image/png',
  114. purpose: 'any maskable'
  115. }
  116. ]
  117. }
  118. })
  119. }
  120. /**
  121. * @description ViteAutoImport
  122. * @param viteEnv
  123. */
  124. const createAutoImport = () => {
  125. return autoImport({
  126. // 自动导入 Vue 相关函数
  127. imports: ['vue', 'vue-router', 'pinia'],
  128. eslintrc: {
  129. enabled: false,
  130. filepath: './.eslintrc-auto-import.json',
  131. globalsPropValue: true
  132. },
  133. resolvers: [
  134. // 自动导入 Element Plus 相关函数ElMessage, ElMessageBox... (带样式)
  135. ElementPlusResolver(),
  136. IconsResolver({
  137. prefix: 'Icon'
  138. })
  139. ],
  140. vueTemplate: true, // 是否在 vue 模板中自动导入
  141. // 生成后改为false
  142. dts: false
  143. // dts: path.resolve(path.resolve(__dirname, '../src'), 'typings', 'auto-imports.d.ts')
  144. })
  145. }