Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support Node's --require and -r flags in babel-node #7471

Merged
merged 2 commits into from Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/babel-node/src/_babel-node.js
Expand Up @@ -23,6 +23,7 @@ function collect(value, previousValue): Array<string> {

/* eslint-disable max-len */
program.option("-e, --eval [script]", "Evaluate script");
program.option("-r, --require [module]", "Require module");
program.option("-p, --print [code]", "Evaluate script and print result");
program.option(
"-o, --only [globs]",
Expand Down Expand Up @@ -132,7 +133,11 @@ if (program.eval || program.print) {

if (arg[0] === "-") {
const parsedArg = program[arg.slice(2)];
if (parsedArg && parsedArg !== true) {
if (
arg === "-r" ||
arg === "--require" ||
(parsedArg && parsedArg !== true)
) {
ignoreNext = true;
}
} else {
Expand All @@ -142,6 +147,15 @@ if (program.eval || program.print) {
});
args = args.slice(i);

// We have to handle require ourselfs, as we want to require it in the context of babel-register
if (program.require) {
let requireFileName = program.require;
if (!path.isAbsolute(requireFileName)) {
requireFileName = path.join(process.cwd(), requireFileName);
}
require(requireFileName);
}

// make the filename absolute
const filename = args[0];
if (!path.isAbsolute(filename)) {
Expand Down
9 changes: 8 additions & 1 deletion packages/babel-node/src/babel-node.js
Expand Up @@ -34,7 +34,7 @@ function getNormalizedV8Flag(arg) {
}

getV8Flags(function(err, v8Flags) {
babelArgs.forEach(function(arg) {
babelArgs.forEach(function(arg, index) {
const flag = arg.split("=")[0];

switch (flag) {
Expand All @@ -50,6 +50,13 @@ getV8Flags(function(err, v8Flags) {
args.unshift(arg);
break;

case "-r":
case "--require":
args.push(flag);
args.push(babelArgs[index + 1]);
delete babelArgs[index + 1];
break;

case "-gc":
args.unshift("--expose-gc");
break;
Expand Down