vite.config.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { resolve } from "path";
  2. import vue from "@vitejs/plugin-vue";
  3. import svgLoader from "vite-svg-loader";
  4. import vueJsx from "@vitejs/plugin-vue-jsx";
  5. import { warpperEnv, regExps } from "./build";
  6. import { viteMockServe } from "vite-plugin-mock";
  7. import styleImport from "vite-plugin-style-import";
  8. import ElementPlus from "unplugin-element-plus/vite";
  9. import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  10. import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
  11. // 当前执行node命令时文件夹的地址(工作目录)
  12. const root: string = process.cwd();
  13. // 路径查找
  14. const pathResolve = (dir: string): string => {
  15. return resolve(__dirname, ".", dir);
  16. };
  17. // 设置别名
  18. const alias: Record<string, string> = {
  19. "/@": pathResolve("src"),
  20. "@build": pathResolve("build"),
  21. //解决开发环境下的警告
  22. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  23. };
  24. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  25. const {
  26. VITE_PORT,
  27. VITE_PUBLIC_PATH,
  28. VITE_PROXY_DOMAIN,
  29. VITE_PROXY_DOMAIN_REAL
  30. } = warpperEnv(loadEnv(mode, root));
  31. const prodMock = true;
  32. return {
  33. base: VITE_PUBLIC_PATH,
  34. root,
  35. resolve: {
  36. alias
  37. },
  38. // 服务端渲染
  39. server: {
  40. // 是否开启 https
  41. https: false,
  42. // 端口号
  43. port: VITE_PORT,
  44. host: "0.0.0.0",
  45. // 本地跨域代理
  46. proxy:
  47. VITE_PROXY_DOMAIN_REAL.length > 0
  48. ? {
  49. [VITE_PROXY_DOMAIN]: {
  50. target: VITE_PROXY_DOMAIN_REAL,
  51. // ws: true,
  52. changeOrigin: true,
  53. rewrite: (path: string) => regExps(path, VITE_PROXY_DOMAIN)
  54. }
  55. }
  56. : null
  57. },
  58. plugins: [
  59. vue(),
  60. // jsx、tsx语法支持
  61. vueJsx(),
  62. // 自定义主题
  63. themePreprocessorPlugin({
  64. scss: {
  65. multipleScopeVars: [
  66. {
  67. scopeName: "layout-theme-default",
  68. path: pathResolve("src/layout/theme/default-vars.scss")
  69. },
  70. {
  71. scopeName: "layout-theme-light",
  72. path: pathResolve("src/layout/theme/light-vars.scss")
  73. },
  74. {
  75. scopeName: "layout-theme-dusk",
  76. path: pathResolve("src/layout/theme/dusk-vars.scss")
  77. },
  78. {
  79. scopeName: "layout-theme-volcano",
  80. path: pathResolve("src/layout/theme/volcano-vars.scss")
  81. },
  82. {
  83. scopeName: "layout-theme-yellow",
  84. path: pathResolve("src/layout/theme/yellow-vars.scss")
  85. },
  86. {
  87. scopeName: "layout-theme-mingQing",
  88. path: pathResolve("src/layout/theme/mingQing-vars.scss")
  89. },
  90. {
  91. scopeName: "layout-theme-auroraGreen",
  92. path: pathResolve("src/layout/theme/auroraGreen-vars.scss")
  93. },
  94. {
  95. scopeName: "layout-theme-pink",
  96. path: pathResolve("src/layout/theme/pink-vars.scss")
  97. },
  98. {
  99. scopeName: "layout-theme-saucePurple",
  100. path: pathResolve("src/layout/theme/saucePurple-vars.scss")
  101. }
  102. ],
  103. // 默认取 multipleScopeVars[0].scopeName
  104. defaultScopeName: "",
  105. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  106. extract: true,
  107. // 独立主题css文件的输出路径,默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
  108. outputDir: "",
  109. // 会选取defaultScopeName对应的主题css文件在html添加link
  110. themeLinkTagId: "head",
  111. // "head"||"head-prepend" || "body" ||"body-prepend"
  112. themeLinkTagInjectTo: "head",
  113. // 是否对抽取的css文件内对应scopeName的权重类名移除
  114. removeCssScopeName: false,
  115. // 可以自定义css文件名称的函数
  116. customThemeCssFileName: scopeName => scopeName
  117. }
  118. }),
  119. // svg组件化支持
  120. svgLoader(),
  121. // 按需加载vxe-table
  122. styleImport({
  123. libs: [
  124. {
  125. libraryName: "vxe-table",
  126. esModule: true,
  127. ensureStyleFile: true,
  128. resolveComponent: name => `vxe-table/es/${name}`,
  129. resolveStyle: name => `vxe-table/es/${name}/style.css`
  130. }
  131. ]
  132. }),
  133. ElementPlus({}),
  134. // mock支持
  135. viteMockServe({
  136. mockPath: "mock",
  137. localEnabled: command === "serve",
  138. prodEnabled: command !== "serve" && prodMock,
  139. injectCode: `
  140. import { setupProdMockServer } from './mockProdServer';
  141. setupProdMockServer();
  142. `,
  143. logger: true
  144. })
  145. ],
  146. optimizeDeps: {
  147. include: [
  148. "element-plus/lib/locale/lang/zh-cn",
  149. "element-plus/lib/locale/lang/en",
  150. "vxe-table/lib/locale/lang/zh-CN",
  151. "vxe-table/lib/locale/lang/en-US"
  152. ],
  153. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  154. },
  155. build: {
  156. sourcemap: false,
  157. brotliSize: false,
  158. // 消除打包大小超过500kb警告
  159. chunkSizeWarningLimit: 2000,
  160. minify: false
  161. },
  162. define: {
  163. __INTLIFY_PROD_DEVTOOLS__: false
  164. }
  165. };
  166. };