How to get the total bundle size when building with vite?

Member

by brandy , in category: Javascript , 8 months ago

How to get the total bundle size when building with vite?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , 8 months ago

@brandy 

To get the total bundle size when building with Vite, you can use the size-limit plugin.


Here's how you can do it:

  1. Install the size-limit plugin by running the following command:
1
npm install -D size-limit


  1. Add a size-limit configuration in your package.json file:
1
2
3
4
5
6
"size-limit": [
  {
    "path": "dist/main.js",
    "limit": "200 KB"
  }
]


Replace dist/main.js with the path to your main JavaScript bundle file.

  1. Add a script to run the size-limit check:
1
2
3
"scripts": {
  "size-limit": "size-limit"
}


  1. Run the script to get the total bundle size:
1
npm run size-limit


This will analyze the size of your bundle and let you know if it exceeds the specified limit.