Skip to content

Commit

Permalink
Cleanup USE_ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 12, 2023
1 parent cb5060d commit ce498a6
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 114 deletions.
50 changes: 21 additions & 29 deletions packages/babel-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
if (!process.env.IS_PUBLISH) {
if (!USE_ESM) {
if (process.env.BABEL_8_BREAKING) {
throw new Error(
"BABEL_8_BREAKING is only supported in ESM. Please run `make use-esm`.",
);
}
}
if (!process.env.IS_PUBLISH && !USE_ESM && process.env.BABEL_8_BREAKING) {
throw new Error(
"BABEL_8_BREAKING is only supported in ESM. Please run `make use-esm`.",
);
}

export const version = PACKAGE_JSON.version;
Expand Down Expand Up @@ -83,29 +79,25 @@ export const DEFAULT_EXTENSIONS = Object.freeze([

import Module from "module";
import * as thisFile from "./index.ts";
if (USE_ESM) {
if (!IS_STANDALONE) {
// Pass this module to the CJS proxy, so that it can be synchronously accessed.
const cjsProxy = Module.createRequire(import.meta.url)("../cjs-proxy.cjs");
cjsProxy["__ initialize @babel/core cjs proxy __"] = thisFile;
}
if (USE_ESM && !IS_STANDALONE) {
// Pass this module to the CJS proxy, so that it can be synchronously accessed.
const cjsProxy = Module.createRequire(import.meta.url)("../cjs-proxy.cjs");
cjsProxy["__ initialize @babel/core cjs proxy __"] = thisFile;
}

if (!process.env.BABEL_8_BREAKING) {
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
// For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
if (!USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.OptionManager = class OptionManager {
init(opts: {}) {
return loadOptionsSync(opts);
}
};
// eslint-disable-next-line no-restricted-globals
exports.OptionManager = class OptionManager {
init(opts: {}) {
return loadOptionsSync(opts);
}
};

// eslint-disable-next-line no-restricted-globals
exports.Plugin = function Plugin(alias: string) {
throw new Error(
`The (${alias}) Babel 5 plugin is being run with an unsupported Babel version.`,
);
};
}
// eslint-disable-next-line no-restricted-globals
exports.Plugin = function Plugin(alias: string) {
throw new Error(
`The (${alias}) Babel 5 plugin is being run with an unsupported Babel version.`,
);
};
}
24 changes: 10 additions & 14 deletions packages/babel-helper-environment-visitor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import type { NodePath, Visitor } from "@babel/traverse";
import type * as t from "@babel/types";

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
if (!IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
exports.skipAllButComputedKey = function skipAllButComputedKey(
path: NodePath<t.Method | t.ClassProperty>,
) {
path.skip();
if (path.node.computed) {
// requeue the computed key
path.context.maybeQueue(path.get("key"));
}
};
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
exports.skipAllButComputedKey = function skipAllButComputedKey(
path: NodePath<t.Method | t.ClassProperty>,
) {
path.skip();
if (path.node.computed) {
// requeue the computed key
path.context.maybeQueue(path.get("key"));
}
}
};
}

export function requeueComputedKeyAndDecorators(
Expand Down
24 changes: 10 additions & 14 deletions packages/babel-helper-module-transforms/src/dynamic-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@

import { types as t, template } from "@babel/core";

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
if (!IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
exports.getDynamicImportSource = function getDynamicImportSource(
node: t.CallExpression,
): t.StringLiteral | t.TemplateLiteral {
const [source] = node.arguments;
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
exports.getDynamicImportSource = function getDynamicImportSource(
node: t.CallExpression,
): t.StringLiteral | t.TemplateLiteral {
const [source] = node.arguments;

return t.isStringLiteral(source) || t.isTemplateLiteral(source)
? source
: (template.expression.ast`\`\${${source}}\`` as t.TemplateLiteral);
};
}
}
return t.isStringLiteral(source) || t.isTemplateLiteral(source)
? source
: (template.expression.ast`\`\${${source}}\`` as t.TemplateLiteral);
};
}

export function buildDynamicImport(
Expand Down
14 changes: 5 additions & 9 deletions packages/babel-helper-module-transforms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,11 @@ const {

export { buildDynamicImport } from "./dynamic-import.ts";

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
if (!IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
exports.getDynamicImportSource =
// eslint-disable-next-line no-restricted-globals, import/extensions
require("./dynamic-import").getDynamicImportSource;
}
}
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
exports.getDynamicImportSource =
// eslint-disable-next-line no-restricted-globals, import/extensions
require("./dynamic-import").getDynamicImportSource;
}

export { default as getModuleName } from "./get-module-name.ts";
Expand Down
18 changes: 7 additions & 11 deletions packages/babel-helper-replace-supers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ const {
thisExpression,
} = t;

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
if (!IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
const ns = require("@babel/helper-environment-visitor");
// eslint-disable-next-line no-restricted-globals
exports.environmentVisitor = ns.default;
// eslint-disable-next-line no-restricted-globals
exports.skipAllButComputedKey = ns.skipAllButComputedKey;
}
}
if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {
// eslint-disable-next-line no-restricted-globals
const ns = require("@babel/helper-environment-visitor");
// eslint-disable-next-line no-restricted-globals
exports.environmentVisitor = ns.default;
// eslint-disable-next-line no-restricted-globals
exports.skipAllButComputedKey = ns.skipAllButComputedKey;
}

type ThisRef =
Expand Down
8 changes: 3 additions & 5 deletions packages/babel-highlight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,9 @@ function getChalk(forceColor: boolean) {
}
return chalk;
}
if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.getChalk = (options: Options) => getChalk(options.forceColor);
}
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.getChalk = (options: Options) => getChalk(options.forceColor);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-preset-env/src/available-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@ if (!process.env.BABEL_8_BREAKING) {
// syntax enabled by default, we can safely skip enabling it.
if (!USE_ESM) {
// @ts-expect-error unknown key
availablePlugins["unicode-sets-regex"] =
USE_ESM || IS_STANDALONE
? e()
: () => require("@babel/plugin-syntax-unicode-sets-regex");
availablePlugins["unicode-sets-regex"] = IS_STANDALONE
? e()
: () => require("@babel/plugin-syntax-unicode-sets-regex");
}
}
22 changes: 10 additions & 12 deletions packages/babel-traverse/src/path/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,16 @@ export function ensureBlock(
return this.node;
}

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
/**
* Keeping this for backward-compatibility. You should use arrowFunctionToExpression() for >=7.x.
*/
// eslint-disable-next-line no-restricted-globals
exports.arrowFunctionToShadowed = function (this: NodePath) {
if (!this.isArrowFunctionExpression()) return;

this.arrowFunctionToExpression();
};
}
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
/**
* Keeping this for backward-compatibility. You should use arrowFunctionToExpression() for >=7.x.
*/
// eslint-disable-next-line no-restricted-globals
exports.arrowFunctionToShadowed = function (this: NodePath) {
if (!this.isArrowFunctionExpression()) return;

this.arrowFunctionToExpression();
};
}

/**
Expand Down
30 changes: 14 additions & 16 deletions packages/babel-traverse/src/path/lib/virtual-types-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,21 @@ export function isForAwaitStatement(this: NodePath): boolean {
return isForOfStatement(this.node, { await: true });
}

if (!process.env.BABEL_8_BREAKING) {
if (!USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.isExistentialTypeParam = function isExistentialTypeParam(
this: NodePath,
): void {
if (!process.env.BABEL_8_BREAKING && !USE_ESM) {
// eslint-disable-next-line no-restricted-globals
exports.isExistentialTypeParam = function isExistentialTypeParam(
this: NodePath,
): void {
throw new Error(
"`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.",
);
};

// eslint-disable-next-line no-restricted-globals
exports.isNumericLiteralTypeAnnotation =
function isNumericLiteralTypeAnnotation(this: NodePath): void {
throw new Error(
"`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.",
"`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.",
);
};

// eslint-disable-next-line no-restricted-globals
exports.isNumericLiteralTypeAnnotation =
function isNumericLiteralTypeAnnotation(this: NodePath): void {
throw new Error(
"`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.",
);
};
}
}

0 comments on commit ce498a6

Please sign in to comment.