Add noInterop option to babel-plugin-transform-es2015-modules-commonjs.

The intent of this option is to toggle module interop behavior. When `true`
no `interopRequireXXX` helper invocations will be emitted.
This commit is contained in:
Robert Jackson
2017-03-06 14:21:58 -05:00
parent 8a82cc060a
commit 0d1edb9811
16 changed files with 78 additions and 3 deletions

View File

@@ -151,6 +151,7 @@ export default function () {
this.ranCommonJS = true;
const strict = !!this.opts.strict;
const noInterop = !!this.opts.noInterop;
const { scope } = path;
@@ -327,7 +328,7 @@ export default function () {
} else if (specifier.isExportDefaultSpecifier()) {
// todo
} else if (specifier.isExportSpecifier()) {
if (specifier.node.local.name === "default") {
if (!noInterop && specifier.node.local.name === "default") {
topNodes.push(buildExportsFrom(t.stringLiteral(specifier.node.exported.name),
t.memberExpression(
t.callExpression(this.addHelper("interopRequireDefault"), [ref]),
@@ -371,7 +372,7 @@ export default function () {
for (let i = 0; i < specifiers.length; i++) {
const specifier = specifiers[i];
if (t.isImportNamespaceSpecifier(specifier)) {
if (strict) {
if (strict || noInterop) {
remaps[specifier.local.name] = uid;
} else {
const varDecl = t.variableDeclaration("var", [
@@ -402,7 +403,7 @@ export default function () {
if (specifier.imported.name === "default") {
if (wildcard) {
target = wildcard;
} else {
} else if (!noInterop) {
target = wildcard = path.scope.generateUidIdentifier(uid.name);
const varDecl = t.variableDeclaration("var", [
t.variableDeclarator(