plugins.ts 2.5 KB

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