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.

146 lines
4.8 KiB

3 years ago
  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 ElementPlus from "unplugin-element-plus/vite";
  10. import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
  11. const pathResolve = (dir: string): string => {
  12. return resolve(__dirname, ".", dir);
  13. };
  14. const alias: Record<string, string> = {
  15. "/@": pathResolve("src"),
  16. "@build": pathResolve("build"),
  17. //解决开发环境下的警告 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.
  18. "vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
  19. };
  20. const root: string = process.cwd();
  21. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  22. const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = warpperEnv(
  23. loadEnv(mode, root)
  24. );
  25. const prodMock = true;
  26. return {
  27. /**
  28. *
  29. * /manages/ /manages/
  30. * @default '/'
  31. */
  32. base:
  33. process.env.NODE_ENV === "production" ? "/manages/" : VITE_PUBLIC_PATH,
  34. root,
  35. resolve: {
  36. alias
  37. },
  38. // 服务端渲染
  39. server: {
  40. // 是否开启 https
  41. https: false,
  42. /**
  43. *
  44. * @default 3000
  45. */
  46. port: VITE_PORT,
  47. host: "0.0.0.0",
  48. // 本地跨域代理
  49. proxy: createProxy(VITE_PROXY)
  50. },
  51. plugins: [
  52. vue(),
  53. vueJsx(),
  54. themePreprocessorPlugin({
  55. scss: {
  56. multipleScopeVars: [
  57. {
  58. scopeName: "layout-theme-default",
  59. path: pathResolve("src/layout/theme/default-vars.scss")
  60. },
  61. {
  62. scopeName: "layout-theme-light",
  63. path: pathResolve("src/layout/theme/light-vars.scss")
  64. },
  65. {
  66. scopeName: "layout-theme-dusk",
  67. path: pathResolve("src/layout/theme/dusk-vars.scss")
  68. },
  69. {
  70. scopeName: "layout-theme-volcano",
  71. path: pathResolve("src/layout/theme/volcano-vars.scss")
  72. },
  73. {
  74. scopeName: "layout-theme-yellow",
  75. path: pathResolve("src/layout/theme/yellow-vars.scss")
  76. },
  77. {
  78. scopeName: "layout-theme-mingQing",
  79. path: pathResolve("src/layout/theme/mingQing-vars.scss")
  80. },
  81. {
  82. scopeName: "layout-theme-auroraGreen",
  83. path: pathResolve("src/layout/theme/auroraGreen-vars.scss")
  84. },
  85. {
  86. scopeName: "layout-theme-pink",
  87. path: pathResolve("src/layout/theme/pink-vars.scss")
  88. },
  89. {
  90. scopeName: "layout-theme-saucePurple",
  91. path: pathResolve("src/layout/theme/saucePurple-vars.scss")
  92. }
  93. ],
  94. // 默认取 multipleScopeVars[0].scopeName
  95. defaultScopeName: "",
  96. // 在生产模式是否抽取独立的主题css文件,extract为true以下属性有效
  97. extract: true,
  98. // 独立主题css文件的输出路径,默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
  99. outputDir: "",
  100. // 会选取defaultScopeName对应的主题css文件在html添加link
  101. themeLinkTagId: "head",
  102. // "head"||"head-prepend" || "body" ||"body-prepend"
  103. themeLinkTagInjectTo: "head",
  104. // 是否对抽取的css文件内对应scopeName的权重类名移除
  105. removeCssScopeName: false,
  106. // 可以自定义css文件名称的函数
  107. customThemeCssFileName: scopeName => scopeName
  108. }
  109. }),
  110. svgLoader(),
  111. ElementPlus({}),
  112. viteMockServe({
  113. mockPath: "mock",
  114. localEnabled: command === "serve",
  115. prodEnabled: command !== "serve" && prodMock,
  116. injectCode: `
  117. import { setupProdMockServer } from './mockProdServer';
  118. setupProdMockServer();
  119. `,
  120. logger: true
  121. })
  122. ],
  123. optimizeDeps: {
  124. include: [
  125. "element-plus/lib/locale/lang/zh-cn",
  126. "element-plus/lib/locale/lang/en"
  127. ],
  128. exclude: ["@zougt/vite-plugin-theme-preprocessor/dist/browser-utils"]
  129. },
  130. build: {
  131. // @ts-ignore
  132. sourcemap: false,
  133. brotliSize: false,
  134. // 消除打包大小超过500kb警告
  135. chunkSizeWarningLimit: 2000,
  136. minify: false
  137. },
  138. define: {
  139. __INTLIFY_PROD_DEVTOOLS__: false
  140. }
  141. };
  142. };