From 2f09cc524d4770b3e7e443dda0785a0594acd3f5 Mon Sep 17 00:00:00 2001 From: Nick Heiner Date: Mon, 11 Apr 2022 12:26:11 -0400 Subject: [PATCH 1/5] Pass filename to importInterop method --- .../babel-helper-module-transforms/src/index.ts | 3 +++ .../src/normalize-and-load-metadata.ts | 6 ++++-- .../src/index.ts | 1 + .../test/importInterop-function.js | 13 +++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts index 64b18ae96d8e..8e4d19e99aa5 100644 --- a/packages/babel-helper-module-transforms/src/index.ts +++ b/packages/babel-helper-module-transforms/src/index.ts @@ -57,6 +57,7 @@ export function rewriteModuleStatementsAndPrepareHeader( importInterop = noInterop ? "none" : "babel", lazy, esNamespaceOnly, + filename, constantReexports = loose, enumerableModuleMeta = loose, @@ -71,6 +72,7 @@ export function rewriteModuleStatementsAndPrepareHeader( noInterop?; lazy?; esNamespaceOnly?; + filename: string; constantReexports?; enumerableModuleMeta?; noIncompleteNsImportDetection?: boolean; @@ -85,6 +87,7 @@ export function rewriteModuleStatementsAndPrepareHeader( initializeReexports: constantReexports, lazy, esNamespaceOnly, + filename, }); if (!allowTopLevelThis) { diff --git a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts index 0fac804b8f21..be9f65313f99 100644 --- a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts +++ b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts @@ -89,9 +89,9 @@ export function validateImportInteropOption( return importInterop; } -function resolveImportInterop(importInterop, source) { +function resolveImportInterop(importInterop, source, filename: string) { if (typeof importInterop === "function") { - return validateImportInteropOption(importInterop(source)); + return validateImportInteropOption(importInterop(source, filename)); } return importInterop; } @@ -108,6 +108,7 @@ export default function normalizeModuleAndLoadMetadata( initializeReexports = false, lazy = false, esNamespaceOnly = false, + filename, }, ): ModuleMetadata { if (!exportName) { @@ -136,6 +137,7 @@ export default function normalizeModuleAndLoadMetadata( const resolvedInterop = resolveImportInterop( importInterop, metadata.source, + filename, ); if (resolvedInterop === "none") { diff --git a/packages/babel-plugin-transform-modules-commonjs/src/index.ts b/packages/babel-plugin-transform-modules-commonjs/src/index.ts index f89866366534..50da552ebfc3 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-commonjs/src/index.ts @@ -211,6 +211,7 @@ export default declare((api, options) => { ? mjsStrictNamespace : strictNamespace, noIncompleteNsImportDetection, + filename: this.file.opts.filename, }, ); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js b/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js index 17e890da0084..d10f2e4e3894 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js @@ -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 = ` @@ -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" }], @@ -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); }); From eabbabafefec3dc71da37fc5d058ed83a4c52b39 Mon Sep 17 00:00:00 2001 From: Nick Heiner Date: Mon, 11 Apr 2022 12:38:55 -0400 Subject: [PATCH 2/5] Pass filename in more places --- packages/babel-plugin-transform-modules-amd/src/index.ts | 1 + packages/babel-plugin-transform-modules-umd/src/index.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/babel-plugin-transform-modules-amd/src/index.ts b/packages/babel-plugin-transform-modules-amd/src/index.ts index 71b0b7585c86..ed96e4a5c954 100644 --- a/packages/babel-plugin-transform-modules-amd/src/index.ts +++ b/packages/babel-plugin-transform-modules-amd/src/index.ts @@ -118,6 +118,7 @@ export default declare((api, options) => { allowTopLevelThis, importInterop, noInterop, + filename: this.opts.filename, }, ); diff --git a/packages/babel-plugin-transform-modules-umd/src/index.ts b/packages/babel-plugin-transform-modules-umd/src/index.ts index ff8ef645f398..0c400aa64fd8 100644 --- a/packages/babel-plugin-transform-modules-umd/src/index.ts +++ b/packages/babel-plugin-transform-modules-umd/src/index.ts @@ -160,6 +160,7 @@ export default declare((api, options) => { allowTopLevelThis, noInterop, importInterop, + filename: this.opts.filename, }, ); From 7638a6e790428b883bbd46a10d42ac2866fb9b1c Mon Sep 17 00:00:00 2001 From: Nick Heiner Date: Mon, 11 Apr 2022 14:12:44 -0400 Subject: [PATCH 3/5] Change to string | undefined, since the filename may not be passed to babel --- packages/babel-helper-module-transforms/src/index.ts | 2 +- .../src/normalize-and-load-metadata.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts index 8e4d19e99aa5..1fe1e6a9fc3b 100644 --- a/packages/babel-helper-module-transforms/src/index.ts +++ b/packages/babel-helper-module-transforms/src/index.ts @@ -72,7 +72,7 @@ export function rewriteModuleStatementsAndPrepareHeader( noInterop?; lazy?; esNamespaceOnly?; - filename: string; + filename: string | undefined; constantReexports?; enumerableModuleMeta?; noIncompleteNsImportDetection?: boolean; diff --git a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts index be9f65313f99..bd2941fba21f 100644 --- a/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts +++ b/packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts @@ -89,7 +89,11 @@ export function validateImportInteropOption( return importInterop; } -function resolveImportInterop(importInterop, source, filename: string) { +function resolveImportInterop( + importInterop, + source, + filename: string | undefined, +) { if (typeof importInterop === "function") { return validateImportInteropOption(importInterop(source, filename)); } From 5f712229771fd7369e0ca9b929730f1af15e7f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 13 Apr 2022 23:40:48 +0200 Subject: [PATCH 4/5] Add more tests --- .../test/importInterop-function.js | 114 ++++++++++----- .../test/importInterop-function.js | 107 +++++++++----- .../test/importInterop-function.js | 138 +++++++++++------- 3 files changed, 232 insertions(+), 127 deletions(-) diff --git a/packages/babel-plugin-transform-modules-amd/test/importInterop-function.js b/packages/babel-plugin-transform-modules-amd/test/importInterop-function.js index 856ee6dbd0cc..8a653d902dca 100644 --- a/packages/babel-plugin-transform-modules-amd/test/importInterop-function.js +++ b/packages/babel-plugin-transform-modules-amd/test/importInterop-function.js @@ -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); + }); }); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js b/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js index d10f2e4e3894..aec4e52b6787 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js @@ -3,53 +3,82 @@ 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"; - - a(); - b(); - c(); - `; - - const importInterop = jest.fn(source => { - if (source === "a") return "babel"; - else if (source === "b") return "node"; - else if (source === "c") return "none"; - }); +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; + } + + 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"; + }; - const filename = "path/to/fake-filename.js"; + expect(transform(code, importInterop)).toMatchInlineSnapshot(` + "\\"use strict\\"; - const output = babel.transformSync(code, { - configFile: false, - filename, - ast: false, - plugins: [ - [externalHelpers, { helperVersion: "7.100.0" }], - [transformCommonjs, { importInterop }], - ], - }).code; + var _a = babelHelpers.interopRequireDefault(require(\\"a\\")); - expect(output).toMatchInlineSnapshot(` - "\\"use strict\\"; + var _b = require(\\"b\\"); - var _a = babelHelpers.interopRequireDefault(require(\\"a\\")); + var _c = require(\\"c\\"); - var _b = require(\\"b\\"); + (0, _a.default)(); + + _b(); + + (0, _c.default)();" + `); + }); - var _c = require(\\"c\\"); + it("gets called with the filename if present", () => { + const code = ` + import a from "a"; + import b from "b"; + `; - (0, _a.default)(); + const importInterop = jest.fn(() => "babel"); - _b(); + const filename = "path/to/fake-filename.js"; - (0, _c.default)();" - `); + transform(code, importInterop, filename); - const resolvedFilename = path.resolve(filename); - expect(importInterop).toHaveBeenCalledWith("a", resolvedFilename); - expect(importInterop).toHaveBeenCalledWith("b", resolvedFilename); - expect(importInterop).toHaveBeenCalledWith("c", resolvedFilename); + 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); + }); }); diff --git a/packages/babel-plugin-transform-modules-umd/test/importInterop-function.js b/packages/babel-plugin-transform-modules-umd/test/importInterop-function.js index a10776d04680..08b9d80dae67 100644 --- a/packages/babel-plugin-transform-modules-umd/test/importInterop-function.js +++ b/packages/babel-plugin-transform-modules-umd/test/importInterop-function.js @@ -1,55 +1,93 @@ import * as babel from "@babel/core"; import transformUmd 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" }], - [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)(); - });" - `); +describe("'importInterop'", () => { + function transform(code, importInterop, filename) { + return babel.transformSync(code, { + configFile: false, + filename, + ast: false, + plugins: [ + [externalHelpers, { helperVersion: "7.100.0" }], + [transformUmd, { 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(` + "(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)(); + });" + `); + }); + + 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); + }); }); From d5951e1d5c3a87ed4247692fb339a2e1923de686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 13 Apr 2022 23:41:14 +0200 Subject: [PATCH 5/5] Fix bug in the other two transforms --- packages/babel-plugin-transform-modules-amd/src/index.ts | 2 +- packages/babel-plugin-transform-modules-umd/src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-modules-amd/src/index.ts b/packages/babel-plugin-transform-modules-amd/src/index.ts index ed96e4a5c954..a9191d7e5539 100644 --- a/packages/babel-plugin-transform-modules-amd/src/index.ts +++ b/packages/babel-plugin-transform-modules-amd/src/index.ts @@ -118,7 +118,7 @@ export default declare((api, options) => { allowTopLevelThis, importInterop, noInterop, - filename: this.opts.filename, + filename: this.file.opts.filename, }, ); diff --git a/packages/babel-plugin-transform-modules-umd/src/index.ts b/packages/babel-plugin-transform-modules-umd/src/index.ts index 0c400aa64fd8..1885b0043228 100644 --- a/packages/babel-plugin-transform-modules-umd/src/index.ts +++ b/packages/babel-plugin-transform-modules-umd/src/index.ts @@ -160,7 +160,7 @@ export default declare((api, options) => { allowTopLevelThis, noInterop, importInterop, - filename: this.opts.filename, + filename: this.file.opts.filename, }, );