Skip to content

Commit

Permalink
Expose CJS proxy in @babel/core
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jun 3, 2021
1 parent 7dd8220 commit 870b982
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/babel-core/package.json
Expand Up @@ -4,6 +4,10 @@
"description": "Babel compiler core.",
"type": "module",
"main": "./lib/index.js",
"exports": {
"require": "./proxy.cjs",
"default": "./lib/index.js"
},
"author": "The Babel Team (https://babel.dev/team)",
"license": "MIT",
"publishConfig": {
Expand Down
25 changes: 25 additions & 0 deletions packages/babel-core/proxy.cjs
@@ -0,0 +1,25 @@
"use strict";

const babelP = import("./lib/index.js");

proxy("createConfigItem");
proxy("loadPartialConfig");
proxy("loadOptions");
proxy("transform");
proxy("transformFile");
proxy("transformFromAst");
proxy("parse");

function proxy(name) {
exports[`${name}Sync`] = () => {
throw new Error(
"Only async methods are supported when using `require()` to load `@babel/core`."
);
};
exports[`${name}Async`] = (...args) =>
babelP.then(babel => babel[`${name}Async`](...args));
exports[name] = (...args) => {
const cb = args.pop();
babelP.then(babel => babel[name](...args, cb), cb);
};
}

0 comments on commit 870b982

Please sign in to comment.