vite.config.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { resolve } from "path";
  2. import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  3. import vue from "@vitejs/plugin-vue";
  4. import vueJsx from "@vitejs/plugin-vue-jsx";
  5. import { warpperEnv } from "./build/utils";
  6. import { createProxy } from "./build/proxy";
  7. import { viteMockServe } from "vite-plugin-mock";
  8. import svgLoader from "vite-svg-loader";
  9. import styleImport from "vite-plugin-style-import";
  10. import ElementPlus from "unplugin-element-plus/vite";
  11. import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
  12. const pathResolve = (dir: string): string => {
  13. return resolve(__dirname, ".", dir);
  14. };
  15. const alias: Record<string, string> = {
  16. "/@": pathResolve("src"),
  17. "@build": pathResolve("build"),
  18. //解决开发环境下的警告 You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
  19. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  20. };
  21. const root: string = process.cwd();
  22. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  23. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = warpperEnv(
  24. loadEnv(mode, root)
  25. );
  26. const prodMock = true;
  27. return {
  28. /**
  29. * 基本公共路径
  30. * /manages/ 可根据项目部署域名的后缀自行填写(全局搜/manages/替换)
  31. * @default '/'
  32. */
  33. base:
  34. process.env.NODE_ENV === "production" ? "/manages/" : VITE_PUBLIC_PATH,
  35. root,
  36. resolve: {
  37. alias
  38. },
  39. // 服务端渲染
  40. server: {
  41. // 是否开启 https
  42. https: false,
  43. /**
  44. * 端口号
  45. * @default 3000
  46. */
  47. port: VITE_PORT,
  48. host: "0.0.0.0",
  49. // 本地跨域代理
  50. proxy: createProxy(VITE_PROXY)
  51. },
  52. plugins: [
  53. vue(),
  54. vueJsx(),
  55. themePreprocessorPlugin({
  56. scss: {
  57. multipleScopeVars: [
  58. {
  59. // 暗雅(默认)
  60. scopeName: "layout-theme-default",
  61. path: pathResolve("src/layout/theme/default-vars.scss")
  62. },
  63. {
  64. // 明亮
  65. scopeName: "layout-theme-light",
  66. path: pathResolve("src/layout/theme/light-vars.scss")
  67. },
  68. {
  69. // 薄暮
  70. scopeName: "layout-theme-dusk",
  71. path: pathResolve("src/layout/theme/dusk-vars.scss")
  72. },
  73. {
  74. // 火山
  75. scopeName: "layout-theme-volcano",
  76. path: pathResolve("src/layout/theme/volcano-vars.scss")
  77. },
  78. {
  79. // 黄色
  80. scopeName: "layout-theme-yellow",
  81. path: pathResolve("src/layout/theme/yellow-vars.scss")
  82. },
  83. {
  84. // 明青
  85. scopeName: "layout-theme-mingQing",
  86. path: pathResolve("src/layout/theme/mingQing-vars.scss")
  87. },
  88. {
  89. // 极光绿
  90. scopeName: "layout-theme-auroraGreen",
  91. path: pathResolve("src/layout/theme/auroraGreen-vars.scss")
  92. },
  93. {
  94. // 粉红
  95. scopeName: "layout-theme-pink",
  96. path: pathResolve("src/layout/theme/pink-vars.scss")
  97. },
  98. {
  99. // 酱紫
  100. scopeName: "layout-theme-saucePurple",
  101. path: pathResolve("src/layout/theme/saucePurple-vars.scss")
  102. }
  103. ],
  104. // 默认取 multipleScopeVars[0].scopeName
  105. defaultScopeName: "",
  106. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  107. extract: true,
  108. // 独立主题css文件的输出路径,默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
  109. outputDir:
  110. process.env.NODE_ENV === "production"
  111. ? "/manages/assets/"
  112. : VITE_PUBLIC_PATH,
  113. // 会选取defaultScopeName对应的主题css文件在html添加link
  114. themeLinkTagId: "head",
  115. // "head"||"head-prepend" || "body" ||"body-prepend"
  116. themeLinkTagInjectTo: "head",
  117. // 是否对抽取的css文件内对应scopeName的权重类名移除
  118. removeCssScopeName: false,
  119. // 可以自定义css文件名称的函数
  120. customThemeCssFileName: scopeName => scopeName
  121. }
  122. }),
  123. svgLoader(),
  124. styleImport({
  125. libs: [
  126. // 按需加载vxe-table
  127. {
  128. libraryName: "vxe-table",
  129. esModule: true,
  130. ensureStyleFile: true,
  131. resolveComponent: name => `vxe-table/es/${name}`,
  132. resolveStyle: name => `vxe-table/es/${name}/style.css`
  133. }
  134. ]
  135. }),
  136. ElementPlus({}),
  137. viteMockServe({
  138. mockPath: "mock",
  139. localEnabled: command === "serve",
  140. prodEnabled: command !== "serve" && prodMock,
  141. injectCode: `
  142. import { setupProdMockServer } from './mockProdServer';
  143. setupProdMockServer();
  144. `,
  145. logger: true
  146. })
  147. ],
  148. optimizeDeps: {
  149. include: [
  150. "element-plus/lib/locale/lang/zh-cn",
  151. "element-plus/lib/locale/lang/en",
  152. "vxe-table/lib/locale/lang/zh-CN",
  153. "vxe-table/lib/locale/lang/en-US"
  154. ],
  155. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  156. },
  157. build: {
  158. // @ts-ignore
  159. sourcemap: false,
  160. brotliSize: false,
  161. // 消除打包大小超过500kb警告
  162. chunkSizeWarningLimit: 2000,
  163. minify: false
  164. },
  165. define: {
  166. __INTLIFY_PROD_DEVTOOLS__: false
  167. }
  168. };
  169. };