@babel/standalone

@babel/standalone provides a standalone build of Babel for use in browsers and other non-Node.js environments.

When (not) to use @babel/standalone

If you’re using Babel in production, you should normally not use @babel/standalone. Instead, you should use a build system running on Node.js, such as Webpack, Rollup, or Parcel, to transpile your JS ahead of time.

However, there are some valid use cases for @babel/standalone:

  • It provides an easy, convenient way to prototype with Babel. Using @babel/standalone, you can get started using Babel with just a simple script tag in your HTML.
  • Sites that compile user-provided JavaScript in real-time, like JSFiddle, JS Bin, the REPL on the Babel site, JSitor, etc.
  • Apps that embed a JavaScript engine such as V8 directly, and want to use Babel for compilation
  • Apps that want to use JavaScript as a scripting language for extending the app itself, including all the goodies that ES2015 provides.
  • Other non-Node.js environments (ReactJS.NET, ruby-babel-transpiler, php-babel-transpiler, etc).

Installation

There are several ways to get a copy of @babel/standalone. Pick whichever one you like:

Script Tags

When loaded in a browser, @babel/standalone will automatically compile and execute all script tags with type text/babel or text/jsx:

  1. <div id="output"></div>
  2. <!-- Load Babel -->
  3. <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  4. <!-- Your custom script here -->
  5. <script type="text/babel">
  6. const getMessage = () => "Hello World";
  7. document.getElementById("output").innerHTML = getMessage();
  8. </script>

If you want to use your browser’s native support for ES Modules, you’d normally need to set a type="module" attribute on your script tag.

Added in: v7.10.0

With @babel/standalone, set a data-type="module" attribute instead, like this:

  1. <script type="text/babel" data-type="module">

You can use the data-plugins and data-presets attributes to specify the Babel plugins/presets to use:

  1. <script type="text/babel" data-presets="env,stage-3">

Loading external scripts via src attribute is supported too:

  1. <script type="text/babel" src="foo.js"></script>

You can also set the async attribute for external scripts.

  1. <script type="text/babel" src="foo.js" async></script>

API

Load babel.js or babel.min.js in your environment. This will expose Babel’s API in a Babel object:

  1. var input = 'const getMessage = () => "Hello World";';
  2. var output = Babel.transform(input, { presets: ["env"] }).code;

Note that config files don’t work in @babel/standalone, as no file system access is available. The presets and/or plugins to use must be specified in the options passed to Babel.transform.

Customization

custom plugins

Custom plugins and presets can be added using the registerPlugin and registerPreset methods respectively:

  1. // Simple plugin that converts every identifier to "LOL"
  2. function lolizer() {
  3. return {
  4. visitor: {
  5. Identifier(path) {
  6. path.node.name = "LOL";
  7. },
  8. },
  9. };
  10. }
  11. Babel.registerPlugin("lolizer", lolizer);

Once registered, you can either use the custom plugin in an inline script:

  1. <script type="text/babel" data-plugins="lolizer">

Or you can use the plugin with Babel.transform:

  1. var output = Babel.transform("function helloWorld() { alert(hello); }", {
  2. plugins: ["lolizer"],
  3. });
  4. // Returns "function LOL() { LOL(LOL); }"

custom presets: passing options to built-in presets/plugins

If you want to pass options to builtin plugins and presets, you can create a new preset and pass these options inside the preset.

  1. // Define a preset
  2. Babel.registerPreset("env-plus", {
  3. presets: [[Babel.availablePresets["env"], { loose: true }]],
  4. plugins: [
  5. [
  6. Babel.availablePlugins["proposal-decorators"],
  7. { decoratorsBeforeExport: true },
  8. ],
  9. ],
  10. });

Once registered, you can use this preset in an inline script:

  1. <script type="text/babel" data-presets="env-plus">