vite.config.ts 2.7 KB

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