vite.config.ts 3.0 KB

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