Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active April 8, 2018 02:25
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rauschma/49f6c6f8a9f65d5a2f6c to your computer and use it in GitHub Desktop.
Save rauschma/49f6c6f8a9f65d5a2f6c to your computer and use it in GitHub Desktop.

Installing Babel

The following are a few important npm packages.

Core Babel and access to Babel:

  • babel-core: the core compilation machinery and plugin infrastructure for Babel. You will rarely need to install this package, because other packages such as babel-cli have it as a dependency, meaning that it will be automatically installed when they are installed.

  • babel-cli: a command line interface to Babel. It includes the following commands:

    • babel-doctor detects common problems with your Babel installation.
    • babel transpiles files or stdin via Babel.
    • babel-node a version of the Node.js executable node that transpiles everything via Babel.
    • babel-external-helpers prints all of Babel’s helper functions (such as inherits for subclassing) to the console.
  • babel-register: lets you switch on Babel transpilation from within Node.js. After you do, all modules you require (minus code you want to ignore, e.g. packages installed via npm) are automatically transpiled.

Complementary libraries:

  • babel-polyfill: globally installs the ES6 standard library (new Array methods, Reflect, etc.). This package is optional (there are other polyfills/shims or the JavaScript engine you are targeting may already provide everything you need). But if you do want to use it, you need to import it at least once in your app. Unless you are using babel-node, which imports it while it is starting up.

  • babel-plugin-external-helpers-2: If this plugin is switched on, it does two things:

    • It puts all of Babel’s helper functions (such as inherits for subclassing) into an object stored in a global variable.
    • It calls those global functions from the code generated by Babel, instead of embedding the helpers in the code.
  • babel-runtime provides the services of babel-polyfill and babel-plugin-external-helpers-2, but without using global variables (which is important for libraries). It is a modules that contains both the functionality of the ES6 standard library and the helpers mentioned in the previous item.

    • The plugin transform-runtime transforms Babel’s output so that operations use the module babel-runtime instead of something global. This approach has limits – not everything global can be detected via static analysis and redirected to the module. The package of the plugin contains the file definitions.js that lists what parts of the standard library are available.

All Babel packages reside in a single repository on GitHub. Browsing their source code and their package.json files is instructive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment