Merge pull request #5422 from rwjblue/remove-interop-require-default-in-strict-mode
Add noInterop option to babel-plugin-transform-es2015-modules-commonjs.
This commit is contained in:
commit
836dd95c44
@ -55,3 +55,7 @@ require("babel-core").transform("code", {
|
||||
plugins: ["transform-es2015-modules-amd"]
|
||||
});
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
See options for `babel-plugin-transform-es2015-commonjs`.
|
||||
|
||||
@ -0,0 +1 @@
|
||||
export { default } from 'foo';
|
||||
@ -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;
|
||||
}
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", ["transform-es2015-modules-amd", { "noInterop": true }]]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
import foo from "foo";
|
||||
|
||||
foo;
|
||||
@ -0,0 +1,5 @@
|
||||
define(["foo"], function (_foo) {
|
||||
"use strict";
|
||||
|
||||
_foo.default;
|
||||
});
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", ["transform-es2015-modules-amd", { "noInterop": true }]]
|
||||
}
|
||||
@ -82,10 +82,53 @@ Object.defineProperty(exports, "__esModule", {
|
||||
});
|
||||
```
|
||||
|
||||
In environments that don't support this you can enable loose mode on `es6.modules`
|
||||
In environments that don't support this you can enable loose mode on `babel-plugin-transform-es20150-modules-commonjs`
|
||||
and instead of using `Object.defineProperty` an assignment will be used instead.
|
||||
|
||||
```javascript
|
||||
var foo = exports.foo = 5;
|
||||
exports.__esModule = true;
|
||||
```
|
||||
|
||||
### `strict`
|
||||
|
||||
`boolean`, defaults to `false`
|
||||
|
||||
By default, when using exports with babel a non-enumerable `__esModule` property
|
||||
is exported. In some cases this property is used to determine if the import _is_ the
|
||||
default export or if it _contains_ the default export.
|
||||
|
||||
```javascript
|
||||
var foo = exports.foo = 5;
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
```
|
||||
|
||||
In order to prevent the `__esModule` property from being exported, you can set
|
||||
the `strict` option to `true`.
|
||||
|
||||
### `noInterop`
|
||||
|
||||
`boolean`, defaults to `false`
|
||||
|
||||
By default, when using exports with babel a non-enumerable `__esModule` property
|
||||
is exported. This property is then used to determine if the import _is_ the default
|
||||
export or if it _contains_ the default export.
|
||||
|
||||
```javascript
|
||||
"use strict";
|
||||
|
||||
var _foo = require("foo");
|
||||
|
||||
var _foo2 = _interopRequireDefault(_foo);
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : { default: obj };
|
||||
}
|
||||
```
|
||||
|
||||
In cases where the auto-unwrapping of `default` is not needed, you can set the
|
||||
`noInterop` option to `true` to avoid the usage of the `interopRequireDefault`
|
||||
helper (shown in inline form above).
|
||||
|
||||
@ -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(
|
||||
|
||||
@ -0,0 +1 @@
|
||||
export { default } from 'foo';
|
||||
@ -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;
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,3 @@
|
||||
import foo from "foo";
|
||||
|
||||
foo();
|
||||
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
var _foo = require("foo");
|
||||
|
||||
(0, _foo.default)();
|
||||
@ -0,0 +1,4 @@
|
||||
import * as foo from 'foo';
|
||||
|
||||
foo.bar();
|
||||
foo.baz();
|
||||
@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var _foo = require('foo');
|
||||
|
||||
_foo.bar();
|
||||
_foo.baz();
|
||||
3
packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/options.json
vendored
Normal file
3
packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/noInterop/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", ["transform-es2015-modules-commonjs", { "noInterop": true }]]
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
import * as foo from 'foo';
|
||||
|
||||
foo.bar();
|
||||
foo.baz();
|
||||
@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var _foo = require('foo');
|
||||
|
||||
_foo.bar();
|
||||
_foo.baz();
|
||||
Loading…
x
Reference in New Issue
Block a user