Ember CLI Babel Transpilation
ember-cli-babel is the official Ember CLI addon responsible for integrating Babel into Ember applications and addons. It transpiles modern JavaScript (ESNext) down to a syntax supported by target browsers, leveraging `@babel/preset-env` and respecting the `config/targets.js` file. The current stable version is 8.3.1, with releases occurring as needed to align with Babel updates, Ember CLI changes, and bug fixes. Key differentiators include its deep integration with the Ember CLI build pipeline, automatic polyfill management (though `includePolyfill` was removed in v8), and support for advanced features like static class blocks and TypeScript transpilation. It provides a robust, zero-config-by-default setup while offering extensive customization options through the `ember-cli-build.js` file for both Babel presets/plugins and `ember-cli-babel`'s internal behavior, making it indispensable for modern Ember development.
Common errors
-
Error: The currently active Node.js version (v14.x) is not supported by ember-cli-babel. Please upgrade to Node.js v16, v18, or v20 or higher.
cause Using an unsupported Node.js version with `ember-cli-babel` v8 or newer.fixUpgrade your Node.js environment to a supported version (e.g., `nvm install 18` and `nvm use 18`). -
Error: .babelrc files are ignored by ember-cli-babel. Please configure Babel options in ember-cli-build.js instead.
cause Attempting to configure Babel using a `.babelrc` file instead of `ember-cli-build.js`.fixRemove your `.babelrc` file and transfer all Babel configurations to the `babel` object within your `new EmberApp()` constructor in `ember-cli-build.js`. -
ReferenceError: regeneratorRuntime is not defined
cause The `transform-regenerator` plugin was explicitly excluded from Babel, and no global polyfill for generator functions is being provided.fixEither remove `transform-regenerator` from the `exclude` list in your Babel configuration, or ensure that a polyfill like `core-js/stable/regenerator-runtime` is explicitly imported in your application.
Warnings
- breaking `ember-cli-babel` v8.0.0 dropped support for Node.js v14. The minimum supported Node.js version is now v16, v18, or v20+.
- breaking The `includePolyfill` option was removed in `ember-cli-babel` v8.0.0. Projects relying on this option for automatic polyfill inclusion will need to update their strategy.
- breaking `ember-cli-babel` v8.0.0 updated its internal dependency `broccoli-babel-transpiler` to v8, which may introduce breaking changes or require adjustments to advanced configurations.
- gotcha By default, `ember-cli-babel` ignores `.babelrc` files. All Babel configurations must be specified within the `babel` object in your `ember-cli-build.js` or addon's `index.js`.
- gotcha With Babel's evolution, `@babel/plugin-proposal-*` packages have largely been replaced by `@babel/plugin-transform-*` counterparts. While `ember-cli-babel` adapts, directly configured plugins might need updates.
Install
-
npm install ember-cli-babel -
yarn add ember-cli-babel -
pnpm add ember-cli-babel
Imports
- babel (configuration object)
Directly creating a .babelrc file (it is ignored by default)
new EmberApp(defaults, { babel: { /* preset-env and plugin options */ } }) - ember-cli-babel (configuration object)
Setting 'ember-cli-babel' specific options directly under the 'babel' key.
new EmberApp(defaults, { 'ember-cli-babel': { /* addon-specific options */ } }) - Custom Babel Plugins
plugins: ['@babel/plugin-proposal-decorators'] (without require.resolve, which might cause path resolution issues)
plugins: [require.resolve('@babel/plugin-proposal-decorators')]
Quickstart
/* eslint-env node */
'use strict';
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function (defaults) {
let app = new EmberApp(defaults, {
// Configure Babel transpilation options
babel: {
// Enable "loose" mode for class properties for smaller output and compatibility.
loose: true,
// Exclude a specific transform if not needed or handled by other means.
exclude: [
'transform-regenerator'
],
// Add custom Babel plugins, using require.resolve for robust path resolution.
plugins: [
require.resolve('@babel/plugin-proposal-decorators') // Common in Ember for decorators
]
},
// Configure ember-cli-babel specific options
'ember-cli-babel': {
// Disable debug tooling support for production builds to optimize size.
disableDebugTooling: EmberApp.env() === 'production',
// Enable TypeScript transpilation if your project uses TypeScript.
// Note: This handles syntax transpilation, not type checking.
enableTypeScriptTransform: true,
// Specify additional file extensions for Babel to process.
extensions: ['js', 'ts', 'gjs', 'gts']
}
// Other EmberApp configurations go here
});
return app.toTree();
};