Babel Plugin: Ember Modules API Polyfill

3.5.0 · active · verified Wed Apr 22

babel-plugin-ember-modules-api-polyfill is a Babel plugin designed to facilitate the adoption of the modern JavaScript Modules API (RFC #176) by Ember addon authors. It achieves this by transpiling new module import statements (e.g., `import { inject } from "@ember/service"`) back into their equivalent legacy global object references (e.g., `const inject = Ember.inject.service`) during the build process. This mechanism ensures that addons can leverage contemporary Ember API patterns without sacrificing compatibility with older Ember applications that have not yet fully migrated. Currently at version 3.5.0, the plugin maintains an active release cadence, frequently incorporating bug fixes and enhancements, often driven by updates to the underlying `ember-rfc176-data` package and evolving Ember API requirements. Its primary role is to provide a smooth transition path for Ember applications from a global object dependency model to a more modularized architecture.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `babel-plugin-ember-modules-api-polyfill` in a `babel.config.js` file and provides an example Ember component utilizing modern module imports that the plugin will transpile to legacy global API access.

// babel.config.js
module.exports = {
  plugins: [
    ['babel-plugin-ember-modules-api-polyfill', {
      // Recommended option for Ember 3.27+ to avoid global window.Ember deprecation
      useEmberModule: true
    }]
  ]
};

// src/my-component.js (Example input code)
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

export default class MyComponent extends Component {
  @service auth;
  @tracked count = 0;

  @action
  increment() {
    this.count++;
    console.log(`Current count: ${this.count}`);
  }

  constructor() {
    super(...arguments);
    console.log('Auth service injected:', this.auth);
  }
}

// To apply the transformation with Babel CLI (assuming babel-cli is installed):
// npx babel src/my-component.js --out-file dist/my-component.js --config-file babel.config.js
// The output in dist/my-component.js would contain code using Ember.Component, Ember.inject.service, etc.

view raw JSON →