Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remaining imports to be ESM-compatible #14674

Merged
merged 5 commits into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -94,6 +94,23 @@ module.exports = {
],
rules: {
"no-restricted-globals": ["error", ...cjsGlobals],
"no-restricted-imports": [
"error",
{
patterns: ["**/*.json"],
paths: [
{
name: "semver",
message:
"semver's named exports are not recognized by the Node.js ESM-CJS interop.",
importNames: Object.keys(require("semver")).filter(
// We use it as a type import.
name => name !== "SemVer"
),
},
],
},
],
},
},
{
Expand Down Expand Up @@ -137,6 +154,15 @@ module.exports = {
],
},
},
{
files: ["packages/babel-preset-env/data/**/*.js"],
rules: {
"import/no-extraneous-dependencies": [
"error",
{ packageDir: "./packages/babel-preset-env" },
],
},
},
{
files: ["scripts/**/*.js"],
rules: {
Expand Down
6 changes: 4 additions & 2 deletions babel.config.js
Expand Up @@ -277,12 +277,14 @@ const monorepoPackages = ["codemods", "eslint", "packages"]
.reduce((a, b) => a.concat(b))
.map(name => name.replace(/^babel-/, "@babel/"));

function importInteropSrc(source) {
function importInteropSrc(source, filename) {
if (
// These internal files are "real CJS" (whose default export is
// on module.exports) and not compiled ESM.
source.startsWith("@babel/compat-data/") ||
source.includes("babel-eslint-shared-fixtures/utils")
source.includes("babel-eslint-shared-fixtures/utils") ||
(source.includes("../data/") &&
/babel-preset-env[\\/]src[\\/]/.test(filename))
) {
return "node";
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
@@ -1,10 +1,10 @@
import path from "path";
import fs from "fs";
import { createRequire } from "module";
import helpers from "@babel/helpers";
import * as helpers from "@babel/helpers";
import { transformFromAstSync, File } from "@babel/core";
import template from "@babel/template";
import t from "@babel/types";
import * as t from "@babel/types";
import { fileURLToPath } from "url";

import transformRuntime from "../lib/index.js";
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-env/data/core-js-compat.js
@@ -0,0 +1 @@
module.exports = require("core-js-compat/data.json");
1 change: 1 addition & 0 deletions packages/babel-preset-env/data/package.json
@@ -0,0 +1 @@
{ "type": "commonjs" }
4 changes: 2 additions & 2 deletions packages/babel-preset-env/src/filter-items.ts
@@ -1,4 +1,4 @@
import { lt } from "semver";
import semver from "semver";
import { minVersions } from "./available-plugins";

// $FlowIgnore
Expand All @@ -25,7 +25,7 @@ export function removeUnsupportedItems(
babelVersion: string,
) {
items.forEach(item => {
if (has(minVersions, item) && lt(babelVersion, minVersions[item])) {
if (has(minVersions, item) && semver.lt(babelVersion, minVersions[item])) {
items.delete(item);
}
});
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-preset-env/src/index.ts
@@ -1,5 +1,4 @@
import { lt } from "semver";
import type { SemVer } from "semver";
import semver, { type SemVer } from "semver";
import { logPlugin } from "./debug";
import getOptionSpecificExcludesFor from "./get-option-specific-excludes";
import {
Expand Down Expand Up @@ -308,7 +307,7 @@ export default declarePreset((api, opts: Options) => {
// @babel/core < 7.13.0 doesn't load targets (api.targets() always
// returns {} thanks to @babel/helper-plugin-utils), so we always want
// to fallback to the old targets behavior in this case.
lt(api.version, "7.13.0") ||
semver.lt(api.version, "7.13.0") ||
// If any browserslist-related option is specified, fallback to the old
// behavior of not using the targets specified in the top-level options.
opts.targets ||
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-preset-env/src/normalize-options.ts
@@ -1,7 +1,6 @@
import corejs3Polyfills from "core-js-compat/data.json";
import { coerce } from "semver";
import type { SemVer } from "semver";
import semver, { type SemVer } from "semver";
import corejs2Polyfills from "@babel/compat-data/corejs2-built-ins";
import corejs3Polyfills from "../data/core-js-compat";
import { plugins as pluginsList } from "./plugins-compat-data";
import moduleTransformations from "./module-transformations";
import { TopLevelOptions, ModulesOption, UseBuiltInsOption } from "./options";
Expand Down Expand Up @@ -187,7 +186,7 @@ export function normalizeCoreJSOption(
rawVersion = corejs;
}

const version = rawVersion ? coerce(String(rawVersion)) : false;
const version = rawVersion ? semver.coerce(String(rawVersion)) : false;

if (!useBuiltIns && version) {
console.warn(
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/scripts/generators/asserts.js
@@ -1,4 +1,4 @@
import t from "@babel/types";
import * as t from "@babel/types";

export default function generateAsserts() {
let output = `/*
Expand Down
7 changes: 5 additions & 2 deletions packages/babel-traverse/scripts/generators/validators.js
@@ -1,5 +1,5 @@
import t from "@babel/types";
import virtualTypes from "../../lib/path/lib/virtual-types.js";
import * as t from "@babel/types";
import * as virtualTypes from "../../lib/path/lib/virtual-types.js";

export default function generateValidators() {
let output = `/*
Expand All @@ -18,6 +18,9 @@ export interface NodePathValidators {
}

for (const type of Object.keys(virtualTypes)) {
// TODO: Remove this check once we stop compiling to CJS
if (type === "default" || type === "__esModule") continue;

const { types } = virtualTypes[type];
if (type[0] === "_") continue;
if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-traverse/scripts/generators/virtual-types.js
@@ -1,4 +1,4 @@
import virtualTypes from "../../lib/path/lib/virtual-types.js";
import * as virtualTypes from "../../lib/path/lib/virtual-types.js";

export default function generateValidators() {
let output = `/*
Expand All @@ -11,6 +11,9 @@ export interface VirtualTypeAliases {
`;

for (const type of Object.keys(virtualTypes)) {
// TODO: Remove this check once we stop compiling to CJS
if (type === "default" || type === "__esModule") continue;

output += ` ${type}: ${(virtualTypes[type].types || ["Node"])
.map(t => `t.${t}`)
.join(" | ")};`;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/scripts/generators/asserts.js
@@ -1,4 +1,4 @@
import definitions from "../../lib/definitions/index.js";
import * as definitions from "../../lib/definitions/index.js";

function addAssertHelper(type) {
const result =
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/scripts/generators/ast-types.js
@@ -1,4 +1,4 @@
import t from "../../lib/index.js";
import * as t from "../../lib/index.js";
import stringifyValidator from "../utils/stringifyValidator.js";

export default function generateAstTypes() {
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-types/scripts/generators/builders.js
@@ -1,5 +1,5 @@
import t from "../../lib/index.js";
import definitions from "../../lib/definitions/index.js";
import * as t from "../../lib/index.js";
import * as definitions from "../../lib/definitions/index.js";
import formatBuilderName from "../utils/formatBuilderName.js";
import lowerFirst from "../utils/lowerFirst.js";
import stringifyValidator from "../utils/stringifyValidator.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/scripts/generators/constants.js
@@ -1,4 +1,4 @@
import definitions from "../../lib/definitions/index.js";
import * as definitions from "../../lib/definitions/index.js";

export default function generateConstants() {
let output = `/*
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/scripts/generators/docs.js
Expand Up @@ -2,7 +2,7 @@ import util from "util";
import stringifyValidator from "../utils/stringifyValidator.js";
import toFunctionName from "../utils/toFunctionName.js";

import t from "../../lib/index.js";
import * as t from "../../lib/index.js";

const readme = [
`---
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/scripts/generators/flow.js
@@ -1,4 +1,4 @@
import t from "../../lib/index.js";
import * as t from "../../lib/index.js";
import stringifyValidator from "../utils/stringifyValidator.js";
import toFunctionName from "../utils/toFunctionName.js";

Expand Down
@@ -1,4 +1,4 @@
import t from "../../lib/index.js";
import * as t from "../../lib/index.js";
import stringifyValidator from "../utils/stringifyValidator.js";
import toFunctionName from "../utils/toFunctionName.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/scripts/generators/validators.js
@@ -1,4 +1,4 @@
import definitions from "../../lib/definitions/index.js";
import * as definitions from "../../lib/definitions/index.js";

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

Expand Down