Skip to content

Commit 327b87c

Browse files
committed
complete sample
1 parent 911fb03 commit 327b87c

1 file changed

Lines changed: 59 additions & 7 deletions

File tree

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

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

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,67 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
154154

155155
⚠️ Close used terminal in windows to 'unset' env variable.
156156

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:
157+
- Now, let's do a simple exercise, let's copy paste `math.ts` module as `math2.ts`, import it dynamically from `hello.tsx` component by duplicating the button and the handler:
158158

159-
_src/index.tsx_
159+
_src/hello.tsx_
160160

161161
```diff
162-
import "./styles.css";
163-
+ import { operate } from "./math-duplicated";
162+
const applyOperation = async () => {
163+
const { operate } = await import("./math");
164+
setCounter(prevCounter => operate(prevCounter));
165+
};
166+
167+
+ const applyOperation2 = async () => {
168+
+ const { operate } = await import("./math2");
169+
+ setCounter(prevCounter => operate(prevCounter));
170+
+ };
171+
172+
return (
173+
<>
174+
<h2>Hello from React</h2>
175+
<p>Api server is {config.API_BASE}</p>
176+
<p>Feature A is {config.IS_FEATURE_A_ENABLED ? "enabled" : "disabled"}</p>
177+
<p>Counter state: {counter}</p>
178+
<button
179+
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
180+
onClick={applyOperation}
181+
>
182+
Apply operation
183+
</button>
184+
+ <button
185+
+ className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
186+
+ onClick={applyOperation2}
187+
+ >
188+
+ Apply operation 2
189+
+ </button>
190+
```
191+
192+
- Now install a new library called 'loglevel':
193+
194+
```bash
195+
npm install loglevel
196+
```
197+
198+
- And let's use it in both math modules:
199+
200+
_src/math.tsx_
201+
202+
```diff
203+
+ import log from "loglevel";
204+
205+
+ log.warn("*** Executing lazy-loaded math chunk");
206+
207+
const randomBetween = (min: number, max: number) =>
208+
```
164209

165-
+ console.log("***", operate(43));
210+
_src/math2.tsx_
166211

167-
const root = createRoot(document.getElementById("root"));
212+
```diff
213+
+ import log from "loglevel";
214+
215+
+ log.warn("*** Executing lazy-loaded math2 chunk");
216+
217+
const randomBetween = (min: number, max: number) =>
168218
```
169219

170220
- Run again the build in a clean terminal:
@@ -173,4 +223,6 @@ Install [Node.js and npm](https://nodejs.org/en/) (20.19.0 || >=22.12.0) if they
173223
npm run build
174224
```
175225

176-
- 🔎 Now check the stats again and see how our latest build is compared against the baseline, offering differences in size, etc.
226+
- 🔎 Now check the stats again and see how our latest build is compared against the baseline, offering differences in a bunch of stats, mainly size, which allow us to compare any improvement or decline in optimization.
227+
228+
- 🤯 It is specially worth mentioning that we won't see any stat related to duplicated code or duplicated modules. We would expect our library `loglevel` to be included in both chunks `math` and `math2`. However, vite is smart enough to avoid duplicates as much as possible by extracting this common library to a separate bundle. Amazing!

0 commit comments

Comments
 (0)