diff --git a/babel.config.js b/babel.config.js index fabe3ac8dafc..8f12e3d65546 100644 --- a/babel.config.js +++ b/babel.config.js @@ -150,6 +150,7 @@ module.exports = function (api) { convertESM ? "@babel/proposal-export-namespace-from" : null, convertESM ? "@babel/transform-modules-commonjs" : null, + convertESM ? pluginDefaultImportNode : null, pluginPackageJsonMacro, @@ -405,3 +406,41 @@ function pluginPackageJsonMacro({ types: t }) { }, }; } + +// Match the Node.js behavior (the default import is module.exports) +function pluginDefaultImportNode({ types: t }) { + return { + visitor: { + ImportDeclaration(path) { + const specifiers = path.get("specifiers"); + if ( + specifiers.length === 0 || + !specifiers[0].isImportDefaultSpecifier() + ) { + return; + } + + const { source } = path.node; + if ( + source.value.startsWith(".") || + source.value.startsWith("@babel/") + ) { + // For internal modules, it's either "all CJS" or "all ESM". + // We don't need to worry about interop. + return; + } + + path.insertAfter( + t.variableDeclaration("const", [ + t.variableDeclarator( + specifiers[0].node.local, + t.callExpression(t.identifier("require"), [path.node.source]) + ), + ]) + ); + + specifiers[0].remove(); + }, + }, + }; +} diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.js b/packages/babel-helper-transform-fixture-test-runner/src/index.js index 92db61e95e6d..8cf2f8133ff1 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -13,10 +13,12 @@ import assert from "assert"; import fs from "fs"; import path from "path"; import vm from "vm"; -import checkDuplicatedNodes from "babel-check-duplicated-nodes"; import QuickLRU from "quick-lru"; import escapeRegExp from "./escape-regexp.cjs"; +import _checkDuplicatedNodes from "babel-check-duplicated-nodes"; +const checkDuplicatedNodes = _checkDuplicatedNodes.default; + const cachedScripts = new QuickLRU({ maxSize: 10 }); const contextModuleCache = new WeakMap(); const sharedTestContext = createContext(); diff --git a/packages/babel-highlight/src/index.ts b/packages/babel-highlight/src/index.ts index 6ef68de5fe88..373f37edcd7e 100644 --- a/packages/babel-highlight/src/index.ts +++ b/packages/babel-highlight/src/index.ts @@ -1,7 +1,8 @@ /// -import jsTokens, * as jsTokensNs from "js-tokens"; import type { Token, JSXToken } from "js-tokens"; +import jsTokens from "js-tokens"; + import { isStrictReservedWord, isKeyword, @@ -158,9 +159,6 @@ if (process.env.BABEL_8_BREAKING) { } }; } else { - // This is only available in js-tokens@4, and not in js-tokens@6 - const { matchToToken } = jsTokensNs as any; - /** * RegExp to test for what seems to be a JSX tag name. */ @@ -204,8 +202,8 @@ if (process.env.BABEL_8_BREAKING) { tokenize = function* (text: string) { let match; - while ((match = (jsTokens as any).exec(text))) { - const token = matchToToken(match); + while ((match = (jsTokens as any).default.exec(text))) { + const token = (jsTokens as any).matchToToken(match); yield { type: getTokenType(token, match.index, text),