JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstooling
PrevNext

Learn the concept

Transpilation with Babel & SWC

tooling
senior
transpilation

How does Babel work and how do you configure it?

babel
transpilation
presets
plugins
Quick Answer

Babel transforms modern JavaScript to compatible versions using plugins (individual transforms) and presets (plugin collections). Configure via babel.config.js with presets like @babel/preset-env for automatic browser targeting and plugins for specific features.

Detailed Explanation

Babel Pipeline:

  1. Parse code to AST
  2. Transform AST with plugins
  3. Generate output code

Configuration:

  • presets: Collections of plugins
  • plugins: Individual transforms
  • targets: Browser/Node targets
  • env: Environment-specific config

Common Presets:

  • @babel/preset-env: Modern JS
  • @babel/preset-react: JSX
  • @babel/preset-typescript: TS

Key Concepts:

  • Polyfills: Runtime features
  • core-js: Polyfill library
  • useBuiltIns: Polyfill injection

Code Examples

Babel configurationJavaScript
// babel.config.js
module.exports = {
  presets: [
    ['@babel/preset-env', {
      // Target browsers
      targets: {
        browsers: ['>0.25%', 'not dead'],
        // Or use browserslist in package.json
      },
      // Polyfill configuration
      useBuiltIns: 'usage', // Auto-inject needed polyfills
      corejs: 3,
      // Module output
      modules: false, // Keep ES modules for tree shaking
    }],
    ['@babel/preset-react', {
      runtime: 'automatic', // React 17+ JSX transform
    }],
    '@babel/preset-typescript',
  ],
  plugins: [
    // Decorators (Stage 3)
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    // Class properties
    '@babel/plugin-proposal-class-properties',
    // Optional chaining (if not in preset-env targets)
    '@babel/plugin-proposal-optional-chaining',
  ],
  env: {
    test: {
      presets: [
        ['@babel/preset-env', { targets: { node: 'current' } }],
      ],
    },
    production: {
      plugins: [
        'transform-remove-console',
      ],
    },
  },
};

Real-World Applications

Use Cases

Ensuring a modern React application can run on older browsers (e.g., IE11 for enterprise clients) without sacrificing developer experience or writing legacy code

Configuring Babel with `@babel/preset-env` to transpile modern JavaScript syntax (ES2020+) down to ES5 based on browser compatibility targets, and using `core-js` with `useBuiltIns: 'usage'` for efficient polyfilling of new JavaScript features.

Integrating TypeScript and JSX into a JavaScript project's build process, while also enabling experimental language features

Combining `@babel/preset-typescript` and `@babel/preset-react` in Babel's configuration to handle both TypeScript compilation and JSX transformation, alongside plugins for Stage 3 ECMAScript proposals like optional chaining or nullish coalescing.

Optimizing a large frontend application for production by removing development-only code and applying micro-optimizations

Using environment-specific Babel configurations (e.g., `env.production`) to apply plugins like `transform-remove-console` or custom plugins that inline constant values, reducing bundle size and improving runtime performance.

Mini Projects

Babel Config for Legacy Browser Support

intermediate

Create a simple JavaScript project using modern ESNext features. Configure Babel with `@babel/preset-env` to transpile the code for a target browser (e.g., IE11), demonstrating how to ensure compatibility with older environments.

Custom Babel Plugin for Logging

advanced

Develop a custom Babel plugin that identifies and replaces calls to a specific logging function with another (e.g., changing `log.info` to `console.log`), showcasing how to create custom code transformations.

Industry Examples

Meta (React and Flow)

Meta extensively uses Babel for transforming its JavaScript code, especially with React and Flow (their static type checker). They leverage Babel's plugin architecture to support experimental features, optimize code for different environments, and maintain their large-scale applications.

Next.js

Next.js relies on Babel for JavaScript transpilation, enabling developers to use the latest ECMAScript features and TypeScript. Its configurations are optimized for fast development and efficient production builds, supporting features like fast refresh and server-side rendering.

Resources

Babel Documentation

docs

Babel Plugin Handbook

docs

Related Questions

How does Webpack work and what are its core concepts?

mid
bundlers

How do you configure TypeScript for a project?

mid
typescript
Previous
How do you create custom ESLint rules and plugins?
Next
How do you containerize a JavaScript application with Docker?
PrevNext