How to use Babel 6 external helpers in the browser

2019-06-21 08:42发布

问题:

First the question:

Where can I find the external-helpers.js script, or how can I build the external-helpers for Babel 6?

In Babel 5.x, I was able to use the externalHelpers option, which required including external-helpers.js, which used to be in the babel-core package. Moving on to Babel 6, I see that external-helpers is now external-helpers-2 plugin. This does the job of including the relevant babelHelper calls in my transpiled code, but that is it; I need the actual helper definitions!

In the issue add missing build script for external-helpers.js, it is suggested to " build it yourself with the CLI". I don't see any CLI options that seem to deal with building external helpers.

回答1:

I managed to build the external-helpers.js with the babel-core package and the Node REPL:

var helperBuilder = require('./lib/tools/build-external-helpers');
fs.writeFileSync('external-helpers.js', helperBuilder());

I imagine that, depending on your situation, you could also build the external helpers file by build script (Grunt, Gulp, etc.)



回答2:

The CLI command referred to in the issue you mention is babel-external-helpers, which is part of the babel-cli npm package. With the babel-cli package installed, running babel-external-helpers --help gives the following self-explanatory output:

Usage: babel-external-helpers [options]

Options:

-h, --help                   output usage information
-l, --whitelist [whitelist]  Whitelist of helpers to ONLY include
-t, --output-type [type]     Type of output (global|umd|var)

It simply outputs the file to stdout, so to print the code to a file, you do babel-external-helpers [options] > babel-helpers.js.