<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kai on Medium]]></title>
        <description><![CDATA[Stories by Kai on Medium]]></description>
        <link>https://medium.com/@ithanht?source=rss-4e6e143720b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EDTSqMpyA_kqtMW5JTcNUg.jpeg</url>
            <title>Stories by Kai on Medium</title>
            <link>https://medium.com/@ithanht?source=rss-4e6e143720b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 22:44:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ithanht/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How third-party libraries like React, lodash, or date-fns end up in your app’s bundle, and how to…]]></title>
            <link>https://medium.com/@ithanht/how-third-party-libraries-like-react-lodash-or-date-fns-end-up-in-your-apps-bundle-and-how-to-6475a3e04be6?source=rss-4e6e143720b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/6475a3e04be6</guid>
            <category><![CDATA[web-performance]]></category>
            <category><![CDATA[vites]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[rollup]]></category>
            <dc:creator><![CDATA[Kai]]></dc:creator>
            <pubDate>Sat, 24 Oct 2026 14:10:22 GMT</pubDate>
            <atom:updated>2026-10-24T14:10:22.886Z</atom:updated>
            <content:encoded><![CDATA[<h3>How third-party libraries like React, lodash, or date-fns end up in your app’s bundle, and how to handle them well.</h3><h3>How dependencies get bundled</h3><p><strong>Default behavior:</strong> your bundler follows every import into node_modules, resolves the package’s entry point (via exports, then module, then main), and includes whatever it reaches in the module graph. Then tree-shaking and chunking apply as usual.</p><p>Key points that determine the outcome:</p><ul><li><strong>Format matters.</strong> ESM can be statically analyzed and tree-shaken; CommonJS mostly can’t, so a CJS library tends to come in whole.</li><li><strong>sideEffects flag.</strong> If a package declares &quot;sideEffects&quot;: false, the bundler can drop any imports you don’t use. Without it, the bundler must keep the module in case importing it does something.</li><li><strong>Dev vs. prod.</strong> In dev, Vite pre-bundles dependencies with esbuild (converting CJS to ESM and collapsing many small files into one) so the browser doesn’t make hundreds of requests. For production, Rollup bundles them with your code.</li><li><strong>Where they land.</strong> Dependencies used by the entry go in the main chunk. Ones reached only from a lazy import go in that async chunk. Ones shared across chunks may be hoisted into a shared chunk.</li></ul><h3>Best practices</h3><h4>1. Import precisely</h4><pre>// Bad: pulls in all of lodash (CJS, not shakeable)<br>import _ from &quot;lodash&quot;;<br><br>// Good: ESM build, tree-shakeable<br>import { debounce } from &quot;lodash-es&quot;;<br><br>// Also good: per-method path<br>import debounce from &quot;lodash/debounce&quot;;</pre><p>Watch for libraries with huge barrel exports (icon packs, UI kits). Some are fine with modern bundlers, but verify in the analyzer. If a single icon drags in hundreds, switch to direct path imports or the library’s recommended setup.</p><h4>2. Measure before and after adding a dependency</h4><p>Check bundlephobia (or pkg-size.dev) before installing, then confirm in your own build with rollup-plugin-visualizer, webpack-bundle-analyzer, or vite-bundle-visualizer. Look at <strong>gzipped/brotli size</strong>, not raw size, and at what it adds to the <em>initial</em> load.</p><h4>3. Lazy-load heavy, rarely-used libraries</h4><p>Charting, rich text editors, PDF viewers, syntax highlighters, and maps are classic candidates. Load them on interaction or on the route that needs them:</p><pre>async function exportPdf() {<br>  const { jsPDF } = await import(&quot;jspdf&quot;);<br>  new jsPDF().text(&quot;Hello&quot;, 10, 10).save(&quot;a.pdf&quot;);<br>}</pre><h4>4. Separate stable vendor code for caching</h4><p>Put rarely-changing packages (React, React DOM, router) in their own content-hashed chunk so returning users don’t re-download them on every deploy. Don’t create one giant vendor chunk containing everything: it forces every page to download libraries only one route uses.</p><h4>5. Prevent duplicates</h4><p>Multiple versions of the same package are a common silent cost, and for React-like singletons they cause real bugs.</p><ul><li>Look for duplicates in the analyzer.</li><li>Run npm dedupe / pnpm dedupe, or force one version with overrides (npm), resolutions (yarn), or pnpm.overrides.</li><li>In bundlers, use resolve.alias or Vite’s resolve.dedupe: [&quot;react&quot;, &quot;react-dom&quot;] for singletons.</li></ul><h4>6. Prefer smaller or native alternatives</h4><ul><li>date-fns/dayjs instead of moment (which is large and not shakeable).</li><li>Native APIs: Intl, structuredClone, fetch, URL, Array.prototype.at, and so on, instead of small utility packages.</li><li>A lighter library for the same job (e.g., a 3 KB state library rather than a 30 KB one) when features overlap.</li></ul><h4>7. Check what ships to the browser</h4><ul><li>Make sure server-only or Node-only packages don’t leak into client bundles (in RSC frameworks, keep them in server components).</li><li>Avoid packages that bundle big locale or data files unless needed. For example, import only the locales you use.</li><li>Replace build-time constants like process.env.NODE_ENV so dev-only branches get dead-code eliminated.</li></ul><h4>8. Be careful with CJS-only and side-effectful packages</h4><p>If a dependency is CJS-only, it can’t be tree-shaken, so isolate it behind a lazy import or find an ESM alternative. If it has side effects at import (polyfills, global CSS), expect it to always be included wherever imported.</p><h4>9. Use externals/CDN only deliberately</h4><p>Marking libraries as externals and loading them from a CDN can save bundle size, but you lose version control and tree-shaking, and add a third-party request and failure point. It’s mostly useful for micro-frontends or module federation, where sharing a single instance across apps is the actual goal.</p><h4>10. Keep dependencies updated and audited</h4><p>Newer versions often ship smaller ESM builds and better exports maps. Use npm audit or Dependabot/Renovate, and prune unused packages with depcheck or knip.</p><h3>A quick workflow</h3><ol><li>Build and open the analyzer.</li><li>Sort by size and find the biggest dependencies in the <strong>initial</strong> chunk.</li><li>For each, ask: needed on first paint? If not, lazy-load. Can I import less of it? Is there a lighter alternative? Is it duplicated?</li><li>Rebuild, compare, and add a size-limit budget in CI so it doesn’t regress.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6475a3e04be6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>