[internal] Use the Node.js behavior for default imports (#12795)
This commit is contained in:
parent
ac758f7d70
commit
d9ebfab07f
@ -158,6 +158,7 @@ module.exports = function (api) {
|
|||||||
|
|
||||||
convertESM ? "@babel/proposal-export-namespace-from" : null,
|
convertESM ? "@babel/proposal-export-namespace-from" : null,
|
||||||
convertESM ? "@babel/transform-modules-commonjs" : null,
|
convertESM ? "@babel/transform-modules-commonjs" : null,
|
||||||
|
convertESM ? pluginNodeImportInterop : null,
|
||||||
|
|
||||||
pluginPackageJsonMacro,
|
pluginPackageJsonMacro,
|
||||||
|
|
||||||
@ -413,3 +414,54 @@ function pluginPackageJsonMacro({ types: t }) {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Match the Node.js behavior (the default import is module.exports)
|
||||||
|
function pluginNodeImportInterop({ template }) {
|
||||||
|
return {
|
||||||
|
visitor: {
|
||||||
|
ImportDeclaration(path) {
|
||||||
|
const specifiers = path.get("specifiers");
|
||||||
|
if (specifiers.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { source } = path.node;
|
||||||
|
if (
|
||||||
|
source.value.startsWith(".") ||
|
||||||
|
source.value.startsWith("@babel/") ||
|
||||||
|
source.value === "charcodes"
|
||||||
|
) {
|
||||||
|
// For internal modules, it's either "all CJS" or "all ESM".
|
||||||
|
// We don't need to worry about interop.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const defImport = specifiers.find(s => s.isImportDefaultSpecifier());
|
||||||
|
const nsImport = specifiers.find(s => s.isImportNamespaceSpecifier());
|
||||||
|
|
||||||
|
if (defImport) {
|
||||||
|
path.insertAfter(
|
||||||
|
template.ast`
|
||||||
|
const ${defImport.node.local} = require(${source});
|
||||||
|
`
|
||||||
|
);
|
||||||
|
defImport.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nsImport) {
|
||||||
|
path.insertAfter(
|
||||||
|
template.ast`
|
||||||
|
const ${nsImport.node.local} = {
|
||||||
|
...require(${source}),
|
||||||
|
default: require(${source}),
|
||||||
|
};
|
||||||
|
`
|
||||||
|
);
|
||||||
|
nsImport.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path.node.specifiers.length === 0) path.remove();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
declare module "babel-plugin-polyfill-regenerator" {
|
declare module "babel-plugin-polyfill-regenerator" {
|
||||||
declare module.exports: Function;
|
declare module.exports: { default: Function };
|
||||||
}
|
}
|
||||||
declare module "babel-plugin-polyfill-corejs2" {
|
declare module "babel-plugin-polyfill-corejs2" {
|
||||||
declare module.exports: Function;
|
declare module.exports: { default: Function };
|
||||||
}
|
}
|
||||||
declare module "babel-plugin-polyfill-corejs3" {
|
declare module "babel-plugin-polyfill-corejs3" {
|
||||||
declare module.exports: Function;
|
declare module.exports: { default: Function };
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,10 +13,12 @@ import assert from "assert";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import vm from "vm";
|
import vm from "vm";
|
||||||
import checkDuplicatedNodes from "babel-check-duplicated-nodes";
|
|
||||||
import QuickLRU from "quick-lru";
|
import QuickLRU from "quick-lru";
|
||||||
import escapeRegExp from "./escape-regexp.cjs";
|
import escapeRegExp from "./escape-regexp.cjs";
|
||||||
|
|
||||||
|
import _checkDuplicatedNodes from "babel-check-duplicated-nodes";
|
||||||
|
const checkDuplicatedNodes = _checkDuplicatedNodes.default;
|
||||||
|
|
||||||
const EXTERNAL_HELPERS_VERSION = "7.100.0";
|
const EXTERNAL_HELPERS_VERSION = "7.100.0";
|
||||||
|
|
||||||
const cachedScripts = new QuickLRU({ maxSize: 10 });
|
const cachedScripts = new QuickLRU({ maxSize: 10 });
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
/// <reference path="../../../lib/third-party-libs.d.ts" />
|
/// <reference path="../../../lib/third-party-libs.d.ts" />
|
||||||
|
|
||||||
import jsTokens, * as jsTokensNs from "js-tokens";
|
|
||||||
import type { Token, JSXToken } from "js-tokens";
|
import type { Token, JSXToken } from "js-tokens";
|
||||||
|
import jsTokens from "js-tokens";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
isStrictReservedWord,
|
isStrictReservedWord,
|
||||||
isKeyword,
|
isKeyword,
|
||||||
@ -158,9 +159,6 @@ if (process.env.BABEL_8_BREAKING) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} 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.
|
* 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) {
|
tokenize = function* (text: string) {
|
||||||
let match;
|
let match;
|
||||||
while ((match = (jsTokens as any).exec(text))) {
|
while ((match = (jsTokens as any).default.exec(text))) {
|
||||||
const token = matchToToken(match);
|
const token = (jsTokens as any).matchToToken(match);
|
||||||
|
|
||||||
yield {
|
yield {
|
||||||
type: getTokenType(token, match.index, text),
|
type: getTokenType(token, match.index, text),
|
||||||
|
|||||||
@ -5,9 +5,12 @@ import { types as t } from "@babel/core";
|
|||||||
import { hasMinVersion } from "./helpers";
|
import { hasMinVersion } from "./helpers";
|
||||||
import getRuntimePath from "./get-runtime-path";
|
import getRuntimePath from "./get-runtime-path";
|
||||||
|
|
||||||
import pluginCorejs2 from "babel-plugin-polyfill-corejs2";
|
import _pluginCorejs2 from "babel-plugin-polyfill-corejs2";
|
||||||
import pluginCorejs3 from "babel-plugin-polyfill-corejs3";
|
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
|
||||||
import pluginRegenerator from "babel-plugin-polyfill-regenerator";
|
import _pluginRegenerator from "babel-plugin-polyfill-regenerator";
|
||||||
|
const pluginCorejs2 = _pluginCorejs2.default;
|
||||||
|
const pluginCorejs3 = _pluginCorejs3.default;
|
||||||
|
const pluginRegenerator = _pluginRegenerator.default;
|
||||||
|
|
||||||
const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
|
const pluginsCompat = "#__secret_key__@babel/runtime__compatibility";
|
||||||
|
|
||||||
|
|||||||
@ -16,9 +16,12 @@ import overlappingPlugins from "@babel/compat-data/overlapping-plugins";
|
|||||||
import removeRegeneratorEntryPlugin from "./polyfills/regenerator";
|
import removeRegeneratorEntryPlugin from "./polyfills/regenerator";
|
||||||
import legacyBabelPolyfillPlugin from "./polyfills/babel-polyfill";
|
import legacyBabelPolyfillPlugin from "./polyfills/babel-polyfill";
|
||||||
|
|
||||||
import pluginCoreJS2 from "babel-plugin-polyfill-corejs2";
|
import _pluginCoreJS2 from "babel-plugin-polyfill-corejs2";
|
||||||
import pluginCoreJS3 from "babel-plugin-polyfill-corejs3";
|
import _pluginCoreJS3 from "babel-plugin-polyfill-corejs3";
|
||||||
import pluginRegenerator from "babel-plugin-polyfill-regenerator";
|
import _pluginRegenerator from "babel-plugin-polyfill-regenerator";
|
||||||
|
const pluginCoreJS2 = _pluginCoreJS2.default;
|
||||||
|
const pluginCoreJS3 = _pluginCoreJS3.default;
|
||||||
|
const pluginRegenerator = _pluginRegenerator.default;
|
||||||
|
|
||||||
import getTargets, {
|
import getTargets, {
|
||||||
prettifyTargets,
|
prettifyTargets,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user