Skip to content

Commit

Permalink
Fix @babel/register/@babel/node compat with ESM build (#14693)
Browse files Browse the repository at this point in the history
* Fix `@babel/register` compat with ESM build

* Fix `@babel/node` compat with ESM build
  • Loading branch information
nicolo-ribaudo committed Jul 12, 2022
1 parent a34a5ae commit bcf8b22
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 31 deletions.
2 changes: 2 additions & 0 deletions babel.config.js
Expand Up @@ -725,6 +725,8 @@ function pluginAddImportExtension() {
if (
source.value.startsWith("lodash/") ||
source.value.startsWith("core-js-compat/") ||
source.value === "core-js/stable/index" ||
source.value === "regenerator-runtime/runtime" ||
source.value === "babel-plugin-dynamic-import-node/utils"
) {
source.value += ".js";
Expand Down
4 changes: 1 addition & 3 deletions jest.config.js
Expand Up @@ -43,9 +43,7 @@ module.exports = {
// Some tests require internal files of bundled packages, which are not available
// in production builds. They are marked using the .skip-bundled.js extension.
...(isPublishBundle ? ["\\.skip-bundled\\.js$"] : []),
...(LIB_USE_ESM
? ["/babel-node/", "/babel-register/", "/babel-helpers/"]
: []),
...(LIB_USE_ESM ? ["/babel-helpers/"] : []),
],
testEnvironment: "node",
transformIgnorePatterns: [
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-node/src/_babel-node.ts
Expand Up @@ -5,7 +5,7 @@ import path from "path";
import repl from "repl";
import * as babel from "@babel/core";
import vm from "vm";
import "core-js/stable";
import "core-js/stable/index";
import "regenerator-runtime/runtime";
import register from "@babel/register";
import { fileURLToPath } from "url";
Expand Down
@@ -1,3 +1,3 @@
// See https://github.com/babel/babel/pull/14025#issuecomment-986296424
// for the reason behind using setImmediate.
setImmediate(() => console.log("foo"));
// for the reason behind using setTimeout.
setTimeout(() => console.log("foo"), 50);
@@ -1,3 +1,3 @@
// See https://github.com/babel/babel/pull/14025#issuecomment-986296424
// for the reason behind using setImmediate.
setImmediate(() => console.log("foo"));
// for the reason behind using setTimeout.
setTimeout(() => console.log("foo"), 50);
@@ -1,3 +1,3 @@
// See https://github.com/babel/babel/pull/14025#issuecomment-986296424
// for the reason behind using setImmediate.
setImmediate(() => console.log("foo"));
// for the reason behind using setTimeout.
setTimeout(() => console.log("foo"), 50);
4 changes: 2 additions & 2 deletions packages/babel-node/test/fixtures/cli/-b/in-files/index.js
@@ -1,3 +1,3 @@
// See https://github.com/babel/babel/pull/14025#issuecomment-986296424
// for the reason behind using setImmediate.
setImmediate(() => console.log("foo"));
// for the reason behind using setTimeout.
setTimeout(() => console.log("foo"), 50);
6 changes: 0 additions & 6 deletions packages/babel-register/package.json
Expand Up @@ -44,12 +44,6 @@
{
"exports": null
}
],
"USE_ESM": [
{
"type": "module"
},
null
]
},
"exports": {
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-register/src/experimental-worker.js
Expand Up @@ -13,7 +13,11 @@ if (major < 12 || (major === 12 && minor < 3)) {
const hook = require("./hook");
const { WorkerClient } = require("./worker-client");

const register = hook.register.bind(null, new WorkerClient());
let client;
function register(opts) {
client ||= new WorkerClient();
return hook.register(client, opts);
}

module.exports = Object.assign(register, {
revert: hook.revert,
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-register/src/index.js
Expand Up @@ -4,7 +4,9 @@
* from a compiled Babel import.
*/

if (process.env.BABEL_8_BREAKING) {
if (USE_ESM) {
module.exports = require("./experimental-worker");
} else if (process.env.BABEL_8_BREAKING) {
module.exports = require("./experimental-worker");
} else {
exports = module.exports = function (...args) {
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-register/src/worker/babel-core.js
@@ -1,3 +1,5 @@
const cache = require("./cache");

function initialize(babel) {
exports.init = null;
exports.version = babel.version;
Expand All @@ -10,11 +12,13 @@ function initialize(babel) {
exports.OptionManager = babel.OptionManager;
exports.transformSync = babel.transformSync;
}

cache.initializeCacheFilename();
}

if (process.env.BABEL_8_BREAKING) {
if (USE_ESM) {
// @ts-expect-error CJS-ESM interop.
exports.init = import("@babel/core").then(ns => initialize(ns.default));
exports.init = import("@babel/core").then(initialize);
} else {
initialize(require("@babel/core"));
}
20 changes: 12 additions & 8 deletions packages/babel-register/src/worker/cache.js
Expand Up @@ -3,17 +3,21 @@
const path = require("path");
const fs = require("fs");
const os = require("os");
const babel = require("@babel/core");
const findCacheDir = require("find-cache-dir");

const DEFAULT_CACHE_DIR =
findCacheDir({ name: "@babel/register" }) || os.homedir() || os.tmpdir();
const DEFAULT_FILENAME = path.join(
DEFAULT_CACHE_DIR,
`.babel.${babel.version}.${babel.getEnv()}.json`,
);
let FILENAME = process.env.BABEL_CACHE_PATH;

// This function needs to be exported before requiring ./babel-core, because
// there is a circular dependency between these two files.
exports.initializeCacheFilename = function () {
FILENAME ||= path.join(
findCacheDir({ name: "@babel/register" }) || os.homedir() || os.tmpdir(),
`.babel.${babel.version}.${babel.getEnv()}.json`,
);
};

const babel = require("./babel-core");

const FILENAME = process.env.BABEL_CACHE_PATH || DEFAULT_FILENAME;
let data = {};

let cacheDirty = false;
Expand Down
10 changes: 9 additions & 1 deletion packages/babel-register/test/index.js
Expand Up @@ -36,6 +36,14 @@ function resetCache() {

const OLD_JEST_MOCKS = !!jest.doMock;

let USE_ESM = false;
try {
const type = fs
.readFileSync(new URL("../../../.module-type", import.meta.url), "utf-8")
.trim();
USE_ESM = type === "module";
} catch {}

describe("@babel/register", function () {
let currentHook, currentOptions, sourceMapSupport;

Expand Down Expand Up @@ -86,7 +94,7 @@ describe("@babel/register", function () {
});
}

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM && !process.env.BABEL_8_BREAKING) {
describe("babel 7", () => {
if (!OLD_JEST_MOCKS) {
beforeEach(() => {
Expand Down
5 changes: 5 additions & 0 deletions scripts/set-module-type.js
Expand Up @@ -31,6 +31,11 @@ if (moduleType === "clean") {
fs.statSync(dir).isDirectory() && fs.existsSync(`${dir}/package.json`)
)
.forEach(dir => {
if (dir.endsWith("babel-register")) {
// This is a CJS package
return;
}

if (moduleType === "clean") {
try {
fs.unlinkSync(`${dir}/lib/package.json`);
Expand Down

0 comments on commit bcf8b22

Please sign in to comment.