You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.4 KiB

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