Skip to content

Commit 0e570ec

Browse files
committed
make babel injectable in babel-register
1 parent ca18ea5 commit 0e570ec

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/babel-register/src/node.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import deepClone from "lodash/cloneDeep";
22
import sourceMapSupport from "source-map-support";
33
import * as registerCache from "./cache";
44
import escapeRegExp from "lodash/escapeRegExp";
5-
import * as babel from "@babel/core";
5+
import * as babelCore from "@babel/core";
66
import { OptionManager, DEFAULT_EXTENSIONS } from "@babel/core";
77
import { addHook } from "pirates";
88
import fs from "fs";
99
import path from "path";
1010

1111
const maps = {};
1212
const transformOpts = {};
13+
let babel = babelCore;
1314
let piratesRevert = null;
1415

1516
function installSourceMapSupport() {
@@ -100,6 +101,9 @@ register({
100101
export default function register(opts?: Object = {}) {
101102
// Clone to avoid mutating the arguments object with the 'delete's below.
102103
opts = Object.assign({}, opts);
104+
105+
if (opts.babel) babel = opts.babel;
106+
103107
if (opts.extensions) hookExtensions(opts.extensions);
104108

105109
if (opts.cache === false && cache) {
@@ -110,6 +114,7 @@ export default function register(opts?: Object = {}) {
110114
cache = registerCache.get();
111115
}
112116

117+
delete opts.babel;
113118
delete opts.extensions;
114119
delete opts.cache;
115120

packages/babel-register/test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,20 @@ describe("@babel/register", function() {
103103
chai.expect((gen_error = require(GEN_ERROR))).to.be.ok;
104104
chai.expect(gen_error()).to.match(/gen_error\.js:2:86/);
105105
});
106+
107+
it("uses custom babel when requested", () => {
108+
setupRegister({
109+
babel: {
110+
transform() {
111+
return { code: "module.exports = 'hello';" };
112+
},
113+
getEnv() {
114+
return {};
115+
},
116+
version: "custom",
117+
},
118+
});
119+
120+
chai.expect(require(DATA_ES2015)).to.equal("hello");
121+
});
106122
});

0 commit comments

Comments
 (0)