Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass filename to
importInterop
method (#14456)
Co-authored-by: Nick Heiner <nheiner@netflix.com> Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
- Loading branch information
1 parent
a3a63d2
commit 92ffff4
Showing
8 changed files
with
247 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 76 additions & 38 deletions
114
packages/babel-plugin-transform-modules-amd/test/importInterop-function.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 69 additions & 31 deletions
100
packages/babel-plugin-transform-modules-commonjs/test/importInterop-function.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.