plugins.ts 4.3 KB

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