vite.config.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { loadEnv, defineConfig } from "vite";
  2. import vue from "@vitejs/plugin-vue";
  3. // import { createStyleImportPlugin, VantResolve } from "vite-plugin-style-import";
  4. import viteCompression from "vite-plugin-compression";
  5. // import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
  6. //import Components from "unplugin-vue-components/vite";
  7. //import svgLoader from "vite-svg-loader";
  8. import path from "path";
  9. // import AutoImport from "unplugin-auto-import/vite";
  10. //import jsx from "@vitejs/plugin-vue-jsx";
  11. import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
  12. import VueSetupExtend from "vite-plugin-vue-setup-extend";
  13. // https://vitejs.dev/config/
  14. export default defineConfig(({ mode }) => {
  15. // 获取环境配置文件
  16. const env = loadEnv(mode, process.cwd());
  17. const { VITE_APP_ENV } = env;
  18. return {
  19. base: VITE_APP_ENV === "production" ? "/" : "/",
  20. plugins: [
  21. vue(),
  22. createSvgIconsPlugin({
  23. // 指定需要缓存的图标文件夹
  24. iconDirs: [path.resolve(process.cwd(), "src/assets/icons/svg")],
  25. // 指定symbolId格式
  26. symbolId: "icon-[dir]-[name]",
  27. }),
  28. /* createStyleImportPlugin({
  29. resolves: [VantResolve()],
  30. }), */
  31. /* AutoImport({
  32. resolvers: [ElementPlusResolver()],
  33. }),
  34. Components({
  35. resolvers: [ElementPlusResolver()],
  36. }), */
  37. VueSetupExtend(),
  38. viteCompression({
  39. // 开启gzip模式
  40. verbose: true,
  41. disable: false,
  42. threshold: 10240 * 50,
  43. deleteOriginFile: false, // 压缩后是否删除源文件
  44. algorithm: "gzip",
  45. ext: ".gz",
  46. }),
  47. ],
  48. /* css: {
  49. preprocessorOptions: {
  50. scss: {
  51. additionalData: '@import "./src/assets/styles/ruoyi.scss";', // 全局公共样式
  52. },
  53. },
  54. }, */
  55. resolve: {
  56. alias: {
  57. // 设置 `@` 指向 `src` 目录
  58. "@": path.resolve(__dirname, "./src"),
  59. "@assets": path.resolve("src/assets"),
  60. "@comps": path.resolve("src/components"),
  61. "@utils": path.resolve("src/utils"),
  62. "@router": path.resolve("src/router"),
  63. "@store": path.resolve("src/store"),
  64. },
  65. //extensions: [".ts", ".js", ".vue", ".json", ".mjs"],
  66. extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  67. },
  68. css: {
  69. postcss: {
  70. plugins: [
  71. {
  72. postcssPlugin: "internal:charset-removal",
  73. AtRule: {
  74. charset: (atRule) => {
  75. if (atRule.name === "charset") {
  76. atRule.remove();
  77. }
  78. },
  79. },
  80. },
  81. ],
  82. },
  83. },
  84. build: {
  85. // https://blog.csdn.net/lj1530562965/article/details/122231280
  86. // 混淆器设置
  87. minify: "terser",
  88. // 不生成source map文件,默认false
  89. sourcemap: false,
  90. // 指定输出路径(相对于项目根目录),默认dist
  91. outDir: "dist",
  92. // 指定生成静态资源的存放路径,默认assets
  93. assetsDir: "assets",
  94. // chunk大小警告限制,默认500kbs
  95. chunkSizeWarningLimit: 1500,
  96. // 是否禁用css拆分(默认true),设置false时所有CSS将被提取到一个CSS文件中
  97. cssCodeSplit: true,
  98. // 简要配置
  99. terserOptions: {
  100. compress: {
  101. // 移除console
  102. drop_console: true,
  103. // 移除debugger
  104. drop_debugger: true,
  105. },
  106. // 保留类名
  107. keep_classnames: true,
  108. format: {
  109. // 移除所有的注释
  110. comments: false,
  111. },
  112. },
  113. // js、css等文件打包到不同文件夹
  114. // https://rollupjs.org/guide/en/#outputoptions-object
  115. /* rollupOptions: {
  116. output: {
  117. chunkFileNames: "assets/js/[name]-[hash].js",
  118. entryFileNames: "assets/js/[name]-[hash].js",
  119. assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
  120. }
  121. } */
  122. },
  123. server: {
  124. host: "0.0.0.0", // 默认为localhost
  125. port: 80, // 端口号
  126. open: false, // 是否自动打开浏览器
  127. proxy: {
  128. // 本地开发环境通过代理实现跨域,生产环境使用 nginx 转发
  129. "/dev-api": {
  130. target: "http://223.80.101.20:8096", // 后端服务实际地址
  131. changeOrigin: true,
  132. //rewrite: (path) => path.replace(/^\/dev-api/, ""),
  133. rewrite: (path) =>
  134. path.replace(
  135. new RegExp("^" + env.VITE_APP_BASE_API),
  136. ""
  137. ),
  138. },
  139. },
  140. },
  141. };
  142. });