vite.config.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. scopeName: "layout-theme-default",
  60. path: pathResolve("src/layout/theme/default-vars.scss")
  61. },
  62. {
  63. scopeName: "layout-theme-light",
  64. path: pathResolve("src/layout/theme/light-vars.scss")
  65. },
  66. {
  67. scopeName: "layout-theme-dusk",
  68. path: pathResolve("src/layout/theme/dusk-vars.scss")
  69. },
  70. {
  71. scopeName: "layout-theme-volcano",
  72. path: pathResolve("src/layout/theme/volcano-vars.scss")
  73. },
  74. {
  75. scopeName: "layout-theme-yellow",
  76. path: pathResolve("src/layout/theme/yellow-vars.scss")
  77. },
  78. {
  79. scopeName: "layout-theme-mingQing",
  80. path: pathResolve("src/layout/theme/mingQing-vars.scss")
  81. },
  82. {
  83. scopeName: "layout-theme-auroraGreen",
  84. path: pathResolve("src/layout/theme/auroraGreen-vars.scss")
  85. },
  86. {
  87. scopeName: "layout-theme-pink",
  88. path: pathResolve("src/layout/theme/pink-vars.scss")
  89. },
  90. {
  91. scopeName: "layout-theme-saucePurple",
  92. path: pathResolve("src/layout/theme/saucePurple-vars.scss")
  93. }
  94. ],
  95. // 默认取 multipleScopeVars[0].scopeName
  96. defaultScopeName: "",
  97. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  98. extract: true,
  99. // 独立主题css文件的输出路径,默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
  100. outputDir: "",
  101. // 会选取defaultScopeName对应的主题css文件在html添加link
  102. themeLinkTagId: "head",
  103. // "head"||"head-prepend" || "body" ||"body-prepend"
  104. themeLinkTagInjectTo: "head",
  105. // 是否对抽取的css文件内对应scopeName的权重类名移除
  106. removeCssScopeName: false,
  107. // 可以自定义css文件名称的函数
  108. customThemeCssFileName: scopeName => scopeName
  109. }
  110. }),
  111. svgLoader(),
  112. styleImport({
  113. libs: [
  114. // 按需加载vxe-table
  115. {
  116. libraryName: "vxe-table",
  117. esModule: true,
  118. ensureStyleFile: true,
  119. resolveComponent: name => `vxe-table/es/${name}`,
  120. resolveStyle: name => `vxe-table/es/${name}/style.css`
  121. }
  122. ]
  123. }),
  124. ElementPlus({}),
  125. viteMockServe({
  126. mockPath: "mock",
  127. localEnabled: command === "serve",
  128. prodEnabled: command !== "serve" && prodMock,
  129. injectCode: `
  130. import { setupProdMockServer } from './mockProdServer';
  131. setupProdMockServer();
  132. `,
  133. logger: true
  134. })
  135. ],
  136. optimizeDeps: {
  137. include: [
  138. "element-plus/lib/locale/lang/zh-cn",
  139. "element-plus/lib/locale/lang/en",
  140. "vxe-table/lib/locale/lang/zh-CN",
  141. "vxe-table/lib/locale/lang/en-US"
  142. ],
  143. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  144. },
  145. build: {
  146. // @ts-ignore
  147. sourcemap: false,
  148. brotliSize: false,
  149. // 消除打包大小超过500kb警告
  150. chunkSizeWarningLimit: 2000,
  151. minify: false
  152. },
  153. define: {
  154. __INTLIFY_PROD_DEVTOOLS__: false
  155. }
  156. };
  157. };