Skip to content

Commit

Permalink
Pass filename to importInterop method (#14456)
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Heiner <nheiner@netflix.com>
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
3 people committed May 17, 2022
1 parent a3a63d2 commit 92ffff4
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 121 deletions.
3 changes: 3 additions & 0 deletions packages/babel-helper-module-transforms/src/index.ts
Expand Up @@ -58,6 +58,7 @@ export function rewriteModuleStatementsAndPrepareHeader(
importInterop = noInterop ? "none" : "babel",
lazy,
esNamespaceOnly,
filename,

constantReexports = loose,
enumerableModuleMeta = loose,
Expand All @@ -72,6 +73,7 @@ export function rewriteModuleStatementsAndPrepareHeader(
noInterop?;
lazy?;
esNamespaceOnly?;
filename: string | undefined;
constantReexports?;
enumerableModuleMeta?;
noIncompleteNsImportDetection?: boolean;
Expand All @@ -86,6 +88,7 @@ export function rewriteModuleStatementsAndPrepareHeader(
initializeReexports: constantReexports,
lazy,
esNamespaceOnly,
filename,
});

if (!allowTopLevelThis) {
Expand Down
Expand Up @@ -89,9 +89,13 @@ export function validateImportInteropOption(
return importInterop;
}

function resolveImportInterop(importInterop, source) {
function resolveImportInterop(
importInterop,
source,
filename: string | undefined,
) {
if (typeof importInterop === "function") {
return validateImportInteropOption(importInterop(source));
return validateImportInteropOption(importInterop(source, filename));
}
return importInterop;
}
Expand All @@ -108,6 +112,7 @@ export default function normalizeModuleAndLoadMetadata(
initializeReexports = false,
lazy = false,
esNamespaceOnly = false,
filename,
},
): ModuleMetadata {
if (!exportName) {
Expand Down Expand Up @@ -136,6 +141,7 @@ export default function normalizeModuleAndLoadMetadata(
const resolvedInterop = resolveImportInterop(
importInterop,
metadata.source,
filename,
);

if (resolvedInterop === "none") {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-modules-amd/src/index.ts
Expand Up @@ -134,6 +134,7 @@ export default declare<State>((api, options: Options) => {
allowTopLevelThis,
importInterop,
noInterop,
filename: this.file.opts.filename,
},
);

Expand Down
@@ -1,43 +1,81 @@
import * as babel from "@babel/core";
import transformAmd from "../lib/index.js";
import externalHelpers from "@babel/plugin-external-helpers";
import path from "path";

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" }],
[transformAmd, { importInterop }],
],
}).code;

expect(output).toMatchInlineSnapshot(`
"define([\\"a\\", \\"b\\", \\"c\\"], function (_a, _b, _c) {
\\"use strict\\";
_a = babelHelpers.interopRequireDefault(_a);
(0, _a.default)();
_b();
(0, _c.default)();
});"
`);
describe("'importInterop'", () => {
function transform(code, importInterop, filename) {
return babel.transformSync(code, {
configFile: false,
filename,
ast: false,
plugins: [
[externalHelpers, { helperVersion: "7.100.0" }],
[transformAmd, { importInterop }],
],
}).code;
}

it("'importInterop' accepts a 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";
};

expect(transform(code, importInterop)).toMatchInlineSnapshot(`
"define([\\"a\\", \\"b\\", \\"c\\"], function (_a, _b, _c) {
\\"use strict\\";
_a = babelHelpers.interopRequireDefault(_a);
(0, _a.default)();
_b();
(0, _c.default)();
});"
`);
});

it("gets called with the filename if present", () => {
const code = `
import a from "a";
import b from "b";
`;

const importInterop = jest.fn(() => "babel");

const filename = "path/to/fake-filename.js";

transform(code, importInterop, filename);

expect(importInterop).toHaveBeenCalledTimes(2);
expect(importInterop).toHaveBeenCalledWith("a", path.resolve(filename));
expect(importInterop).toHaveBeenCalledWith("b", path.resolve(filename));
});

it("gets called with undefined if the filename is not present", () => {
const code = `
import a from "a";
import b from "b";
`;

const importInterop = jest.fn(() => "babel");

transform(code, importInterop);

expect(importInterop).toHaveBeenCalledTimes(2);
expect(importInterop).toHaveBeenCalledWith("a", undefined);
expect(importInterop).toHaveBeenCalledWith("b", undefined);
});
});
Expand Up @@ -225,6 +225,7 @@ export default declare((api, options: Options) => {
? mjsStrictNamespace
: strictNamespace,
noIncompleteNsImportDetection,
filename: this.file.opts.filename,
},
);

Expand Down
@@ -1,46 +1,84 @@
import * as babel from "@babel/core";
import transformCommonjs from "../lib/index.js";
import externalHelpers from "@babel/plugin-external-helpers";
import path from "path";

it("'importInterop' accepts a function", function () {
const code = `
import a from "a";
import b from "b";
import c from "c";
describe("'importInterop'", () => {
function transform(code, importInterop, filename) {
return babel.transformSync(code, {
configFile: false,
filename,
ast: false,
plugins: [
[externalHelpers, { helperVersion: "7.100.0" }],
[transformCommonjs, { importInterop }],
],
}).code;
}

a();
b();
c();
`;
it("'importInterop' accepts a function", () => {
const code = `
import a from "a";
import b from "b";
import c from "c";
const importInterop = source => {
if (source === "a") return "babel";
else if (source === "b") return "node";
else if (source === "c") return "none";
};
a();
b();
c();
`;

const output = babel.transformSync(code, {
configFile: false,
ast: false,
plugins: [
[externalHelpers, { helperVersion: "7.100.0" }],
[transformCommonjs, { importInterop }],
],
}).code;
const importInterop = source => {
if (source === "a") return "babel";
else if (source === "b") return "node";
else if (source === "c") return "none";
};

expect(output).toMatchInlineSnapshot(`
"\\"use strict\\";
expect(transform(code, importInterop)).toMatchInlineSnapshot(`
"\\"use strict\\";
var _a = babelHelpers.interopRequireDefault(require(\\"a\\"));
var _a = babelHelpers.interopRequireDefault(require(\\"a\\"));
var _b = require(\\"b\\");
var _b = require(\\"b\\");
var _c = require(\\"c\\");
var _c = require(\\"c\\");
(0, _a.default)();
(0, _a.default)();
_b();
_b();
(0, _c.default)();"
`);
(0, _c.default)();"
`);
});

it("gets called with the filename if present", () => {
const code = `
import a from "a";
import b from "b";
`;

const importInterop = jest.fn(() => "babel");

const filename = "path/to/fake-filename.js";

transform(code, importInterop, filename);

expect(importInterop).toHaveBeenCalledTimes(2);
expect(importInterop).toHaveBeenCalledWith("a", path.resolve(filename));
expect(importInterop).toHaveBeenCalledWith("b", path.resolve(filename));
});

it("gets called with undefined if the filename is not present", () => {
const code = `
import a from "a";
import b from "b";
`;

const importInterop = jest.fn(() => "babel");

transform(code, importInterop);

expect(importInterop).toHaveBeenCalledTimes(2);
expect(importInterop).toHaveBeenCalledWith("a", undefined);
expect(importInterop).toHaveBeenCalledWith("b", undefined);
});
});
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-modules-umd/src/index.ts
Expand Up @@ -173,6 +173,7 @@ export default declare((api, options: Options) => {
allowTopLevelThis,
noInterop,
importInterop,
filename: this.file.opts.filename,
},
);

Expand Down

0 comments on commit 92ffff4

Please sign in to comment.