plugins.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import vue from "@vitejs/plugin-vue";
  2. import svgLoader from "vite-svg-loader";
  3. import legacy from "@vitejs/plugin-legacy";
  4. import vueJsx from "@vitejs/plugin-vue-jsx";
  5. import WindiCSS from "vite-plugin-windicss";
  6. import { viteMockServe } from "vite-plugin-mock";
  7. import liveReload from "vite-plugin-live-reload";
  8. import styleImport from "vite-plugin-style-import";
  9. import ElementPlus from "unplugin-element-plus/vite";
  10. import { visualizer } from "rollup-plugin-visualizer";
  11. import removeConsole from "vite-plugin-remove-console";
  12. import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
  13. export function getPluginsList(command, VITE_LEGACY) {
  14. const prodMock = true;
  15. const lifecycle = process.env.npm_lifecycle_event;
  16. return [
  17. vue(),
  18. // jsx、tsx语法支持
  19. vueJsx(),
  20. WindiCSS(),
  21. // 线上环境删除console
  22. removeConsole(),
  23. // 修改layout文件夹下的文件时自动重载浏览器 解决 https://github.com/xiaoxian521/vue-pure-admin/issues/170
  24. liveReload(["src/layout/**/*", "src/router/**/*"]),
  25. // 自定义主题
  26. themePreprocessorPlugin({
  27. scss: {
  28. multipleScopeVars: [
  29. {
  30. scopeName: "layout-theme-default",
  31. path: "src/layout/theme/default-vars.scss"
  32. },
  33. {
  34. scopeName: "layout-theme-light",
  35. path: "src/layout/theme/light-vars.scss"
  36. },
  37. {
  38. scopeName: "layout-theme-dusk",
  39. path: "src/layout/theme/dusk-vars.scss"
  40. },
  41. {
  42. scopeName: "layout-theme-volcano",
  43. path: "src/layout/theme/volcano-vars.scss"
  44. },
  45. {
  46. scopeName: "layout-theme-yellow",
  47. path: "src/layout/theme/yellow-vars.scss"
  48. },
  49. {
  50. scopeName: "layout-theme-mingQing",
  51. path: "src/layout/theme/mingQing-vars.scss"
  52. },
  53. {
  54. scopeName: "layout-theme-auroraGreen",
  55. path: "src/layout/theme/auroraGreen-vars.scss"
  56. },
  57. {
  58. scopeName: "layout-theme-pink",
  59. path: "src/layout/theme/pink-vars.scss"
  60. },
  61. {
  62. scopeName: "layout-theme-saucePurple",
  63. path: "src/layout/theme/saucePurple-vars.scss"
  64. }
  65. ],
  66. // 默认取 multipleScopeVars[0].scopeName
  67. defaultScopeName: "",
  68. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  69. extract: true,
  70. // 独立主题css文件的输出路径,默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
  71. outputDir: "",
  72. // 会选取defaultScopeName对应的主题css文件在html添加link
  73. themeLinkTagId: "head",
  74. // "head"||"head-prepend" || "body" ||"body-prepend"
  75. themeLinkTagInjectTo: "head",
  76. // 是否对抽取的css文件内对应scopeName的权重类名移除
  77. removeCssScopeName: false,
  78. // 可以自定义css文件名称的函数
  79. customThemeCssFileName: scopeName => scopeName
  80. }
  81. }),
  82. // svg组件化支持
  83. svgLoader(),
  84. // 按需加载vxe-table
  85. styleImport({
  86. libs: [
  87. {
  88. libraryName: "vxe-table",
  89. esModule: true,
  90. ensureStyleFile: true,
  91. resolveComponent: name => `vxe-table/es/${name}`,
  92. resolveStyle: name => `vxe-table/es/${name}/style.css`
  93. }
  94. ]
  95. }),
  96. ElementPlus({}),
  97. // mock支持
  98. viteMockServe({
  99. mockPath: "mock",
  100. localEnabled: command === "serve",
  101. prodEnabled: command !== "serve" && prodMock,
  102. injectCode: `
  103. import { setupProdMockServer } from './mockProdServer';
  104. setupProdMockServer();
  105. `,
  106. logger: true
  107. }),
  108. // 是否为打包后的文件提供传统浏览器兼容性支持
  109. VITE_LEGACY
  110. ? legacy({
  111. targets: ["ie >= 11"],
  112. additionalLegacyPolyfills: ["regenerator-runtime/runtime"]
  113. })
  114. : null,
  115. // 打包分析
  116. lifecycle === "report"
  117. ? visualizer({ open: true, brotliSize: true, filename: "report.html" })
  118. : null
  119. ];
  120. }