Skip to content

Commit d9ebfab

Browse files
[internal] Use the Node.js behavior for default imports (#12795)
1 parent ac758f7 commit d9ebfab

6 files changed

Lines changed: 74 additions & 16 deletions

File tree

babel.config.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ module.exports = function (api) {
158158

159159
convertESM ? "@babel/proposal-export-namespace-from" : null,
160160
convertESM ? "@babel/transform-modules-commonjs" : null,
161+
convertESM ? pluginNodeImportInterop : null,
161162

162163
pluginPackageJsonMacro,
163164

@@ -413,3 +414,54 @@ function pluginPackageJsonMacro({ types: t }) {
413414
},
414415
};
415416
}
417+
418+
// Match the Node.js behavior (the default import is module.exports)
419+
function pluginNodeImportInterop({ template }) {
420+
return {
421+
visitor: {
422+
ImportDeclaration(path) {
423+
const specifiers = path.get("specifiers");
424+
if (specifiers.length === 0) {
425+
return;
426+
}
427+
428+
const { source } = path.node;
429+
if (
430+
source.value.startsWith(".") ||
431+
source.value.startsWith("@babel/") ||
432+
source.value === "charcodes"
433+
) {
434+
// For internal modules, it's either "all CJS" or "all ESM".
435+
// We don't need to worry about interop.
436+
return;
437+
}
438+
439+
const defImport = specifiers.find(s => s.isImportDefaultSpecifier());
440+
const nsImport = specifiers.find(s => s.isImportNamespaceSpecifier());
441+
442+
if (defImport) {
443+
path.insertAfter(
444+
template.ast`
445+
const ${defImport.node.local} = require(${source});
446+
`
447+
);
448+
defImport.remove();
449+
}
450+
451+
if (nsImport) {
452+
path.insertAfter(
453+
template.ast`
454+
const ${nsImport.node.local} = {
455+
...require(${source}),
456+
default: require(${source}),
457+
};
458+
`
459+
);
460+
nsImport.remove();
461+
}
462+
463+
if (path.node.specifiers.length === 0) path.remove();
464+
},
465+
},
466+
};
467+
}

lib/babel-polyfills.js.flow

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
declare module "babel-plugin-polyfill-regenerator" {
2-
declare module.exports: Function;
2+
declare module.exports: { default: Function };
33
}
44
declare module "babel-plugin-polyfill-corejs2" {
5-
declare module.exports: Function;
5+
declare module.exports: { default: Function };
66
}
77
declare module "babel-plugin-polyfill-corejs3" {
8-
declare module.exports: Function;
8+
declare module.exports: { default: Function };
99
}

packages/babel-helper-transform-fixture-test-runner/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import assert from "assert";
1313
import fs from "fs";
1414
import path from "path";
1515
import vm from "vm";
16-
import checkDuplicatedNodes from "babel-check-duplicated-nodes";
1716
import QuickLRU from "quick-lru";
1817
import escapeRegExp from "./escape-regexp.cjs";
1918

19+
import _checkDuplicatedNodes from "babel-check-duplicated-nodes";
20+
const checkDuplicatedNodes = _checkDuplicatedNodes.default;
21+
2022
const EXTERNAL_HELPERS_VERSION = "7.100.0";
2123

2224
const cachedScripts = new QuickLRU({ maxSize: 10 });

packages/babel-highlight/src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// <reference path="../../../lib/third-party-libs.d.ts" />
22

3-
import jsTokens, * as jsTokensNs from "js-tokens";
43
import type { Token, JSXToken } from "js-tokens";
4+
import jsTokens from "js-tokens";
5+
56
import {
67
isStrictReservedWord,
78
isKeyword,
@@ -158,9 +159,6 @@ if (process.env.BABEL_8_BREAKING) {
158159
}
159160
};
160161
} else {
161-
// This is only available in js-tokens@4, and not in js-tokens@6
162-
const { matchToToken } = jsTokensNs as any;
163-
164162
/**
165163
* RegExp to test for what seems to be a JSX tag name.
166164
*/
@@ -204,8 +202,8 @@ if (process.env.BABEL_8_BREAKING) {
204202

205203
tokenize = function* (text: string) {
206204
let match;
207-
while ((match = (jsTokens as any).exec(text))) {
208-
const token = matchToToken(match);
205+
while ((match = (jsTokens as any).default.exec(text))) {
206+
const token = (jsTokens as any).matchToToken(match);
209207

210208
yield {
211209
type: getTokenType(token, match.index, text),

packages/babel-plugin-transform-runtime/src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { types as t } from "@babel/core";
55
import { hasMinVersion } from "./helpers";
66
import getRuntimePath from "./get-runtime-path";
77

8-
import pluginCorejs2 from "babel-plugin-polyfill-corejs2";
9-
import pluginCorejs3 from "babel-plugin-polyfill-corejs3";
10-
import pluginRegenerator from "babel-plugin-polyfill-regenerator";
8+
import _pluginCorejs2 from "babel-plugin-polyfill-corejs2";
9+
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
10+
import _pluginRegenerator from "babel-plugin-polyfill-regenerator";
11+
const pluginCorejs2 = _pluginCorejs2.default;
12+
const pluginCorejs3 = _pluginCorejs3.default;
13+
const pluginRegenerator = _pluginRegenerator.default;
1114

1215
const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
1316

packages/babel-preset-env/src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ import overlappingPlugins from "@babel/compat-data/overlapping-plugins";
1616
import removeRegeneratorEntryPlugin from "./polyfills/regenerator";
1717
import legacyBabelPolyfillPlugin from "./polyfills/babel-polyfill";
1818

19-
import pluginCoreJS2 from "babel-plugin-polyfill-corejs2";
20-
import pluginCoreJS3 from "babel-plugin-polyfill-corejs3";
21-
import pluginRegenerator from "babel-plugin-polyfill-regenerator";
19+
import _pluginCoreJS2 from "babel-plugin-polyfill-corejs2";
20+
import _pluginCoreJS3 from "babel-plugin-polyfill-corejs3";
21+
import _pluginRegenerator from "babel-plugin-polyfill-regenerator";
22+
const pluginCoreJS2 = _pluginCoreJS2.default;
23+
const pluginCoreJS3 = _pluginCoreJS3.default;
24+
const pluginRegenerator = _pluginRegenerator.default;
2225

2326
import getTargets, {
2427
prettifyTargets,

0 commit comments

Comments
 (0)