vite.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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";
  11. const pathResolve = (dir: string): any => {
  12. return resolve(__dirname, ".", dir);
  13. };
  14. const alias: Record<string, string> = {
  15. "/@": pathResolve("src"),
  16. //解决开发环境下的警告 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.
  17. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  18. };
  19. const root: string = process.cwd();
  20. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  21. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = warpperEnv(
  22. loadEnv(mode, root)
  23. );
  24. const prodMock = true;
  25. return {
  26. /**
  27. * 基本公共路径
  28. * /manages/ 可根据项目部署域名的后缀自行填写(全局搜/manages/替换)
  29. * @default '/'
  30. */
  31. base:
  32. process.env.NODE_ENV === "production" ? "/manages/" : VITE_PUBLIC_PATH,
  33. root,
  34. resolve: {
  35. alias
  36. },
  37. // 服务端渲染
  38. server: {
  39. // 是否开启 https
  40. https: false,
  41. /**
  42. * 端口号
  43. * @default 3000
  44. */
  45. port: VITE_PORT,
  46. host: "0.0.0.0",
  47. // 本地跨域代理
  48. proxy: createProxy(VITE_PROXY)
  49. },
  50. plugins: [
  51. vue(),
  52. vueJsx(),
  53. svgLoader(),
  54. styleImport({
  55. libs: [
  56. // 按需加载vxe-table
  57. {
  58. libraryName: "vxe-table",
  59. esModule: true,
  60. resolveComponent: name => `vxe-table/es/${name}`,
  61. resolveStyle: name => `vxe-table/es/${name}/style.css`
  62. }
  63. ]
  64. }),
  65. ElementPlus({}),
  66. viteMockServe({
  67. mockPath: "mock",
  68. localEnabled: command === "serve",
  69. prodEnabled: command !== "serve" && prodMock,
  70. injectCode: `
  71. import { setupProdMockServer } from './mockProdServer';
  72. setupProdMockServer();
  73. `,
  74. logger: true
  75. })
  76. ],
  77. optimizeDeps: {
  78. include: [
  79. "element-plus/lib/locale/lang/zh-cn",
  80. "element-plus/lib/locale/lang/en",
  81. "vxe-table/lib/locale/lang/zh-CN",
  82. "vxe-table/lib/locale/lang/en-US"
  83. ]
  84. },
  85. build: {
  86. // @ts-ignore
  87. sourcemap: false,
  88. brotliSize: false,
  89. // 消除打包大小超过500kb警告
  90. chunkSizeWarningLimit: 2000
  91. },
  92. define: {
  93. __INTLIFY_PROD_DEVTOOLS__: false
  94. }
  95. };
  96. };