Repository: babel/babel
File: packages/babel-core/src/index.ts (TypeScript)

Code before:
  transformFile,
  transformFileAsync,
  transformFileSync,
} from "./transform-file.ts";
export {
  transformFromAst,
  transformFromAstAsync,
  transformFromAstSync,
} from "./transform-ast.ts";
export { parse, parseAsync, parseSync } from "./parse.ts";

/**
 * Recommended set of compilable extensions. Not used in @babel/core directly, but meant as
 * as an easy source for tooling making use of @babel/core.
 */
export const DEFAULT_EXTENSIONS = Object.freeze([
  ".js",
  ".jsx",
  ".es6",
  ".es",
  ".mjs",
  ".cjs",
] as const);

import Module from "module" with { USE_ESM: "true", IS_STANDALONE: "false" };
import * as thisFile from "./index.ts" with { USE_ESM: "true", IS_STANDALONE: "false" };
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 && !USE_ESM) {
  // For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
  // eslint-disable-next-line no-restricted-globals
  exports.OptionManager = class OptionManager {
    init(opts: any) {
      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.`,
    );
  };
}


Review comment (suggestion):
This is creative! It seems to me that there is an implicit `&&` logic between multiple boolean toggles, while we could support `||` such as `{ or: { conditions } }`, I am wondering if we can use/abuse the dynamic `import()` for such purpose, the demonstrated example is `&&` but we can easily replace it with `||` if it is required:

```js
const thisFile = await import("./index.ts", { if: USE_ESM && !IS_STANDALONE });
```

If the variable declaration is at top level, we can just replace it by static import after evaluating the `if` option bag. The pros are support of more complicate conditions and the cons are that tools may not support dynamic import quite well as the static one.

Code after:
  transformFile,
  transformFileAsync,
  transformFileSync,
} from "./transform-file.ts";
export {
  transformFromAst,
  transformFromAstAsync,
  transformFromAstSync,
} from "./transform-ast.ts";
export { parse, parseAsync, parseSync } from "./parse.ts";

/**
 * Recommended set of compilable extensions. Not used in @babel/core directly, but meant as
 * as an easy source for tooling making use of @babel/core.
 */
export const DEFAULT_EXTENSIONS = Object.freeze([
  ".js",
  ".jsx",
  ".es6",
  ".es",
  ".mjs",
  ".cjs",
] as const);

import Module from "module" with { if: "USE_ESM && !IS_STANDALONE" };
import * as thisFile from "./index.ts" with { if: "USE_ESM && !IS_STANDALONE" };
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 && !USE_ESM) {
  // For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
  // eslint-disable-next-line no-restricted-globals
  exports.OptionManager = class OptionManager {
    init(opts: any) {
      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.`,
    );
  };
}