vite.config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { defineConfig } from "vite";
  2. import vue from "@vitejs/plugin-vue";
  3. import vueJsx from "@vitejs/plugin-vue-jsx";
  4. import legacy from "@vitejs/plugin-legacy";
  5. import { resolve } from "path";
  6. import postCssPxToRem from "postcss-pxtorem";
  7. import { visualizer } from "rollup-plugin-visualizer";
  8. import viteCompression from "vite-plugin-compression";
  9. // https://vitejs.dev/config/
  10. export default defineConfig({
  11. base: "./",
  12. resolve: {},
  13. // assetsInclude: ['**/*.html'],
  14. plugins: [
  15. // mkcert(), // 本地https
  16. // 其他插件...
  17. viteCompression({
  18. algorithm: "gzip", // 指定压缩算法为gzip,[ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
  19. ext: ".gz", // 指定压缩后的文件扩展名为.gz
  20. threshold: 10240, // 仅对文件大小大于threshold的文件进行压缩,默认为10KB
  21. deleteOriginFile: false, // 是否删除原始文件,默认为false
  22. filter: /\.(js|css|json|html|ico|svg)(\?.*)?$/i, // 匹配要压缩的文件的正则表达式,默认为匹配.js、.css、.json、.html、.ico和.svg文件
  23. compressionOptions: { level: 9 }, // 指定gzip压缩级别,默认为9(最高级别)
  24. verbose: true, //是否在控制台输出压缩结果
  25. disable: false, //是否禁用插件
  26. }),
  27. legacy({
  28. targets: ["Chrome 63"],
  29. additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
  30. modernPolyfills: true,
  31. }),
  32. vue(),
  33. vueJsx(),
  34. visualizer({ open: true }), // 自动开启分析页面
  35. ],
  36. css: {
  37. postcss: {
  38. plugins: [
  39. postCssPxToRem({
  40. rootValue: 37.5,
  41. propList: ["*"],
  42. selectorBlackList: [".norem"],
  43. }),
  44. ],
  45. },
  46. },
  47. build: {
  48. minify: 'terser', // 启用 terser 压缩
  49. terserOptions: {
  50. compress: {
  51. pure_funcs: ['console.log'], // 只删除 console.log
  52. //drop_console: true, // 删除所有 console
  53. drop_debugger: true, // 删除 debugger
  54. }
  55. },
  56. rollupOptions: {
  57. input: {
  58. instrument: resolve(__dirname, "instrument.html"),
  59. },
  60. output: {
  61. // 最小化拆分包
  62. manualChunks(id) {
  63. if (id.includes("osmd-extended")) {
  64. // 通过拆分包的方式将所有来自osmd-extended的模块打包到单独的chunk中
  65. return id
  66. .toString()
  67. .split("osmd-extended/")[1]
  68. .split("/")[0]
  69. .toString();
  70. }
  71. },
  72. chunkFileNames: "js/[name]-[hash].js", // 引入文件名的名称
  73. entryFileNames: "js/[name]-[hash].js", // 包的入口文件名称
  74. assetFileNames: "[ext]/[name]-[hash].[ext]", // 资源文件像 字体,图片等
  75. },
  76. },
  77. },
  78. server: {
  79. cors: true,
  80. port: 3000,
  81. // https: true,
  82. proxy: {
  83. "^/instrument/.*": {
  84. target: "https://test.gym.lexiaoya.cn",
  85. changeOrigin: true,
  86. rewrite: (path) => path.replace(/^\/instrument/, ""),
  87. },
  88. },
  89. },
  90. preview: {
  91. port: 3000,
  92. host: "192.168.3.114",
  93. },
  94. });
  95. // vite.config.js