Skip to content

Commit 911fb03

Browse files
committed
optional analyzer
1 parent eb5e5ad commit 911fb03

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

  • 03-bundling/06-vite/11-bundle-analyzer

03-bundling/06-vite/11-bundle-analyzer/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,86 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
9191
```bash
9292
npm build
9393
```
94+
95+
## Optional
96+
97+
- Another very interesing analyzer is a rollup plugin to extract bundle statistics. It's called `rollup-plugin-bundle-stats`. Just install it:
98+
99+
```bash
100+
npm install rollup-plugin-bundle-stats --save-dev
101+
```
102+
103+
- And now let's configure in vite config file like this:
104+
105+
```diff
106+
import { defineConfig } from "vite";
107+
import checker from "vite-plugin-checker";
108+
import react from "@vitejs/plugin-react";
109+
import tailwindcss from "@tailwindcss/vite";
110+
import { analyzer } from "vite-bundle-analyzer";
111+
+ import { bundleStats } from "rollup-plugin-bundle-stats";
112+
113+
export default defineConfig({
114+
plugins: [
115+
checker({ typescript: true }),
116+
tailwindcss(),
117+
react(),
118+
analyzer({
119+
analyzerMode: "static",
120+
openAnalyzer: false,
121+
reportTitle: "Bundle Analysis",
122+
fileName: "bundle-report.html",
123+
}),
124+
+ bundleStats(),
125+
],
126+
});
127+
```
128+
129+
- Build it again:
130+
131+
```bash
132+
npm build
133+
```
134+
135+
🔎 Check new `bundle-stats.html` page and explore its powerfull features.
136+
137+
- One of the advanced features of this tool is the ability to compare our current build against a baseline build. First of all, we must indicate which run is our baseline. A simple, quick way, is to set an env variable when building our baseline build:
138+
139+
⚡ If you are using Linux/Bash, you can run:
140+
141+
```bash
142+
BUNDLE_STATS_BASELINE=true npm run build
143+
```
144+
145+
⚠️ Under Window's powershell terminal, you could first set variable for the session and then build it, like:
146+
147+
```bash
148+
$env:BUNDLE_STATS_BASELINE="true"
149+
```
150+
151+
```bash
152+
npm run build
153+
```
154+
155+
⚠️ Close used terminal in windows to 'unset' env variable.
156+
157+
- Now, let's do a simple exercise, let's copy paste `math.ts` module as `math-duplicated.ts`, import it from `index.tsx` and use its functionality to avoid vite tree-shaking. We want to include duplicated code on purpose:
158+
159+
_src/index.tsx_
160+
161+
```diff
162+
import "./styles.css";
163+
+ import { operate } from "./math-duplicated";
164+
165+
+ console.log("***", operate(43));
166+
167+
const root = createRoot(document.getElementById("root"));
168+
```
169+
170+
- Run again the build in a clean terminal:
171+
172+
```bash
173+
npm run build
174+
```
175+
176+
- 🔎 Now check the stats again and see how our latest build is compared against the baseline, offering differences in size, etc.

0 commit comments

Comments
 (0)