|
| 1 | +# Bundle Analyzer |
| 2 | + |
| 3 | +For the next example, let's install and use a bundle analyzer to inspect the internal content of our bundles. |
| 4 | + |
| 5 | +📌 We start from sample `10-code-splitting`. |
| 6 | + |
| 7 | +# Steps to build it |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they are not already installed on your computer. |
| 12 | + |
| 13 | +> ⚠ Verify that you are running at least latest Node LTS version and npm. You can check your current version by running `node -v` and `npm -v` in a terminal/console window. Older versions may produce errors. |
| 14 | +
|
| 15 | +## Steps |
| 16 | + |
| 17 | +- We start from `10-code-splitting`. Just copy the project and install: |
| 18 | + |
| 19 | + ```bash |
| 20 | + npm install |
| 21 | + ``` |
| 22 | + |
| 23 | +- Before we proceed with this tool, here goes a brief introduction why they are so useful and widely used: |
| 24 | + |
| 25 | + > ℹ️ A bundle analyzer or visualizer is a tool that help us to understand the contents of bundles generated by our bundler. It visually displays how modules and dependecies are distributed within the bundle, showing its relationships and the size of each package, allowing us to: |
| 26 | + > |
| 27 | + > - Reduce the final bundle size. |
| 28 | + > - Detect unnecessary or too heavy dependencies. |
| 29 | + > - Improve load times and user experience. |
| 30 | + > |
| 31 | + > These tools are critical to manually increase application performance before we commit to production, and, therefore, they are usually applied over production bundles only. |
| 32 | + > |
| 33 | + > There are different libraries out there, like `rollup-plugin-visualizer`, `vite-bundle-analyzer`, etc. We will use the first one for this sample. |
| 34 | +
|
| 35 | +- Given these tools are usually plug and play, their usage is as simple as installing it first: |
| 36 | + |
| 37 | + ```bash |
| 38 | + npm install rollup-plugin-visualizer --save-dev |
| 39 | + ``` |
| 40 | + |
| 41 | +- And then, use it as a plugin in `vite.config.js`: |
| 42 | + |
| 43 | + _vite.config.js_ |
| 44 | + |
| 45 | + ```diff |
| 46 | + import { defineConfig } from "vite"; |
| 47 | + import checker from "vite-plugin-checker"; |
| 48 | + import react from "@vitejs/plugin-react"; |
| 49 | + import tailwindcss from "@tailwindcss/vite"; |
| 50 | + + import { visualizer } from "rollup-plugin-visualizer"; |
| 51 | + |
| 52 | + export default defineConfig({ |
| 53 | + plugins: [ |
| 54 | + checker({ typescript: true }), |
| 55 | + tailwindcss(), |
| 56 | + react(), |
| 57 | + + visualizer(), |
| 58 | + ], |
| 59 | + }); |
| 60 | + ``` |
0 commit comments