Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass filename to importInterop method #14456

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/babel-helper-module-transforms/src/index.ts
Expand Up @@ -57,6 +57,7 @@ export function rewriteModuleStatementsAndPrepareHeader(
importInterop = noInterop ? "none" : "babel",
lazy,
esNamespaceOnly,
filename,

constantReexports = loose,
enumerableModuleMeta = loose,
Expand All @@ -71,6 +72,7 @@ export function rewriteModuleStatementsAndPrepareHeader(
noInterop?;
lazy?;
esNamespaceOnly?;
filename: string | undefined;
constantReexports?;
enumerableModuleMeta?;
noIncompleteNsImportDetection?: boolean;
Expand All @@ -85,6 +87,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 @@ -118,6 +118,7 @@ export default declare((api, options) => {
allowTopLevelThis,
importInterop,
noInterop,
filename: this.opts.filename,
},
);

Expand Down
Expand Up @@ -211,6 +211,7 @@ export default declare((api, options) => {
? mjsStrictNamespace
: strictNamespace,
noIncompleteNsImportDetection,
filename: this.file.opts.filename,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also use rewriteModuleStatementsAndPrepareHeader in the amd and umd plugins. I don't remember if they all support the importInterop option, but if they do they should have the same behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's covered by what I already have in the diff, right? Or am I missing something?

},
);

Expand Down
@@ -1,6 +1,7 @@
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 = `
Expand All @@ -13,14 +14,17 @@ it("'importInterop' accepts a function", function () {
c();
`;

const importInterop = source => {
const importInterop = jest.fn(source => {
if (source === "a") return "babel";
else if (source === "b") return "node";
else if (source === "c") return "none";
};
});

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

const output = babel.transformSync(code, {
configFile: false,
filename,
ast: false,
plugins: [
[externalHelpers, { helperVersion: "7.100.0" }],
Expand All @@ -43,4 +47,9 @@ it("'importInterop' accepts a function", function () {

(0, _c.default)();"
`);

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

Expand Down