Skip to content

Commit d016c15

Browse files
Fix remaining imports to be ESM-compatible (#14674)
1 parent e482c76 commit d016c15

File tree

19 files changed

+60
-26
lines changed

19 files changed

+60
-26
lines changed

.eslintrc.cjs

+26
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ module.exports = {
9494
],
9595
rules: {
9696
"no-restricted-globals": ["error", ...cjsGlobals],
97+
"no-restricted-imports": [
98+
"error",
99+
{
100+
patterns: ["**/*.json"],
101+
paths: [
102+
{
103+
name: "semver",
104+
message:
105+
"semver's named exports are not recognized by the Node.js ESM-CJS interop.",
106+
importNames: Object.keys(require("semver")).filter(
107+
// We use it as a type import.
108+
name => name !== "SemVer"
109+
),
110+
},
111+
],
112+
},
113+
],
97114
},
98115
},
99116
{
@@ -137,6 +154,15 @@ module.exports = {
137154
],
138155
},
139156
},
157+
{
158+
files: ["packages/babel-preset-env/data/**/*.js"],
159+
rules: {
160+
"import/no-extraneous-dependencies": [
161+
"error",
162+
{ packageDir: "./packages/babel-preset-env" },
163+
],
164+
},
165+
},
140166
{
141167
files: ["scripts/**/*.js"],
142168
rules: {

babel.config.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,14 @@ const monorepoPackages = ["codemods", "eslint", "packages"]
277277
.reduce((a, b) => a.concat(b))
278278
.map(name => name.replace(/^babel-/, "@babel/"));
279279

280-
function importInteropSrc(source) {
280+
function importInteropSrc(source, filename) {
281281
if (
282282
// These internal files are "real CJS" (whose default export is
283283
// on module.exports) and not compiled ESM.
284284
source.startsWith("@babel/compat-data/") ||
285-
source.includes("babel-eslint-shared-fixtures/utils")
285+
source.includes("babel-eslint-shared-fixtures/utils") ||
286+
(source.includes("../data/") &&
287+
/babel-preset-env[\\/]src[\\/]/.test(filename))
286288
) {
287289
return "node";
288290
}

packages/babel-plugin-transform-runtime/scripts/build-dist.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from "path";
22
import fs from "fs";
33
import { createRequire } from "module";
4-
import helpers from "@babel/helpers";
4+
import * as helpers from "@babel/helpers";
55
import { transformFromAstSync, File } from "@babel/core";
66
import template from "@babel/template";
7-
import t from "@babel/types";
7+
import * as t from "@babel/types";
88
import { fileURLToPath } from "url";
99

1010
import transformRuntime from "../lib/index.js";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("core-js-compat/data.json");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "commonjs" }

packages/babel-preset-env/src/filter-items.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lt } from "semver";
1+
import semver from "semver";
22
import { minVersions } from "./available-plugins";
33

44
// $FlowIgnore
@@ -25,7 +25,7 @@ export function removeUnsupportedItems(
2525
babelVersion: string,
2626
) {
2727
items.forEach(item => {
28-
if (has(minVersions, item) && lt(babelVersion, minVersions[item])) {
28+
if (has(minVersions, item) && semver.lt(babelVersion, minVersions[item])) {
2929
items.delete(item);
3030
}
3131
});

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { lt } from "semver";
2-
import type { SemVer } from "semver";
1+
import semver, { type SemVer } from "semver";
32
import { logPlugin } from "./debug";
43
import getOptionSpecificExcludesFor from "./get-option-specific-excludes";
54
import {
@@ -308,7 +307,7 @@ export default declarePreset((api, opts: Options) => {
308307
// @babel/core < 7.13.0 doesn't load targets (api.targets() always
309308
// returns {} thanks to @babel/helper-plugin-utils), so we always want
310309
// to fallback to the old targets behavior in this case.
311-
lt(api.version, "7.13.0") ||
310+
semver.lt(api.version, "7.13.0") ||
312311
// If any browserslist-related option is specified, fallback to the old
313312
// behavior of not using the targets specified in the top-level options.
314313
opts.targets ||

packages/babel-preset-env/src/normalize-options.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import corejs3Polyfills from "core-js-compat/data.json";
2-
import { coerce } from "semver";
3-
import type { SemVer } from "semver";
1+
import semver, { type SemVer } from "semver";
42
import corejs2Polyfills from "@babel/compat-data/corejs2-built-ins";
3+
import corejs3Polyfills from "../data/core-js-compat";
54
import { plugins as pluginsList } from "./plugins-compat-data";
65
import moduleTransformations from "./module-transformations";
76
import { TopLevelOptions, ModulesOption, UseBuiltInsOption } from "./options";
@@ -187,7 +186,7 @@ export function normalizeCoreJSOption(
187186
rawVersion = corejs;
188187
}
189188

190-
const version = rawVersion ? coerce(String(rawVersion)) : false;
189+
const version = rawVersion ? semver.coerce(String(rawVersion)) : false;
191190

192191
if (!useBuiltIns && version) {
193192
console.warn(

packages/babel-traverse/scripts/generators/asserts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import t from "@babel/types";
1+
import * as t from "@babel/types";
22

33
export default function generateAsserts() {
44
let output = `/*

packages/babel-traverse/scripts/generators/validators.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import t from "@babel/types";
2-
import virtualTypes from "../../lib/path/lib/virtual-types.js";
1+
import * as t from "@babel/types";
2+
import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
33

44
export default function generateValidators() {
55
let output = `/*
@@ -18,6 +18,9 @@ export interface NodePathValidators {
1818
}
1919

2020
for (const type of Object.keys(virtualTypes)) {
21+
// TODO: Remove this check once we stop compiling to CJS
22+
if (type === "default" || type === "__esModule") continue;
23+
2124
const { types } = virtualTypes[type];
2225
if (type[0] === "_") continue;
2326
if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {

packages/babel-traverse/scripts/generators/virtual-types.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import virtualTypes from "../../lib/path/lib/virtual-types.js";
1+
import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
22

33
export default function generateValidators() {
44
let output = `/*
@@ -11,6 +11,9 @@ export interface VirtualTypeAliases {
1111
`;
1212

1313
for (const type of Object.keys(virtualTypes)) {
14+
// TODO: Remove this check once we stop compiling to CJS
15+
if (type === "default" || type === "__esModule") continue;
16+
1417
output += ` ${type}: ${(virtualTypes[type].types || ["Node"])
1518
.map(t => `t.${t}`)
1619
.join(" | ")};`;

packages/babel-types/scripts/generators/asserts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import definitions from "../../lib/definitions/index.js";
1+
import * as definitions from "../../lib/definitions/index.js";
22

33
function addAssertHelper(type) {
44
const result =

packages/babel-types/scripts/generators/ast-types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import t from "../../lib/index.js";
1+
import * as t from "../../lib/index.js";
22
import stringifyValidator from "../utils/stringifyValidator.js";
33

44
export default function generateAstTypes() {

packages/babel-types/scripts/generators/builders.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import t from "../../lib/index.js";
2-
import definitions from "../../lib/definitions/index.js";
1+
import * as t from "../../lib/index.js";
2+
import * as definitions from "../../lib/definitions/index.js";
33
import formatBuilderName from "../utils/formatBuilderName.js";
44
import lowerFirst from "../utils/lowerFirst.js";
55
import stringifyValidator from "../utils/stringifyValidator.js";

packages/babel-types/scripts/generators/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import definitions from "../../lib/definitions/index.js";
1+
import * as definitions from "../../lib/definitions/index.js";
22

33
export default function generateConstants() {
44
let output = `/*

packages/babel-types/scripts/generators/docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import util from "util";
22
import stringifyValidator from "../utils/stringifyValidator.js";
33
import toFunctionName from "../utils/toFunctionName.js";
44

5-
import t from "../../lib/index.js";
5+
import * as t from "../../lib/index.js";
66

77
const readme = [
88
`---

packages/babel-types/scripts/generators/flow.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import t from "../../lib/index.js";
1+
import * as t from "../../lib/index.js";
22
import stringifyValidator from "../utils/stringifyValidator.js";
33
import toFunctionName from "../utils/toFunctionName.js";
44

packages/babel-types/scripts/generators/typescript-legacy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import t from "../../lib/index.js";
1+
import * as t from "../../lib/index.js";
22
import stringifyValidator from "../utils/stringifyValidator.js";
33
import toFunctionName from "../utils/toFunctionName.js";
44

packages/babel-types/scripts/generators/validators.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import definitions from "../../lib/definitions/index.js";
1+
import * as definitions from "../../lib/definitions/index.js";
22

33
const has = Function.call.bind(Object.prototype.hasOwnProperty);
44

0 commit comments

Comments
 (0)