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 all 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.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 @@ -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,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 @@ -160,6 +160,7 @@ export default declare((api, options) => {
allowTopLevelThis,
noInterop,
importInterop,
filename: this.file.opts.filename,
},
);

Expand Down