vite.config.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { resolve } from "path";
  2. import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  3. import vue from "@vitejs/plugin-vue";
  4. import vueJsx from "@vitejs/plugin-vue-jsx";
  5. import { warpperEnv } from "./build/utils";
  6. import { createProxy } from "./build/proxy";
  7. import { viteMockServe } from "vite-plugin-mock";
  8. import svgLoader from "vite-svg-loader";
  9. import styleImport from "vite-plugin-style-import";
  10. import ElementPlus from "unplugin-element-plus/vite";
  11. import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
  12. const pathResolve = (dir: string): string => {
  13. return resolve(__dirname, ".", dir);
  14. };
  15. const alias: Record<string, string> = {
  16. "/@": pathResolve("src"),
  17. "@build": pathResolve("build"),
  18. //解决开发环境下的警告 You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
  19. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  20. };
  21. const root: string = process.cwd();
  22. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  23. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = warpperEnv(
  24. loadEnv(mode, root)
  25. );
  26. const prodMock = true;
  27. return {
  28. /**
  29. * 基本公共路径
  30. * /manages/ 可根据项目部署域名的后缀自行填写(全局搜/manages/替换)
  31. * @default '/'
  32. */
  33. base:
  34. process.env.NODE_ENV === "production" ? "/manages/" : VITE_PUBLIC_PATH,
  35. root,
  36. resolve: {
  37. alias
  38. },
  39. // 服务端渲染
  40. server: {
  41. // 是否开启 https
  42. https: false,
  43. /**
  44. * 端口号
  45. * @default 3000
  46. */
  47. port: VITE_PORT,
  48. host: "0.0.0.0",
  49. // 本地跨域代理
  50. proxy: createProxy(VITE_PROXY)
  51. },
  52. plugins: [
  53. vue(),
  54. vueJsx(),
  55. themePreprocessorPlugin({
  56. scss: {
  57. multipleScopeVars: [
  58. {
  59. scopeName: "layout-theme-default",
  60. path: pathResolve("src/layout/theme/default-vars.scss")
  61. },
  62. {
  63. scopeName: "layout-theme-light",
  64. path: pathResolve("src/layout/theme/light-vars.scss")
  65. }
  66. ],
  67. // 默认取 multipleScopeVars[0].scopeName
  68. defaultScopeName: "",
  69. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  70. extract: true,
  71. // 独立主题css文件的输出路径,默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
  72. outputDir: "",
  73. // 会选取defaultScopeName对应的主题css文件在html添加link
  74. themeLinkTagId: "body",
  75. // "head"||"head-prepend" || "body" ||"body-prepend"
  76. themeLinkTagInjectTo: "body",
  77. // 是否对抽取的css文件内对应scopeName的权重类名移除
  78. removeCssScopeName: false,
  79. // 可以自定义css文件名称的函数
  80. customThemeCssFileName: scopeName => scopeName
  81. }
  82. }),
  83. svgLoader(),
  84. styleImport({
  85. libs: [
  86. // 按需加载vxe-table
  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. viteMockServe({
  98. mockPath: "mock",
  99. localEnabled: command === "serve",
  100. prodEnabled: command !== "serve" && prodMock,
  101. injectCode: `
  102. import { setupProdMockServer } from './mockProdServer';
  103. setupProdMockServer();
  104. `,
  105. logger: true
  106. })
  107. ],
  108. optimizeDeps: {
  109. include: [
  110. "element-plus/lib/locale/lang/zh-cn",
  111. "element-plus/lib/locale/lang/en",
  112. "vxe-table/lib/locale/lang/zh-CN",
  113. "vxe-table/lib/locale/lang/en-US"
  114. ]
  115. },
  116. build: {
  117. // @ts-ignore
  118. sourcemap: false,
  119. brotliSize: false,
  120. // 消除打包大小超过500kb警告
  121. chunkSizeWarningLimit: 2000
  122. },
  123. define: {
  124. __INTLIFY_PROD_DEVTOOLS__: false
  125. }
  126. };
  127. };