Anton Rusinov fcdfc61bdb 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 !
2017-10-10 00:51:34 -04:00

107 lines
3.0 KiB
JavaScript

import template from "babel-template";
import {
isModule,
rewriteModuleStatementsAndPrepareHeader,
hasExports,
isSideEffectImport,
buildNamespaceInitStatements,
ensureStatementsHoisted,
wrapInterop,
} from "babel-helper-module-transforms";
const buildWrapper = template(`
define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) {
})
`);
export default function({ types: t }, options) {
const { loose, allowTopLevelThis, strict, strictMode, noInterop } = options;
return {
visitor: {
Program: {
exit(path) {
if (!isModule(path)) return;
let moduleName = this.getModuleName();
if (moduleName) moduleName = t.stringLiteral(moduleName);
const {
meta,
headers,
} = rewriteModuleStatementsAndPrepareHeader(path, {
loose,
strict,
strictMode,
allowTopLevelThis,
noInterop,
});
const amdArgs = [];
const commonjsArgs = [];
const importNames = [];
if (hasExports(meta)) {
amdArgs.push(t.stringLiteral("exports"));
commonjsArgs.push(t.identifier("exports"));
importNames.push(t.identifier(meta.exportName));
}
for (const [source, metadata] of meta.source) {
amdArgs.push(t.stringLiteral(source));
commonjsArgs.push(
t.callExpression(t.identifier("require"), [
t.stringLiteral(source),
]),
);
importNames.push(t.identifier(metadata.name));
if (!isSideEffectImport(metadata)) {
const interop = wrapInterop(
path,
t.identifier(metadata.name),
metadata.interop,
);
if (interop) {
const header = t.expressionStatement(
t.assignmentExpression(
"=",
t.identifier(metadata.name),
interop,
),
);
header.loc = metadata.loc;
headers.push(header);
}
}
headers.push(...buildNamespaceInitStatements(meta, metadata));
}
ensureStatementsHoisted(headers);
path.unshiftContainer("body", headers);
const { body, directives } = path.node;
path.node.directives = [];
path.node.body = [];
const amdWrapper = path.pushContainer("body", [
buildWrapper({
MODULE_NAME: moduleName,
AMD_ARGUMENTS: t.arrayExpression(amdArgs),
COMMONJS_ARGUMENTS: commonjsArgs,
IMPORT_NAMES: importNames,
}),
])[0];
const amdFactory = amdWrapper
.get("expression.arguments")
.filter(arg => arg.isFunctionExpression())[0]
.get("body");
amdFactory.pushContainer("directives", directives);
amdFactory.pushContainer("body", body);
},
},
},
};
}