You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 03-bundling/06-vite/11-bundle-analyzer/README.md
+83Lines changed: 83 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,3 +91,86 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
91
91
```bash
92
92
npm build
93
93
```
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:
0 commit comments