Add a 'whitelist' option for the external-helpers plugin to mirror the helper builder. (#8531)
This commit is contained in:
parent
cada040bec
commit
595240f071
@ -4,7 +4,18 @@ import { types as t } from "@babel/core";
|
||||
export default declare((api, options) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
const { helperVersion = "7.0.0-beta.0" } = options;
|
||||
const { helperVersion = "7.0.0-beta.0", whitelist = false } = options;
|
||||
|
||||
if (
|
||||
whitelist !== false &&
|
||||
(!Array.isArray(whitelist) || whitelist.some(w => typeof w !== "string"))
|
||||
) {
|
||||
throw new Error(
|
||||
".whitelist must be undefined, false, or an array of strings",
|
||||
);
|
||||
}
|
||||
|
||||
const helperWhitelist = whitelist ? new Set(whitelist) : null;
|
||||
|
||||
return {
|
||||
pre(file) {
|
||||
@ -19,6 +30,12 @@ export default declare((api, options) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// babelCore.buildExternalHelpers() allows a whitelist of helpers that
|
||||
// will be inserted into the external helpers list. That same whitelist
|
||||
// should be passed into the plugin here in that case, so that we can
|
||||
// avoid referencing 'babelHelpers.XX' when the helper does not exist.
|
||||
if (helperWhitelist && !helperWhitelist.has(name)) return;
|
||||
|
||||
return t.memberExpression(
|
||||
t.identifier("babelHelpers"),
|
||||
t.identifier(name),
|
||||
|
||||
3
packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/input.js
vendored
Normal file
3
packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/input.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
class Foo {
|
||||
method(){}
|
||||
}
|
||||
8
packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/options.json
vendored
Normal file
8
packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/options.json
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"plugins": [
|
||||
["external-helpers", {
|
||||
"whitelist": ["createClass"]
|
||||
}],
|
||||
"transform-classes"
|
||||
]
|
||||
}
|
||||
17
packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/output.js
vendored
Normal file
17
packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/output.js
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
let Foo =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
function Foo() {
|
||||
_classCallCheck(this, Foo);
|
||||
}
|
||||
|
||||
babelHelpers.createClass(Foo, [{
|
||||
key: "method",
|
||||
value: function method() {}
|
||||
}]);
|
||||
return Foo;
|
||||
}();
|
||||
3
packages/babel-plugin-external-helpers/test/index.js
Normal file
3
packages/babel-plugin-external-helpers/test/index.js
Normal file
@ -0,0 +1,3 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
Loading…
x
Reference in New Issue
Block a user