1. babel-standalone
Standalone build of Babel for use in non-Node.js environments. Similar to the (now deprecated) babel-browser
babel-standalone
Package: babel-standalone
Created by: Daniel15
Last modified: Mon, 13 Jun 2022 04:08:25 GMT
Version: 6.26.0
License: MIT
Downloads: 117,654
Repository: https://github.com/Daniel15/babel-standalone

Install

npm install babel-standalone
yarn add babel-standalone

babel-standalone

babel-standalone is a standalone build of Babel for use in non-Node.js environments, including browsers. It's bundled with all the standard Babel plugins and presets, and a build of babili (babel-minify) is optionally available too.

But why?!

It's true that using Babel through Webpack, Browserify or Gulp should be sufficient for most use cases. However, there are some valid use cases for babel-standalone:

  • Sites like JSFiddle, JS Bin, the REPL on the Babel site, etc. These sites compile user-provided JavaScript in real-time.
  • 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.
  • Integration of Babel into a non-Node.js environment (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:

  • Use it via UNPKG: https://unpkg.com/babel-standalone@6/babel.min.js. This is a simple way to embed it on a webpage without having to do any other setup.
  • Install via Bower: bower install babel-standalone
  • Install via NPM: npm install --save babel-standalone
  • Manually grab babel.js and/or babel.min.js from the GitHub releases page. Every release includes these files.
  • Install it via Git: You can use the repo at https://github.com/Daniel15/babel-standalone-bower to pull a prebuilt version from Git. Note that this is generally only advised for systems that must pull artifacts from Git, such as Bower.

Usage

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

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

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

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

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

 <script type="text/babel" data-presets="es2015,stage-2">

Loading external scripts via src attribute is supported too:

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

Note that .babelrc doesn'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.

Customisation

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

 // Simple plugin that converts every identifier to "LOL"
function lolizer() {
  return {
    visitor: {
      Identifier(path) {
        path.node.name = 'LOL';
      }
    }
  }
}
Babel.registerPlugin('lolizer', lolizer);

Once registered, just use the name of the plugin:

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

Custom plugins also work for inline <script>s:

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

Dependencies

babel-core: 6.26.0babel-helper-builder-react-jsx: 6.23.0babel-loader: 6.4.1babel-plugin-check-es2015-constants: 6.22.0babel-plugin-external-helpers: 6.22.0babel-plugin-inline-replace-variables: 1.3.1babel-plugin-minify-constant-folding: 0.2.0babel-plugin-minify-dead-code-elimination: 0.2.0babel-plugin-minify-empty-function: 0.0.1babel-plugin-minify-flip-comparisons: 0.2.0babel-plugin-minify-guarded-expressions: 0.2.0babel-plugin-minify-infinity: 0.2.0babel-plugin-minify-mangle-names: 0.2.0babel-plugin-minify-replace: 0.2.0babel-plugin-minify-simplify: 0.2.0babel-plugin-minify-type-constructors: 0.2.0babel-plugin-syntax-async-functions: 6.13.0babel-plugin-syntax-async-generators: 6.13.0babel-plugin-syntax-class-constructor-call: 6.18.0babel-plugin-syntax-class-properties: 6.13.0babel-plugin-syntax-decorators: 6.13.0babel-plugin-syntax-do-expressions: 6.13.0babel-plugin-syntax-exponentiation-operator: 6.13.0babel-plugin-syntax-export-extensions: 6.13.0babel-plugin-syntax-flow: 6.18.0babel-plugin-syntax-function-bind: 6.13.0babel-plugin-syntax-function-sent: 6.13.0babel-plugin-syntax-jsx: 6.18.0babel-plugin-syntax-object-rest-spread: 6.13.0babel-plugin-syntax-trailing-function-commas: 6.22.0babel-plugin-transform-async-functions: 6.22.0babel-plugin-transform-async-to-generator: 6.24.1babel-plugin-transform-async-to-module-method: 6.24.1babel-plugin-transform-class-constructor-call: 6.24.1babel-plugin-transform-class-properties: 6.24.1babel-plugin-transform-decorators: 6.24.1babel-plugin-transform-decorators-legacy: 1.3.4babel-plugin-transform-do-expressions: 6.22.0babel-plugin-transform-es2015-arrow-functions: 6.22.0babel-plugin-transform-es2015-block-scoped-functions: 6.22.0babel-plugin-transform-es2015-block-scoping: 6.26.0babel-plugin-transform-es2015-classes: 6.24.1babel-plugin-transform-es2015-computed-properties: 6.24.1babel-plugin-transform-es2015-destructuring: 6.23.0babel-plugin-transform-es2015-duplicate-keys: 6.24.1babel-plugin-transform-es2015-for-of: 6.23.0babel-plugin-transform-es2015-function-name: 6.24.1babel-plugin-transform-es2015-instanceof: 6.22.0babel-plugin-transform-es2015-literals: 6.22.0babel-plugin-transform-es2015-modules-amd: 6.24.1babel-plugin-transform-es2015-modules-commonjs: 6.26.0babel-plugin-transform-es2015-modules-systemjs: 6.24.1babel-plugin-transform-es2015-modules-umd: 6.24.1babel-plugin-transform-es2015-object-super: 6.24.1babel-plugin-transform-es2015-parameters: 6.24.1babel-plugin-transform-es2015-shorthand-properties: 6.24.1babel-plugin-transform-es2015-spread: 6.22.0babel-plugin-transform-es2015-sticky-regex: 6.24.1babel-plugin-transform-es2015-template-literals: 6.22.0babel-plugin-transform-es2015-typeof-symbol: 6.23.0babel-plugin-transform-es2015-unicode-regex: 6.24.1babel-plugin-transform-es3-member-expression-literals: 6.22.0babel-plugin-transform-es3-property-literals: 6.22.0babel-plugin-transform-es5-property-mutators: 6.24.1babel-plugin-transform-eval: 6.22.0babel-plugin-transform-exponentiation-operator: 6.24.1babel-plugin-transform-export-extensions: 6.22.0babel-plugin-transform-flow-comments: 6.22.0babel-plugin-transform-flow-strip-types: 6.22.0babel-plugin-transform-function-bind: 6.22.0babel-plugin-transform-inline-environment-variables: 6.8.0babel-plugin-transform-jscript: 6.22.0babel-plugin-transform-member-expression-literals: 6.8.5babel-plugin-transform-merge-sibling-variables: 6.8.6babel-plugin-transform-minify-booleans: 6.8.3babel-plugin-transform-node-env-inline: 6.8.0babel-plugin-transform-object-assign: 6.22.0babel-plugin-transform-object-rest-spread: 6.26.0babel-plugin-transform-object-set-prototype-of-to-assign: 6.22.0babel-plugin-transform-property-literals: 6.8.5babel-plugin-transform-proto-to-assign: 6.26.0babel-plugin-transform-react-constant-elements: 6.23.0babel-plugin-transform-react-display-name: 6.25.0babel-plugin-transform-react-inline-elements: 6.22.0babel-plugin-transform-react-jsx: 6.24.1babel-plugin-transform-react-jsx-compat: 6.24.1babel-plugin-transform-react-jsx-self: 6.22.0babel-plugin-transform-react-jsx-source: 6.22.0babel-plugin-transform-regenerator: 6.26.0babel-plugin-transform-remove-console: 6.8.5babel-plugin-transform-remove-debugger: 6.8.5babel-plugin-transform-runtime: 6.23.0babel-plugin-transform-simplify-comparison-operators: 6.8.5babel-plugin-transform-strict-mode: 6.24.1babel-plugin-transform-undefined-to-void: 6.8.3babel-plugin-undeclared-variables-check: 6.22.0babel-preset-babili: 0.1.4babel-preset-es2015: 6.24.1babel-preset-es2016: 6.24.1babel-preset-es2017: 6.24.1babel-preset-latest: 6.24.1babel-preset-react: 6.24.1babel-preset-stage-0: 6.24.1babel-preset-stage-1: 6.24.1babel-preset-stage-2: 6.24.1babel-preset-stage-3: 6.24.1gulp: ^3.9.1gulp-rename: ^1.2.2gulp-uglify: ^2.0.1jest: ^18.1.0json-loader: ^0.5.4lazypipe: ^1.0.1npm-check-updates: ^2.8.9pump: ^1.0.2react: ^15.4.2webpack: ^1.14.0webpack-stream: ^3.2.0

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code