Przejdź do treści głównej
← ⚡ JavaScript & the browser·Moduł C3 · Lekcja 5
ZadanieInline approximation: declare an inline 'utils' object with three methods (add, sub, mul). Use a NAMED-import-style destructure to pull only 'add' out of utils. Log add(2,3) — expect 5.

Potrząsanie drzewami: dlaczego import nazwany przewyższa import przestrzeni nazw

75 XP7 min
Teoria

What tree-shaking does

A bundler walks your import graph and drops any export your code doesn't reference. The "tree" is your module graph; the "shake" is unused leaves falling off. End result: smaller bundles.

What kills tree-shaking

import * as lodash from "lodash";    // imports ALL of lodash, no shake
lodash.debounce(fn, 100);

vs.

import { debounce } from "lodash";   // bundler can drop everything else
debounce(fn, 100);

The namespace import import * as X forces the bundler to keep every exported symbol — you might reference X.anything at runtime, it can't know.

The other killer: side effects

// some-pkg/index.js
import "./polyfill.js";    // side-effect import — runs the file even if you import nothing

If a package's entrypoint runs code at import time, that code stays in your bundle even if you only used one export. package.json has "sideEffects": false to declare a library is side-effect-free; bundlers honour it.

Two practical rules

  1. Always use named imports unless the library forces a namespace.
  2. Prefer libraries that mark "sideEffects": false and only use ESM.

In 2026 nearly every modern library does this (lodash-es, date-fns, ramda). It's CommonJS-only libraries (the older Node-style module.exports) that still bloat bundles by default.

🔒

Zarejestruj się, aby zacząć kodować

Teoria jest dostępna dla każdego. Edytor, podgląd na żywo i check odblokowują się z 7-dniowym darmowym okresem próbnym — wymagana karta, anuluj w dowolnym momencie.

Zarejestruj się — darmowy okres próbny →

Pierwsze 10 lekcji w każdej ścieżce jest darmowych. Karta nie jest wymagana.

← PoprzedniaNastępna lekcja →

Codziennie jedna wskazówka Python lub web — na maila

Krótko, ręcznie pisane, bez spamu. Wypisanie w jedno kliknięcie.