Skip to content

Commit

Permalink
Fix loading of modules with TLA in Node.js 23 (#16926)
Browse files Browse the repository at this point in the history
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
  • Loading branch information
nicolo-ribaudo and JLHwung authored Oct 24, 2024
1 parent 10f15bb commit 56658ac
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export default [
"itNoWin32",
"itESM",
"nodeGte8",
"nodeGte14",
"nodeGte12",
"nodeGte20",
"nodeGte23",
Expand Down
22 changes: 12 additions & 10 deletions packages/babel-core/src/config/files/module-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function loadCjsDefault(filepath: string) {
const loadMjsFromPath = endHiddenCallStack(async function loadMjsFromPath(
filepath: string,
) {
const url = pathToFileURL(filepath).toString();
// Add ?import as a workaround for https://github.com/nodejs/node/issues/55500
const url = pathToFileURL(filepath).toString() + "?import";

if (process.env.BABEL_8_BREAKING) {
return await import(url);
Expand Down Expand Up @@ -132,16 +133,17 @@ export default function* loadCodeDefault(
);
}
} catch (e) {
if (
e.code === "ERR_REQUIRE_ASYNC_MODULE" &&
!(async ??= yield* isAsync())
) {
throw new ConfigError(tlaError, filepath);
}
if (
e.code !== "ERR_REQUIRE_ESM" &&
(process.env.BABEL_8_BREAKING || ext !== ".mjs")
if (e.code === "ERR_REQUIRE_ASYNC_MODULE") {
if (!(async ??= yield* isAsync())) {
throw new ConfigError(tlaError, filepath);
}
// fall through: require() failed due to TLA
} else if (
e.code === "ERR_REQUIRE_ESM" ||
(!process.env.BABEL_8_BREAKING && ext === ".mjs")
) {
// fall through: require() failed due to ESM
} else {
throw e;
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-core/test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { itGte, itESM, itLt } from "$repo-utils";

// "minNodeVersion": "8.0.0" <-- For Ctrl+F when dropping node 6
const nodeGte8 = itGte("8.0.0");
const nodeGte14 = itGte("14.8.0");

// "minNodeVersion": "23.0.0" <-- For Ctrl+F when dropping node 22
const nodeGte23 = itGte("23.0.0");
Expand Down Expand Up @@ -244,6 +245,14 @@ describe("asynchronicity", () => {
code: `"success"`,
});
});

nodeGte14("called asynchronously when contain TLA", async () => {
process.chdir("plugin-mjs-tla-native");

await expect(spawnTransformAsync()).resolves.toMatchObject({
code: `"success"`,
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
plugins: ["./plugin.js"],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
await Promise.resolve(0);

export default function plugin({ types: t }) {
return {
visitor: {
Program(path) {
path.pushContainer("body", t.stringLiteral("success"));
},
},
};
}
6 changes: 1 addition & 5 deletions scripts/integration-tests/e2e-react-native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ startLocalRegistry "$root"/verdaccio-config.yml

# Create a React Native project
cd /tmp
# Remove the patch when react-native bumps @react-native-community/cli to 12
npm install react-native
npx replace '_fs\(\)\.default\.chmodSync\(destPath, mode\);' '' node_modules/@react-native-community/cli/build/tools/copyFiles.js
npx replace 'createWriteStream\(destPath\);' 'createWriteStream(destPath, {mode});' node_modules/@react-native-community/cli/build/tools/copyFiles.js
npx react-native init rnbabel
YARN_ENABLE_IMMUTABLE_INSTALLS=false npx @react-native-community/cli init rnbabel
cd rnbabel

if [ "$BABEL_8_BREAKING" = true ] ; then
Expand Down

0 comments on commit 56658ac

Please sign in to comment.