From 7ec6f2c6ef01d06985a05a9c92c80f1cc79dec61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 12 Feb 2021 14:27:49 +0100 Subject: [PATCH] [internal] Use the Node.js behavior for default imports --- babel.config.js | 39 +++++++++++++++++++ .../src/index.js | 4 +- packages/babel-highlight/src/index.js | 9 ++--- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/babel.config.js b/babel.config.js index ff08dbe3543f..5aaffb7bc732 100644 --- a/babel.config.js +++ b/babel.config.js @@ -125,6 +125,7 @@ module.exports = function (api) { convertESM ? "@babel/proposal-export-namespace-from" : null, convertESM ? "@babel/transform-modules-commonjs" : null, + convertESM ? pluginDefaultImportNode : null, pluginPackageJsonMacro, @@ -382,3 +383,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 5376b5350205..dfbd227c5881 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -13,11 +13,13 @@ 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 diff from "jest-diff"; import escapeRegExp from "./escape-regexp"; +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.js b/packages/babel-highlight/src/index.js index f2639a521c1e..c51566d38b0d 100644 --- a/packages/babel-highlight/src/index.js +++ b/packages/babel-highlight/src/index.js @@ -1,4 +1,6 @@ -import jsTokens, * as jsTokensNs from "js-tokens"; +import _jsTokens from "js-tokens"; +const jsTokens = _jsTokens.default; + import { isStrictReservedWord, isKeyword, @@ -138,9 +140,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; - /** * RegExp to test for what seems to be a JSX tag name. */ @@ -185,7 +184,7 @@ if (process.env.BABEL_8_BREAKING) { tokenize = function* (text: string) { let match; while ((match = jsTokens.exec(text))) { - const token = matchToToken(match); + const token = _jsTokens.matchToToken(match); yield { type: getTokenType(token, match.index, text),