plugins.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 vueJsx from "@vitejs/plugin-vue-jsx";
  7. import VueMacros from "unplugin-vue-macros/vite";
  8. import { viteMockServe } from "vite-plugin-mock";
  9. import { configCompressPlugin } from "./compress";
  10. import VueI18n from "@intlify/vite-plugin-vue-i18n";
  11. import { visualizer } from "rollup-plugin-visualizer";
  12. import removeConsole from "vite-plugin-remove-console";
  13. import themePreprocessorPlugin from "@pureadmin/theme";
  14. import { genScssMultipleScopeVars } from "../src/layout/theme";
  15. export function getPluginsList(
  16. command: string,
  17. VITE_CDN: boolean,
  18. VITE_COMPRESSION: ViteCompression
  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. configCompressPlugin(VITE_COMPRESSION),
  34. VueMacros(),
  35. // 线上环境删除console
  36. removeConsole({ external: ["src/assets/iconfont/iconfont.js"] }),
  37. viteBuildInfo(),
  38. // 自定义主题
  39. themePreprocessorPlugin({
  40. scss: {
  41. multipleScopeVars: genScssMultipleScopeVars(),
  42. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  43. extract: true,
  44. // 会选取defaultScopeName对应的主题css文件在html添加link
  45. themeLinkTagId: "head",
  46. // "head"||"head-prepend" || "body" ||"body-prepend"
  47. themeLinkTagInjectTo: "head",
  48. // 是否对抽取的css文件内对应scopeName的权重类名移除
  49. removeCssScopeName: false
  50. }
  51. }),
  52. // svg组件化支持
  53. svgLoader(),
  54. // mock支持
  55. viteMockServe({
  56. mockPath: "mock",
  57. localEnabled: command === "serve",
  58. prodEnabled: command !== "serve" && prodMock,
  59. injectCode: `
  60. import { setupProdMockServer } from './mockProdServer';
  61. setupProdMockServer();
  62. `,
  63. logger: false
  64. }),
  65. // 打包分析
  66. lifecycle === "report"
  67. ? visualizer({ open: true, brotliSize: true, filename: "report.html" })
  68. : null
  69. ];
  70. }