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.

(cherry picked from commit 0d1edb9811694d25df2ef75a1e8de773624ec6b8)
This commit is contained in:
Robert Jackson 2017-03-06 14:21:58 -05:00
parent 2127df0db0
commit bc65822379
16 changed files with 78 additions and 3 deletions

View File

@ -0,0 +1 @@
export { default } from 'foo';

View File

@ -0,0 +1,13 @@
define(['exports', 'foo'], function (exports, _foo) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return _foo.default;
}
});
});

View File

@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", ["transform-es2015-modules-amd", { "noInterop": true }]]
}

View File

@ -0,0 +1,3 @@
import foo from "foo";
foo;

View File

@ -0,0 +1,5 @@
define(["foo"], function (_foo) {
"use strict";
_foo.default;
});

View File

@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", ["transform-es2015-modules-amd", { "noInterop": true }]]
}

View File

@ -150,6 +150,7 @@ export default function () {
this.ranCommonJS = true;
const strict = !!this.opts.strict;
const noInterop = !!this.opts.noInterop;
const { scope } = path;
@ -326,7 +327,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]),
@ -370,7 +371,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", [
@ -401,7 +402,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(

View File

@ -0,0 +1 @@
export { default } from 'foo';

View File

@ -0,0 +1,14 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _foo = require('foo');
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return _foo.default;
}
});

View File

@ -0,0 +1,3 @@
import foo from "foo";
foo();

View File

@ -0,0 +1,5 @@
"use strict";
var _foo = require("foo");
(0, _foo.default)();

View File

@ -0,0 +1,4 @@
import * as foo from 'foo';
foo.bar();
foo.baz();

View File

@ -0,0 +1,6 @@
'use strict';
var _foo = require('foo');
_foo.bar();
_foo.baz();

View File

@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", ["transform-es2015-modules-commonjs", { "noInterop": true }]]
}

View File

@ -0,0 +1,4 @@
import * as foo from 'foo';
foo.bar();
foo.baz();

View File

@ -0,0 +1,6 @@
'use strict';
var _foo = require('foo');
_foo.bar();
_foo.baz();