Babel

Babel is a popular JavaScript compiler that allows you to write modern JavaScript while ensuring compatibility with older browsers by transforming your code.

Extension.js supports Babel for transforming your JavaScript code, making it easier to use the latest JavaScript features in your extensions.

Starter Babel Template

Extension.js includes a New Tab Babel template, which provides an out-of-the-box configuration to integrate Babel into your extension.

Babel Extension Template

Try it yourself

npm
pnpm
yarn
npx extension@latest create my-extension --template=new-babel

Usage With An Existing Extension

You can add Babel to your existing Extension.js project by installing the necessary dependencies and configuring the Babel environment.

Installation

Install Babel and its required dependencies:

npm
pnpm
yarn
npx install -D @babel/core @babel/preset-env babel-loader

Babel Configuration

Create a Babel configuration file (babel.config.json) at the root of your project. This file will define the transformation rules for your JavaScript code.

{
  "presets": ["@babel/preset-env"]
}

This basic configuration ensures that your JavaScript will be transpiled to a version that is compatible with a wide range of browsers, based on your target environments.

Example Usage in a JavaScript File

Once Babel is set up, you can start writing modern JavaScript. Here's an example of using ES6 features in your code:

./NewTabApp.jsx

const helloWorld = () => {
  console.log("Hello, Extension!");
};

helloWorld();

Babel will transform this modern syntax to ensure compatibility with older browsers.

Example Babel Configuration for More Advanced Usage

Babel allows you to extend the configuration to use plugins and more advanced features:

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["@babel/plugin-proposal-class-properties", { "loose": true }],
    ["@babel/plugin-transform-runtime"]
  ]
}

This configuration adds support for class properties and runtime transformations, allowing you to use additional JavaScript features.

Next Steps

  • Ensure your codebase is consistent by using ESLint with Babel.