惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
The Cloudflare Blog
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
T
The Blog of Author Tim Ferriss
D
Docker
L
LangChain Blog
Vercel News
Vercel News
C
Check Point Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
人人都是产品经理
人人都是产品经理

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
ReactJs Performance ~ Tree Shaking and Bundle Analysis ~
Ogasawara Ka · 2026-04-30 · via DEV Community

Reduce Your Bundle Size with Effective Tree Shaking

Modern JavaScript bundlers are smart—but they’re not mind readers. If you want smaller production bundles, you need to structure your code in a way that makes unused code easy to remove.

What actually makes tree shaking work?

Tree shaking relies on static analysis. That means your code has to be predictable and explicit so the bundler can safely eliminate what’s not used.

Here are the fundamentals:

1. Stick to ES Modules

Use import / export syntax so the bundler can analyze dependencies at build time.

import { fetchData } from "./api";

export const load = () => fetchData();

Enter fullscreen mode Exit fullscreen mode

Avoid CommonJS where possible:

const { fetchData } = require("./api");

Enter fullscreen mode Exit fullscreen mode

This pattern makes it harder for bundlers to optimize.

  1. Be precise with imports

Pulling in an entire library when you only need one function is one of the fastest ways to bloat your bundle.

// ❌ Pulls in everything
import _ from "lodash";

const result = _.debounce(fn, 300);
// ✅ Only what you need
import { debounce } from "lodash-es";

const result = debounce(fn, 300);
// ✅ Even more direct
import debounce from "lodash-es/debounce";

const result = debounce(fn, 300);

Enter fullscreen mode Exit fullscreen mode

  1. Declare side effects explicitly

Bundlers avoid removing files that might cause side effects. You can help them by clarifying this in package.json:

{
  "sideEffects": false
}

Enter fullscreen mode Exit fullscreen mode

⚠️ Be careful: if your code includes things like global styles, polyfills, or initialization logic, marking everything as side-effect-free can break your app.

  1. Prefer named exports in reusable code

Named exports give bundlers clearer signals about what’s being used.

export const Card = () => {};
export const Tooltip = () => {};

Enter fullscreen mode Exit fullscreen mode

Then import only what you need:

import { Card } from "@/components/ui";

Enter fullscreen mode Exit fullscreen mode

Don’t guess—inspect your bundle

Instead of assuming optimizations are working, analyze the output.

Install analyzer

npm install --save-dev webpack-bundle-analyzer

Enter fullscreen mode Exit fullscreen mode

Example setup

const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");

module.exports = {
  plugins: [
    new BundleAnalyzerPlugin({
      analyzerMode: "static",
      openAnalyzer: false,
      reportFilename: "report.html",
    }),
  ],
};

Enter fullscreen mode Exit fullscreen mode

Build and review

npm run build

Enter fullscreen mode Exit fullscreen mode

Open the generated report and look for unexpectedly large modules.

Common hidden bundle killers

Here are issues that often show up in real projects:

・Heavy date libraries used for simple formatting
・Importing entire icon packs instead of individual icons
・Duplicate dependencies caused by version mismatches
・Legacy browser polyfills that aren’t needed anymore
・Source maps accidentally included in production builds

Practical improvements
Replace heavy utilities

// Instead of a large library
import { format } from "date-fns";


const label = format(new Date(), "yyyy-MM-dd");

Enter fullscreen mode Exit fullscreen mode

Limit icon imports

// ❌ Loads everything
import * as Icons from "lucide-react";
// ✅ Only what you use
import { Search, User } from "lucide-react";

Enter fullscreen mode Exit fullscreen mode

Final thoughts

Tree shaking isn’t automatic magic—it’s a collaboration between your code and your tooling.

If you:

・write explicit imports
・structure exports clearly
・and verify your bundle output

you’ll consistently ship smaller, faster applications.