Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve CJS compat with ESM-based
@babel/core
(#15137)
Co-authored-by: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com>
- Loading branch information
1 parent
43dce19
commit ca52e08
Showing
16 changed files
with
286 additions
and
5 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
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { execFile } from "child_process"; | ||
import { createRequire } from "module"; | ||
import { outputType } from "./helpers/esm.js"; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
async function run(name) { | ||
return new Promise((res, rej) => { | ||
execFile( | ||
process.execPath, | ||
[require.resolve(`./fixtures/esm-cjs-integration/${name}`)], | ||
{ env: process.env }, | ||
(error, stdout, stderr) => { | ||
if (error) rej(error); | ||
res({ stdout: stdout.toString(), stderr: stderr.toString() }); | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
(outputType === "module" ? describe : describe.skip)("usage from cjs", () => { | ||
it("lazy plugin required", async () => { | ||
expect(await run("lazy-plugin-required.cjs")).toMatchInlineSnapshot(` | ||
Object { | ||
"stderr": "", | ||
"stdout": "\\"Replaced!\\"; | ||
", | ||
} | ||
`); | ||
}); | ||
|
||
it("lazy plugin as config string", async () => { | ||
expect(await run("lazy-plugin-as-string.cjs")).toMatchInlineSnapshot(` | ||
Object { | ||
"stderr": "", | ||
"stdout": "\\"Replaced!\\"; | ||
", | ||
} | ||
`); | ||
}); | ||
|
||
it("eager plugin required", async () => { | ||
await expect(run("eager-plugin-required.cjs")).rejects.toThrow( | ||
"The `types` export of @babel/core is only accessible from" + | ||
" the CommonJS version after that the ESM version is loaded.", | ||
); | ||
}); | ||
|
||
it("eager plugin required after dynamic esm import", async () => { | ||
expect(await run("eager-plugin-required-after-dynamic-esm-import.cjs")) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"stderr": "", | ||
"stdout": "\\"Replaced!\\"; | ||
", | ||
} | ||
`); | ||
}); | ||
|
||
it("eager plugin required after static esm import", async () => { | ||
expect(await run("eager-plugin-required-after-static-esm-import.mjs")) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"stderr": "", | ||
"stdout": "\\"Replaced!\\"; | ||
", | ||
} | ||
`); | ||
}); | ||
|
||
it("eager plugin as config string", async () => { | ||
expect(await run("eager-plugin-as-string.cjs")).toMatchInlineSnapshot(` | ||
Object { | ||
"stderr": "", | ||
"stdout": "\\"Replaced!\\"; | ||
", | ||
} | ||
`); | ||
}); | ||
|
||
it("transformSync", async () => { | ||
await expect(run("transform-sync.cjs")).rejects.toThrow( | ||
"The `transformSync` export of @babel/core is only callable from" + | ||
" the CommonJS version after that the ESM version is loaded.", | ||
); | ||
}); | ||
|
||
it("transformSync after dynamic esm import", async () => { | ||
expect(await run("transform-sync-after-dynamic-esm-import.cjs")) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"stderr": "", | ||
"stdout": "REPLACE_ME; | ||
", | ||
} | ||
`); | ||
}); | ||
|
||
it("transformSync after static esm import", async () => { | ||
expect(await run("transform-sync-after-static-esm-import.mjs")) | ||
.toMatchInlineSnapshot(` | ||
Object { | ||
"stderr": "", | ||
"stdout": "REPLACE_ME; | ||
", | ||
} | ||
`); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/babel-core/test/fixtures/esm-cjs-integration/eager-plugin-as-string.cjs
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const babel = require("../../../cjs-proxy.cjs"); | ||
|
||
babel | ||
.transformAsync("REPLACE_ME;", { | ||
configFile: false, | ||
plugins: [__dirname + "/plugins/eager.cjs"], | ||
}) | ||
.then(out => console.log(out.code), console.error); |
8 changes: 8 additions & 0 deletions
8
...core/test/fixtures/esm-cjs-integration/eager-plugin-required-after-dynamic-esm-import.cjs
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import("../../../lib/index.js") | ||
.then(babel => | ||
babel.transformAsync("REPLACE_ME;", { | ||
configFile: false, | ||
plugins: [require("./plugins/eager.cjs")], | ||
}), | ||
) | ||
.then(out => console.log(out.code), console.error); |
8 changes: 8 additions & 0 deletions
8
...-core/test/fixtures/esm-cjs-integration/eager-plugin-required-after-static-esm-import.mjs
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as babel from "../../../lib/index.js"; | ||
|
||
babel | ||
.transformAsync("REPLACE_ME;", { | ||
configFile: false, | ||
plugins: [(await import("./plugins/eager.cjs")).default], | ||
}) | ||
.then(out => console.log(out.code), console.error); |
14 changes: 14 additions & 0 deletions
14
packages/babel-core/test/fixtures/esm-cjs-integration/eager-plugin-required.cjs
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* This test throws an error, because the plugin accesses | ||
* @babel/core's CJS .types export before that the ESM | ||
* version is loaded. | ||
*/ | ||
|
||
const babel = require("../../../cjs-proxy.cjs"); | ||
|
||
babel | ||
.transformAsync("REPLACE_ME;", { | ||
configFile: false, | ||
plugins: [require("./plugins/eager.cjs")], | ||
}) | ||
.then(out => console.log(out.code), console.error); |
8 changes: 8 additions & 0 deletions
8
packages/babel-core/test/fixtures/esm-cjs-integration/lazy-plugin-as-string.cjs
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const babel = require("../../../cjs-proxy.cjs"); | ||
|
||
babel | ||
.transformAsync("REPLACE_ME;", { | ||
configFile: false, | ||
plugins: [__dirname + "/plugins/lazy.cjs"], | ||
}) | ||
.then(out => console.log(out.code), console.error); |
8 changes: 8 additions & 0 deletions
8
packages/babel-core/test/fixtures/esm-cjs-integration/lazy-plugin-required.cjs
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const babel = require("../../../cjs-proxy.cjs"); | ||
|
||
babel | ||
.transformAsync("REPLACE_ME;", { | ||
configFile: false, | ||
plugins: [require("./plugins/lazy.cjs")], | ||
}) | ||
.then(out => console.log(out.code), console.error); |
13 changes: 13 additions & 0 deletions
13
packages/babel-core/test/fixtures/esm-cjs-integration/plugins/eager.cjs
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { types: t } = require("../../../../cjs-proxy.cjs"); | ||
|
||
module.exports = function () { | ||
return { | ||
visitor: { | ||
Identifier(path) { | ||
if (path.node.name === "REPLACE_ME") { | ||
path.replaceWith(t.stringLiteral("Replaced!")); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
34 changes: 34 additions & 0 deletions
34
packages/babel-core/test/fixtures/esm-cjs-integration/plugins/lazy.cjs
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
import { types as t } from "../../../../cjs-proxy.cjs"; | ||
export default function () { | ||
return { | ||
visitor: { | ||
Identifier(path) { | ||
if (path.node.name === "REPLACE_ME") { | ||
path.replaceWith(t.stringLiteral("Replaced!")); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true, | ||
}); | ||
exports["default"] = _default; | ||
var _core = require("../../../../cjs-proxy.cjs"); | ||
function _default() { | ||
return { | ||
visitor: { | ||
Identifier: function Identifier(path) { | ||
if (path.node.name === "REPLACE_ME") { | ||
path.replaceWith(_core.types.stringLiteral("Replaced!")); | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
3 changes: 3 additions & 0 deletions
3
.../babel-core/test/fixtures/esm-cjs-integration/transform-sync-after-dynamic-esm-import.cjs
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import("../../../lib/index.js") | ||
.then(babel => babel.transformSync("REPLACE_ME;", { configFile: false })) | ||
.then(out => console.log(out.code), console.error); |
4 changes: 4 additions & 0 deletions
4
...s/babel-core/test/fixtures/esm-cjs-integration/transform-sync-after-static-esm-import.mjs
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import * as babel from "../../../lib/index.js"; | ||
|
||
const out = babel.transformSync("REPLACE_ME;", { configFile: false }); | ||
console.log(out.code); |
9 changes: 9 additions & 0 deletions
9
packages/babel-core/test/fixtures/esm-cjs-integration/transform-sync.cjs
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* This test throws an error, because the CJS .transformSync | ||
* is called before that the ESM version is loaded. | ||
*/ | ||
|
||
const babel = require("../../../cjs-proxy.cjs"); | ||
|
||
const out = babel.transformSync("REPLACE_ME;", { configFile: false }); | ||
console.log(out.code); |
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