Build Your Own Babel Configuration from Scratch
When setting up Babel in modern JavaScript projects, many developers resort to copying configurations from online examples without fully understanding what each option does. This often leads to issues like white screens on older mobile browsers or unnecessarily large bundle sizes.
To avoid these pitfalls, it's essential to understand how Babel works under the hood—particularly how it handles syntax transformation and polyfill injection. This guide walks through configuring Babel manually by explaining key packages and configuration options.
Core Dependencies Overview
Before diving into configuration, ensure you understand these core packages:
@babel/core: The main engine that transforms code.@babel/cli: Command-line interface for running Babel.@babel/preset-env: Smart preset that transforms modern JS based on target environments.core-js: Polyfill library providing ES6+ APIs mising in older runtimes.@babel/runtimeand@babel/plugin-transform-runtime: Reduce duplication of helper functions and enable non-global polyfills.
Understanding core-js
core-js provides polyfills for missing ES features. Its structure includes:
es/: Stable ECMAScript features only.proposals/: Experimental stage proposals.stable: All stable features (recommended for most use cases).
You can import specific modules (e.g., import 'core-js/es/promise') or entire categories depending on your needs.
Configuring @babel/preset-env
This preset handles two main tasks:
- Transforms modern syntax (e.g., arrow functions, classes) to compatible equivalents.
- Optionally injects polyfills via
core-js, based on usage and target environments.
Key configuration options include:
modules
Controls module system conversion. Setting modules: false preserves ES modules, enabling better tree-shaking with bundlers like Webpack.
targets
Specifies which environments your code must support (e.g., { chrome: 80 } or "ie 11"). Babel uses this to minimize unnecessary transformations and polyfills. If omitted, Babel defaults to supporting the oldest possible targets, bloating output.
useBuiltIns
Determines how polyfills are injected:
'entry': Requires manual import (e.g.,import 'core-js/stable'). Babel then replaces this with only the necessary polyfills for yourtargets.'usage': Automatically imports polyfills only when your code uses unsupported features.false(default): No automatic polyfill handling.
corejs
Required when useBuiltIns is not false. Specify the version (preferably full version like "3.27.2") and whether to include proposal-stage features:
{
corejs: {
version: '3.27.2',
proposals: true
}
}
Handling Third-Party Libraries
When using useBuiltIns: 'usage', note that Babel only scans your source code—not dependencies. If a third-party library uses unsupported APIs (e.g., Promise in IE11), those won’t be polyfilled automatically. In such cases, consider switching to 'entry' mode or manually importing required polyfills.
Non-Global Polyfills with @babel/plugin-transform-runtime
By default, @babel/preset-env injects polyfills globally (e.g., modifying Array.prototype). For library authors who want to avoid polluting the global scope, @babel/plugin-transform-runtime offers an alternative.
Key options:
helpers: Replaces inline Babel helpers with imports from@babel/runtime.regenerator: Handles async/await and generator functions viaregenerator-runtime.corejs: Enables non-global polyfills:false(default): No polyfills.2: Limited to global/static features (deprecated).3: Full support including instance methods (e.g.,[].includes).
Example configuration for a libray:
module.exports = {
presets: [
['@babel/preset-env', { modules: false }]
],
plugins: [
['@babel/plugin-transform-runtime', {
corejs: { version: 3, proposals: true }
}]
]
};
Note: When using this plugin, set targets at the top level of your config or in package.json (not inside @babel/preset-env), as the plugin respects browser compatibility data independently.
Recommended Configurations
For Applications
// babel.config.js
module.exports = {
presets: [
['@babel/preset-env', {
modules: false,
useBuiltIns: 'entry',
corejs: { version: '3.27.2', proposals: true }
}]
],
plugins: ['@babel/plugin-transform-runtime']
};
// package.json
{
"browserslist": ["ie 11"]
}
// entry.js
import 'core-js/stable'; // or 'core-js' for proposals
For Libraries
// babel.config.js
module.exports = {
presets: [
['@babel/preset-env', { modules: false }]
],
plugins: [
['@babel/plugin-transform-runtime', {
corejs: { version: 3, proposals: true }
}]
]
};
// package.json
{
"browserslist": ["ie 11"]
}
Avoid mixing global (useBuiltIns) and non-global (transform-runtime with corejs) polyfill strategies—they can conflict and cause unexpected behavior.