Skip to content

Commit

Permalink
Fix compatibility with Node.js 20.6 (#15947)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 8, 2023
1 parent ab36c3e commit 2223569
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -24,7 +24,7 @@ aliases:
executors:
node-executor:
docker:
- image: cimg/node:20.5
- image: cimg/node:current
working_directory: ~/babel

jobs:
Expand Down
32 changes: 16 additions & 16 deletions .github/workflows/ci.yml
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: 'Check or update Yarn cache (fix w/ "yarn install")'
Expand All @@ -39,7 +39,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: 'Check for unmet constraints (fix w/ "yarn constraints --fix")'
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Generate coverage report
Expand All @@ -90,7 +90,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Use ESM and build
Expand All @@ -117,7 +117,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Build babel artifacts
Expand Down Expand Up @@ -157,7 +157,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Build babel artifacts
Expand All @@ -182,7 +182,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Install
Expand Down Expand Up @@ -212,7 +212,7 @@ jobs:
- name: Use Node.js latest # Run yarn on latest node
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Install
Expand Down Expand Up @@ -256,7 +256,7 @@ jobs:
if: matrix.node-version == '6' || matrix.node-version == '8' || matrix.node-version == '10'
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest

build-babel8:
name: Build Babel 8 Artifacts
Expand All @@ -271,7 +271,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Use ESM
Expand Down Expand Up @@ -343,7 +343,7 @@ jobs:
- name: Use Node.js 18
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Install
Expand All @@ -369,7 +369,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Install
Expand Down Expand Up @@ -398,7 +398,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
# See https://github.com/babel/babel/pull/12906
Expand Down Expand Up @@ -504,7 +504,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
cache: "yarn"
- name: Install
Expand All @@ -529,7 +529,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
- name: Checkout test runner
uses: actions/checkout@v4
Expand Down Expand Up @@ -566,7 +566,7 @@ jobs:
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: 20.5.x
node-version: latest
check-latest: true
- name: Install
run: yarn install
Expand Down
51 changes: 51 additions & 0 deletions babel.config.js
Expand Up @@ -303,6 +303,15 @@ module.exports = function (api) {
].map(normalize),
plugins: [pluginImportMetaUrl],
},
// See comment in the plugin
convertESM && {
test: [
"./packages/babel-core/src/index.ts",
"./packages/babel-types/src/index.ts",
"./packages/babel-traverse/src/index.ts",
],
plugins: [pluginInjectErrorBugNode_20_6_0],
},
{
test: sources.map(source => normalize(source.replace("/src", "/test"))),
plugins: [
Expand Down Expand Up @@ -954,3 +963,45 @@ function pluginReplaceNavigator({ template }) {
},
};
}

// Workaround for https://github.com/nodejs/node/issues/49497, since
// Node.js is taking ages to release the fix.
// This bug only affects CJS packages that are imported from ESM. We only apply
// it to packages that have cycles in their entrypoint. They can be easily
// caught by our CI in ESM mode on Node.js 20.6.0:
// - @babel/core
// - @babel/types
// - @babel/traverse
//
// We will remove this workaround after one week that Node.js 20.6.1 has been
// released, so that all CIs using `node:latest` will be using the new version.
function pluginInjectErrorBugNode_20_6_0({ template }) {
const flag = "___internal__alreadyRunning";

return {
post({ path }) {
path.unshiftContainer(
"body",
// We use `"${flag}" + ""` so that the Node.js ESM-CJS
// integration does not detect it as an export.
template.statement.ast`
if (typeof process === "object" && process.version === "v20.6.0") {
if (exports["${flag}" + ""]) return;
Object.defineProperty(exports, "${flag}", {
value: true,
enumerable: false,
configurable: true,
});
}
`
);
path.pushContainer(
"body",
template.statement.ast`
if (typeof process === "object" && process.version === "v20.6.0")
delete exports["${flag}" + ""];
`
);
},
};
}
5 changes: 5 additions & 0 deletions jest.config.js
Expand Up @@ -60,6 +60,11 @@ module.exports = {
...(process.env.BABEL_COVERAGE === "true"
? ["<rootDir>/packages/babel-standalone/"]
: []),
// Node.js 20.6.0 has a bug that causes importing all previous Babel
// versions to fail. This test relies on Babel 7.12, so let's skip it.
...(process.version === "v20.6.0"
? ["/babel-preset-env/test/regressions.js"]
: []),
],
testEnvironment: "node",
transformIgnorePatterns: [
Expand Down
27 changes: 0 additions & 27 deletions packages/babel-core/src/config/files/import-meta-resolve.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/babel-core/src/config/files/plugins.ts
Expand Up @@ -9,7 +9,7 @@ import { isAsync } from "../../gensync-utils/async.ts";
import loadCodeDefault, { supportsESM } from "./module-types.ts";
import { fileURLToPath, pathToFileURL } from "url";

import importMetaResolve from "./import-meta-resolve.ts";
import { resolve as importMetaResolve } from "../../vendor/import-meta-resolve.ts";

import { createRequire } from "module";
const require = createRequire(import.meta.url);
Expand Down Expand Up @@ -146,8 +146,8 @@ function tryRequireResolve(
}

function tryImportMetaResolve(
id: Parameters<ImportMeta["resolve"]>[0],
options: Parameters<ImportMeta["resolve"]>[1],
id: Parameters<typeof importMetaResolve>[0],
options: Parameters<typeof importMetaResolve>[1],
): Result<string> {
try {
return { error: null, value: importMetaResolve(id, options) };
Expand Down

0 comments on commit 2223569

Please sign in to comment.