vite.config.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. css: {
  39. // https://github.com/vitejs/vite/issues/5833
  40. postcss: {
  41. plugins: [
  42. {
  43. postcssPlugin: "internal:charset-removal",
  44. AtRule: {
  45. charset: atRule => {
  46. if (atRule.name === "charset") {
  47. atRule.remove();
  48. }
  49. }
  50. }
  51. }
  52. ]
  53. }
  54. },
  55. // 服务端渲染
  56. server: {
  57. // 是否开启 https
  58. https: false,
  59. // 端口号
  60. port: VITE_PORT,
  61. host: "0.0.0.0",
  62. // 本地跨域代理
  63. proxy:
  64. VITE_PROXY_DOMAIN_REAL.length > 0
  65. ? {
  66. [VITE_PROXY_DOMAIN]: {
  67. target: VITE_PROXY_DOMAIN_REAL,
  68. // ws: true,
  69. changeOrigin: true,
  70. rewrite: (path: string) => regExps(path, VITE_PROXY_DOMAIN)
  71. }
  72. }
  73. : null
  74. },
  75. plugins: [
  76. vue(),
  77. // jsx、tsx语法支持
  78. vueJsx(),
  79. // 自定义主题
  80. themePreprocessorPlugin({
  81. scss: {
  82. multipleScopeVars: [
  83. {
  84. scopeName: "layout-theme-default",
  85. path: pathResolve("src/layout/theme/default-vars.scss")
  86. },
  87. {
  88. scopeName: "layout-theme-light",
  89. path: pathResolve("src/layout/theme/light-vars.scss")
  90. },
  91. {
  92. scopeName: "layout-theme-dusk",
  93. path: pathResolve("src/layout/theme/dusk-vars.scss")
  94. },
  95. {
  96. scopeName: "layout-theme-volcano",
  97. path: pathResolve("src/layout/theme/volcano-vars.scss")
  98. },
  99. {
  100. scopeName: "layout-theme-yellow",
  101. path: pathResolve("src/layout/theme/yellow-vars.scss")
  102. },
  103. {
  104. scopeName: "layout-theme-mingQing",
  105. path: pathResolve("src/layout/theme/mingQing-vars.scss")
  106. },
  107. {
  108. scopeName: "layout-theme-auroraGreen",
  109. path: pathResolve("src/layout/theme/auroraGreen-vars.scss")
  110. },
  111. {
  112. scopeName: "layout-theme-pink",
  113. path: pathResolve("src/layout/theme/pink-vars.scss")
  114. },
  115. {
  116. scopeName: "layout-theme-saucePurple",
  117. path: pathResolve("src/layout/theme/saucePurple-vars.scss")
  118. }
  119. ],
  120. // 默认取 multipleScopeVars[0].scopeName
  121. defaultScopeName: "",
  122. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  123. extract: true,
  124. // 独立主题css文件的输出路径,默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
  125. outputDir: "",
  126. // 会选取defaultScopeName对应的主题css文件在html添加link
  127. themeLinkTagId: "head",
  128. // "head"||"head-prepend" || "body" ||"body-prepend"
  129. themeLinkTagInjectTo: "head",
  130. // 是否对抽取的css文件内对应scopeName的权重类名移除
  131. removeCssScopeName: false,
  132. // 可以自定义css文件名称的函数
  133. customThemeCssFileName: scopeName => scopeName
  134. }
  135. }),
  136. // svg组件化支持
  137. svgLoader(),
  138. // 按需加载vxe-table
  139. styleImport({
  140. libs: [
  141. {
  142. libraryName: "vxe-table",
  143. esModule: true,
  144. ensureStyleFile: true,
  145. resolveComponent: name => `vxe-table/es/${name}`,
  146. resolveStyle: name => `vxe-table/es/${name}/style.css`
  147. }
  148. ]
  149. }),
  150. ElementPlus({}),
  151. // mock支持
  152. viteMockServe({
  153. mockPath: "mock",
  154. localEnabled: command === "serve",
  155. prodEnabled: command !== "serve" && prodMock,
  156. injectCode: `
  157. import { setupProdMockServer } from './mockProdServer';
  158. setupProdMockServer();
  159. `,
  160. logger: true
  161. })
  162. ],
  163. optimizeDeps: {
  164. include: [
  165. "element-plus/lib/locale/lang/zh-cn",
  166. "element-plus/lib/locale/lang/en",
  167. "vxe-table/lib/locale/lang/zh-CN",
  168. "vxe-table/lib/locale/lang/en-US"
  169. ],
  170. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  171. },
  172. build: {
  173. sourcemap: false,
  174. brotliSize: false,
  175. // 消除打包大小超过500kb警告
  176. chunkSizeWarningLimit: 2000
  177. },
  178. define: {
  179. __INTLIFY_PROD_DEVTOOLS__: false
  180. }
  181. };
  182. };