plugins.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { resolve } from "path";
  2. import vue from "@vitejs/plugin-vue";
  3. import { viteBuildInfo } from "./info";
  4. import svgLoader from "vite-svg-loader";
  5. import legacy from "@vitejs/plugin-legacy";
  6. import vueJsx from "@vitejs/plugin-vue-jsx";
  7. import { viteMockServe } from "vite-plugin-mock";
  8. import VueI18n from "@intlify/vite-plugin-vue-i18n";
  9. import { visualizer } from "rollup-plugin-visualizer";
  10. import removeConsole from "vite-plugin-remove-console";
  11. import themePreprocessorPlugin from "@pureadmin/theme";
  12. import { genScssMultipleScopeVars } from "/@/layout/theme";
  13. import DefineOptions from "unplugin-vue-define-options/vite";
  14. export function getPluginsList(command, VITE_LEGACY) {
  15. const prodMock = true;
  16. const lifecycle = process.env.npm_lifecycle_event;
  17. return [
  18. vue(),
  19. // https://github.com/intlify/bundle-tools/tree/main/packages/vite-plugin-vue-i18n
  20. VueI18n({
  21. runtimeOnly: true,
  22. compositionOnly: true,
  23. include: [resolve("locales/**")]
  24. }),
  25. // jsx、tsx语法支持
  26. vueJsx(),
  27. DefineOptions(),
  28. // 线上环境删除console
  29. removeConsole({ external: ["src/assets/iconfont/iconfont.js"] }),
  30. viteBuildInfo(),
  31. // 自定义主题
  32. themePreprocessorPlugin({
  33. scss: {
  34. multipleScopeVars: genScssMultipleScopeVars(),
  35. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  36. extract: true,
  37. // 会选取defaultScopeName对应的主题css文件在html添加link
  38. themeLinkTagId: "head",
  39. // "head"||"head-prepend" || "body" ||"body-prepend"
  40. themeLinkTagInjectTo: "head",
  41. // 是否对抽取的css文件内对应scopeName的权重类名移除
  42. removeCssScopeName: false
  43. }
  44. }),
  45. // svg组件化支持
  46. svgLoader(),
  47. // mock支持
  48. viteMockServe({
  49. mockPath: "mock",
  50. localEnabled: command === "serve",
  51. prodEnabled: command !== "serve" && prodMock,
  52. injectCode: `
  53. import { setupProdMockServer } from './mockProdServer';
  54. setupProdMockServer();
  55. `,
  56. logger: false
  57. }),
  58. // 是否为打包后的文件提供传统浏览器兼容性支持
  59. VITE_LEGACY
  60. ? legacy({
  61. targets: ["ie >= 11"],
  62. additionalLegacyPolyfills: ["regenerator-runtime/runtime"]
  63. })
  64. : null,
  65. // 打包分析
  66. lifecycle === "report"
  67. ? visualizer({ open: true, brotliSize: true, filename: "report.html" })
  68. : null
  69. ];
  70. }