Move plugin processing to top of plugins (#6381)

* centralize plugin options

* Centralize plugins options

- move more options to the top
- move validations that depend on options to the top

* use isLoose option

* Move more validations to the top

* Move ref parameter for rewriteModuleStatementsAndPrepareHeader() to the top

* fix eslint errors

* remove unused parameter

* set default systemGlobal value

* Revert "Move ref parameter for rewriteModuleStatementsAndPrepareHeader() to the top"

This reverts commit b3855302d17fa19d8acb4c8accab3680c8d2710e.

* Revert "Move more validations to the top"

This reverts commit e5861d8a034ff8f553391f55654f753bcf428a5d.

* fix allowMutablePropsOnTags option usage

* improve naming

* change Contructor definition for sake of consistency

* move allowMutablePropsOnTags validation to the top

* add missing !
This commit is contained in:
Anton Rusinov
2017-10-10 07:51:34 +03:00
committed by Logan Smyth
parent d89063bb32
commit fcdfc61bdb
17 changed files with 135 additions and 130 deletions

View File

@@ -2,10 +2,20 @@ import { addDefault, isModule } from "babel-helper-module-imports";
import definitions from "./definitions";
export default function({ types: t }) {
function getRuntimeModuleName(opts) {
return opts.moduleName || "babel-runtime";
}
export default function({ types: t }, options) {
const {
helpers,
moduleName = "babel-runtime",
polyfill,
regenerator,
useBuiltIns,
useESModules,
} = options;
const notRegenerator = regenerator !== false;
const notPolyfillOrDoesUseBuiltIns = polyfill === false || useBuiltIns;
const isPolyfillAndUseBuiltIns = polyfill && useBuiltIns;
const baseHelpersDir = useBuiltIns ? "helpers/builtin" : "helpers";
const helpersDir = useESModules ? `${baseHelpersDir}/es6` : baseHelpersDir;
function has(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
@@ -15,15 +25,7 @@ export default function({ types: t }) {
return {
pre(file) {
const moduleName = getRuntimeModuleName(this.opts);
if (this.opts.helpers !== false) {
const baseHelpersDir = this.opts.useBuiltIns
? "helpers/builtin"
: "helpers";
const helpersDir = this.opts.useESModules
? `${baseHelpersDir}/es6`
: baseHelpersDir;
if (helpers !== false) {
file.set("helperGenerator", name => {
const isInteropHelper = HEADER_HELPERS.indexOf(name) !== -1;
@@ -41,7 +43,7 @@ export default function({ types: t }) {
});
}
if (this.opts.polyfill && this.opts.useBuiltIns) {
if (isPolyfillAndUseBuiltIns) {
throw new Error(
"The polyfill option conflicts with useBuiltIns; use one or the other",
);
@@ -75,12 +77,9 @@ export default function({ types: t }) {
},
visitor: {
ReferencedIdentifier(path, state) {
ReferencedIdentifier(path) {
const { node, parent, scope } = path;
if (
node.name === "regeneratorRuntime" &&
state.opts.regenerator !== false
) {
if (node.name === "regeneratorRuntime" && notRegenerator) {
path.replaceWith(
this.addDefaultImport(
`${this.moduleName}/regenerator`,
@@ -90,14 +89,13 @@ export default function({ types: t }) {
return;
}
if (state.opts.polyfill === false || state.opts.useBuiltIns) return;
if (notPolyfillOrDoesUseBuiltIns) return;
if (t.isMemberExpression(parent)) return;
if (!has(definitions.builtins, node.name)) return;
if (scope.getBindingIdentifier(node.name)) return;
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
const moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(
this.addDefaultImport(
`${moduleName}/core-js/${definitions.builtins[node.name]}`,
@@ -107,8 +105,8 @@ export default function({ types: t }) {
},
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)
CallExpression(path, state) {
if (state.opts.polyfill === false || state.opts.useBuiltIns) return;
CallExpression(path) {
if (notPolyfillOrDoesUseBuiltIns) return;
// we can't compile this
if (path.node.arguments.length) return;
@@ -120,7 +118,6 @@ export default function({ types: t }) {
return;
}
const moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(
t.callExpression(
this.addDefaultImport(
@@ -133,13 +130,12 @@ export default function({ types: t }) {
},
// Symbol.iterator in arr -> core.$for.isIterable(arr)
BinaryExpression(path, state) {
if (state.opts.polyfill === false || state.opts.useBuiltIns) return;
BinaryExpression(path) {
if (notPolyfillOrDoesUseBuiltIns) return;
if (path.node.operator !== "in") return;
if (!path.get("left").matchesPattern("Symbol.iterator")) return;
const moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(
t.callExpression(
this.addDefaultImport(
@@ -153,8 +149,8 @@ export default function({ types: t }) {
// Array.from -> _core.Array.from
MemberExpression: {
enter(path, state) {
if (state.opts.polyfill === false || state.opts.useBuiltIns) return;
enter(path) {
if (notPolyfillOrDoesUseBuiltIns) return;
if (!path.isReferenced()) return;
const { node } = path;
@@ -183,7 +179,6 @@ export default function({ types: t }) {
}
}
const moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(
this.addDefaultImport(
`${moduleName}/core-js/${methods[prop.name]}`,
@@ -192,8 +187,8 @@ export default function({ types: t }) {
);
},
exit(path, state) {
if (state.opts.polyfill === false || state.opts.useBuiltIns) return;
exit(path) {
if (notPolyfillOrDoesUseBuiltIns) return;
if (!path.isReferenced()) return;
const { node } = path;
@@ -202,7 +197,6 @@ export default function({ types: t }) {
if (!has(definitions.builtins, obj.name)) return;
if (path.scope.getBindingIdentifier(obj.name)) return;
const moduleName = getRuntimeModuleName(state.opts);
path.replaceWith(
t.memberExpression(
this.addDefaultImport(