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.

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