Implement importInterop: "node" option for module transforms (#12838)

This commit is contained in:
Nicolò Ribaudo
2021-04-28 18:22:47 +02:00
committed by GitHub
parent 22b0eb038f
commit be03be1bc3
101 changed files with 834 additions and 32 deletions

View File

@@ -0,0 +1,46 @@
import * as babel from "@babel/core";
import transformCommonjs 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" }],
[transformCommonjs, { importInterop }],
],
}).code;
expect(output).toMatchInlineSnapshot(`
"\\"use strict\\";
var _a = babelHelpers.interopRequireDefault(require(\\"a\\"));
var _b = require(\\"b\\");
var _c = require(\\"c\\");
(0, _a.default)();
_b();
(0, _c.default)();"
`);
});