Install

  1. npm install --save-dev @babel/helpers

Usage

Direct:

  1. import * as helpers from '@babel/helpers';
  2. import * as t from '@babel/types';
  3. const typeofHelper = helpers.get('typeof');
  4. t.isExpressionStatement(typeofHelper);
  5. // true

Inside a plugin:

  1. export default {
  2. visitor: {
  3. UnaryExpression(path) {
  4. // The .addHelper function adds, if needed, the helper to the file
  5. // and returns an expression which references the helper
  6. const typeofHelper = this.addHelper("typeof");
  7. t.isExpression(typeofHelper); // true
  8. }
  9. };

Defining Helpers

NOTE: This package is only meant to be used by the packages included in this repository. There is currently no way for third-party plugins to define a helper.

Helpers are defined in the src/helpers.js file, and they must be valid modules which follow these guidelines:

  • They must have a default export, which is their entry-point.
  • They can import other helpers, exclusively by using default imports.
  • They can't have named exports.
  1. helpers.customHelper = defineHelper(`
  2. import dep from "dependency";
  3. const foo = 2;
  4. export default function getFooTimesDepPlusX(x) {
  5. return foo * dep() + x;
  6. }
  7. `);