Scripts2026年4月12日·1 分钟阅读

Babel — The JavaScript Compiler for Next Generation Code

Babel is a JavaScript compiler that lets you use the latest ECMAScript features today by transforming modern syntax into backwards-compatible code. It powers JSX transformation, TypeScript stripping, and polyfill injection across the web ecosystem.

SC
Script Depot · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

# Install core packages
npm install -D @babel/core @babel/cli @babel/preset-env

# Compile a file
npx babel src/app.js --out-dir dist

# With React JSX support
npm install -D @babel/preset-react
npx babel src/ --out-dir dist --presets=@babel/preset-env,@babel/preset-react

Introduction

Babel is the foundational JavaScript compiler that enabled the modern web. When browsers lagged behind the ECMAScript specification, Babel let developers use arrow functions, classes, destructuring, async/await, and other modern features years before browsers supported them.

With 44,000+ GitHub stars, Babel has been central to the JavaScript ecosystem since 2014. It compiles JSX for React, strips TypeScript types, and transforms cutting-edge proposals into code that runs in any browser. While newer tools like SWC and esbuild offer faster compilation, Babel remains essential for its unmatched plugin ecosystem and transformation flexibility.

What Babel Does

Babel takes JavaScript code written with modern or proposed syntax and transforms it into equivalent code that older environments can execute. It works in three stages: parsing code into an AST, transforming the AST using plugins, and generating output code from the modified AST.

Architecture Overview

[Source Code (ES2024+, JSX, TS)]
          |
     [Parser (@babel/parser)]
     Converts to AST
          |
     [Transform Plugins]
     Each plugin modifies
     specific AST nodes:
     - arrow-functions
     - optional-chaining
     - jsx-transform
     - typescript
          |
     [Code Generator (@babel/generator)]
     AST back to JavaScript
          |
[Output (ES5/ES6 compatible code)]

Self-Hosting & Configuration

// babel.config.json
{
  "presets": [
    ["@babel/preset-env", {
      "targets": "> 0.25%, not dead",
      "useBuiltIns": "usage",
      "corejs": 3
    }],
    ["@babel/preset-react", {
      "runtime": "automatic"
    }],
    "@babel/preset-typescript"
  ],
  "plugins": [
    "@babel/plugin-proposal-decorators"
  ]
}

Key Features

  • Syntax Transformation — use latest JS features in any environment
  • JSX Compilation — transforms React JSX into JavaScript function calls
  • TypeScript Support — strips type annotations (type checking via tsc)
  • Polyfill Injection — automatically adds polyfills based on browser targets
  • Plugin System — hundreds of plugins for every JS proposal and transformation
  • Preset Bundles — curated plugin collections like preset-env and preset-react
  • Source Maps — accurate debugging with generated source maps
  • Macro System — compile-time code transformation via babel-plugin-macros

Comparison with Similar Tools

Feature Babel SWC esbuild TypeScript (tsc) Biome
Language JavaScript Rust Go TypeScript Rust
Speed Moderate 20x faster 100x faster Slow Very Fast
Plugin Ecosystem Very Rich Growing Limited N/A Growing
JSX Support Full Full Full Full N/A
Type Checking No No No Yes No
Custom Transforms Unlimited Limited No Limited Limited
Maturity 10+ years 4 years 5 years 12+ years 2 years

FAQ

Q: Is Babel still needed with modern browsers? A: For many projects targeting modern browsers, Babel is less critical for syntax transforms. However, it remains essential for JSX compilation, experimental proposals (decorators, pipeline operator), and projects needing broad browser support.

Q: Should I switch from Babel to SWC or esbuild? A: If your Babel config is simple (preset-env + preset-react), switching to SWC or esbuild can dramatically speed up builds. If you rely on custom Babel plugins, you may need to keep Babel for those specific transforms.

Q: What is the difference between Babel and TypeScript compiler? A: TypeScript compiler (tsc) does both type checking and compilation. Babel only strips types without checking them — use tsc for type checking and Babel for faster compilation. Many projects use both.

Q: How does preset-env work? A: preset-env reads your browserslist target (e.g., "> 0.25%, not dead") and only includes the transformations needed for those browsers, keeping output as small and modern as possible.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产