Implement importInterop: "node" option for module transforms (#12838)
This commit is contained in:
1
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep1.js
vendored
Normal file
1
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep1.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = function () { return 1 };
|
||||
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep2.js
vendored
Normal file
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep2.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
module.exports = function () { return 2 };
|
||||
module.exports.__esModule = true;
|
||||
1
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep3.js
vendored
Normal file
1
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep3.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = function () { return 3 };
|
||||
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep4.js
vendored
Normal file
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep4.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
module.exports = function () { return 4 };
|
||||
module.exports.__esModule = true;
|
||||
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep5.js
vendored
Normal file
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep5.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
exports.default = function () { return 5 };
|
||||
|
||||
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep6.js
vendored
Normal file
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep6.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
exports.default = function () { return 6 };
|
||||
exports.__esModule = true;
|
||||
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep7.js
vendored
Normal file
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep7.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
exports.named = function () { return 7 };
|
||||
|
||||
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep8.js
vendored
Normal file
2
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/dep8.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
exports.named = function () { return 8 };
|
||||
exports.__esModule = true;
|
||||
36
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/exec.js
vendored
Normal file
36
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/exec/exec.js
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
import dep1 from "./dep1.js";
|
||||
import dep2 from "./dep2.js";
|
||||
import * as dep3 from "./dep3.js";
|
||||
import * as dep4 from "./dep4.js";
|
||||
import dep5 from "./dep5.js";
|
||||
import dep6 from "./dep6.js";
|
||||
import { named as dep7 } from "./dep7.js";
|
||||
import { named as dep8 } from "./dep8.js";
|
||||
|
||||
expect(typeof dep1).toBe("function");
|
||||
expect(dep1()).toBe(1);
|
||||
|
||||
expect(typeof dep2).toBe("function");
|
||||
expect(dep2()).toBe(2);
|
||||
|
||||
expect(typeof dep3).toBe("object");
|
||||
expect(typeof dep3.default).toBe("function");
|
||||
expect(dep3.default()).toBe(3);
|
||||
|
||||
expect(typeof dep4).toBe("object");
|
||||
expect(typeof dep4.default).toBe("function");
|
||||
expect(dep4.default()).toBe(4);
|
||||
|
||||
expect(typeof dep5).toBe("object");
|
||||
expect(typeof dep5.default).toBe("function");
|
||||
expect(dep5.default()).toBe(5);
|
||||
|
||||
expect(typeof dep6).toBe("object");
|
||||
expect(typeof dep6.default).toBe("function");
|
||||
expect(dep6.default()).toBe(6);
|
||||
|
||||
expect(typeof dep7).toBe("function");
|
||||
expect(dep7()).toBe(7);
|
||||
|
||||
expect(typeof dep8).toBe("function");
|
||||
expect(dep8()).toBe(8);
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"sourceType": "module"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'dep';
|
||||
@@ -0,0 +1,20 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "dep"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("dep"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(mod.exports, global.dep);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _dep) {
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(_exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
_exports.default = _dep;
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
export { default, name } from 'dep';
|
||||
@@ -0,0 +1,32 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "dep"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("dep"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(mod.exports, global.dep);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _dep) {
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(_exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(_exports, "default", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _dep.default;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(_exports, "name", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _dep.name;
|
||||
}
|
||||
});
|
||||
_dep = babelHelpers.interopRequireWildcard(_dep, true);
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
export { name } from 'dep';
|
||||
@@ -0,0 +1,25 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "dep"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("dep"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(mod.exports, global.dep);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _dep) {
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(_exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(_exports, "name", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _dep.name;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
import foo from "foo";
|
||||
|
||||
foo();
|
||||
@@ -0,0 +1,17 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(require("foo"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(global.foo);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_foo) {
|
||||
"use strict";
|
||||
|
||||
_foo();
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
import foo, { named } from "foo";
|
||||
|
||||
foo();
|
||||
named();
|
||||
@@ -0,0 +1,19 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(require("foo"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(global.foo);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_foo) {
|
||||
"use strict";
|
||||
|
||||
_foo = babelHelpers.interopRequireWildcard(_foo, true);
|
||||
(0, _foo.default)();
|
||||
(0, _foo.named)();
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
import { named } from "foo";
|
||||
|
||||
named();
|
||||
@@ -0,0 +1,17 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(require("foo"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(global.foo);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_foo) {
|
||||
"use strict";
|
||||
|
||||
(0, _foo.named)();
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
import * as foo from 'foo';
|
||||
|
||||
foo.bar();
|
||||
foo.baz();
|
||||
@@ -0,0 +1,19 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(require("foo"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(global.foo);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (foo) {
|
||||
"use strict";
|
||||
|
||||
foo = babelHelpers.interopRequireWildcard(foo, true);
|
||||
foo.bar();
|
||||
foo.baz();
|
||||
});
|
||||
3
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/options.json
vendored
Normal file
3
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-node/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-modules-umd", { "importInterop": "node" }]]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from 'foo';
|
||||
@@ -0,0 +1,25 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(mod.exports, global.foo);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, 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 @@
|
||||
import foo from "foo";
|
||||
|
||||
foo();
|
||||
@@ -0,0 +1,17 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(require("foo"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(global.foo);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_foo) {
|
||||
"use strict";
|
||||
|
||||
(0, _foo.default)();
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
import * as foo from 'foo';
|
||||
|
||||
foo.bar();
|
||||
foo.baz();
|
||||
@@ -0,0 +1,18 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(require("foo"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(global.foo);
|
||||
global.input = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (foo) {
|
||||
"use strict";
|
||||
|
||||
foo.bar();
|
||||
foo.baz();
|
||||
});
|
||||
3
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-none/options.json
vendored
Normal file
3
packages/babel-plugin-transform-modules-umd/test/fixtures/importInterop-none/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["transform-modules-umd", { "importInterop": "none" }]]
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import * as babel from "@babel/core";
|
||||
import transformUmd from "../lib";
|
||||
import externalHelpers from "@babel/plugin-external-helpers";
|
||||
|
||||
it("'importInterop' accepts a function", function () {
|
||||
const code = `
|
||||
import a from "a";
|
||||
import b from "b";
|
||||
import c from "c";
|
||||
|
||||
a();
|
||||
b();
|
||||
c();
|
||||
`;
|
||||
|
||||
const importInterop = source => {
|
||||
if (source === "a") return "babel";
|
||||
else if (source === "b") return "node";
|
||||
else if (source === "c") return "none";
|
||||
};
|
||||
|
||||
const output = babel.transformSync(code, {
|
||||
configFile: false,
|
||||
ast: false,
|
||||
plugins: [
|
||||
[externalHelpers, { helperVersion: "7.100.0" }],
|
||||
[transformUmd, { importInterop }],
|
||||
],
|
||||
}).code;
|
||||
|
||||
expect(output).toMatchInlineSnapshot(`
|
||||
"(function (global, factory) {
|
||||
if (typeof define === \\"function\\" && define.amd) {
|
||||
define([\\"a\\", \\"b\\", \\"c\\"], factory);
|
||||
} else if (typeof exports !== \\"undefined\\") {
|
||||
factory(require(\\"a\\"), require(\\"b\\"), require(\\"c\\"));
|
||||
} else {
|
||||
var mod = {
|
||||
exports: {}
|
||||
};
|
||||
factory(global.a, global.b, global.c);
|
||||
global.unknown = mod.exports;
|
||||
}
|
||||
})(typeof globalThis !== \\"undefined\\" ? globalThis : typeof self !== \\"undefined\\" ? self : this, function (_a, _b, _c) {
|
||||
\\"use strict\\";
|
||||
|
||||
_a = babelHelpers.interopRequireDefault(_a);
|
||||
(0, _a.default)();
|
||||
|
||||
_b();
|
||||
|
||||
(0, _c.default)();
|
||||
});"
|
||||
`);
|
||||
});
|
||||
Reference in New Issue
Block a user