vite.config.ts 6.3 KB

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