Skip to content

Commit

Permalink
Fix remaining imports to be ESM-compatible (#14674)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Jun 20, 2022
1 parent e482c76 commit d016c15
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 26 deletions.
26 changes: 26 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("core-js-compat/data.json");
1 change: 1 addition & 0 deletions packages/babel-preset-env/data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }
4 changes: 2 additions & 2 deletions packages/babel-preset-env/src/filter-items.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d016c15

Please sign in to comment.