vite.config.ts 5.9 KB

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