Skip to content

Commit

Permalink
[babel-node] Do not hardcode node flags (#9914)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 4, 2019
1 parent eae7a33 commit 5da94bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/babel-node/package.json
Expand Up @@ -23,6 +23,7 @@
"@babel/register": "^7.0.0",
"commander": "^2.8.1",
"lodash": "^4.17.11",
"node-environment-flags": "^1.0.5",
"v8flags": "^3.1.1"
},
"peerDependencies": {
Expand Down
69 changes: 27 additions & 42 deletions packages/babel-node/src/babel-node.js
Expand Up @@ -6,6 +6,10 @@
import getV8Flags from "v8flags";
import path from "path";

// TODO: When support for node < 10.10 will be dropped, this package
// can be replaced with process.allowedNodeEnvironmentFlags
import allowedNodeEnvironmentFlags from "node-environment-flags";

let args = [path.join(__dirname, "_babel-node")];

let babelArgs = process.argv.slice(2);
Expand All @@ -24,7 +28,9 @@ if (argSeparator > -1) {
* that only the flag is returned.
*/
function getNormalizedV8Flag(arg) {
const matches = arg.match(/--(.+)/);
// v8 uses the "no" prefix to negate boolean flags (e.g. --nolezy),
// but they are not listed by v8flags
const matches = arg.match(/--(?:no)?(.+)/);

if (matches) {
return `--${matches[1].replace(/-/g, "_")}`;
Expand All @@ -33,51 +39,30 @@ function getNormalizedV8Flag(arg) {
return arg;
}

// These are aliases for node options defined by babel-node.
const aliases = new Map([["-d", "--debug"], ["-gc", "--expose-gc"]]);

getV8Flags(function(err, v8Flags) {
babelArgs.forEach(function(arg, index) {
for (let i = 0; i < babelArgs.length; i++) {
const arg = babelArgs[i];
const flag = arg.split("=")[0];

switch (flag) {
case "-d":
args.unshift("--debug");
break;

case "debug":
case "--debug":
case "--debug-brk":
case "--inspect":
case "--inspect-brk":
case "--experimental-modules":
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;

case "--nolazy":
args.unshift(flag);
break;

default:
if (
v8Flags.indexOf(getNormalizedV8Flag(flag)) >= 0 ||
arg.indexOf("--trace") === 0
) {
args.unshift(arg);
} else {
args.push(arg);
}
break;
if (flag === "-r" || flag === "--require") {
args.push(flag);
args.push(babelArgs[++i]);
} else if (aliases.has(flag)) {
args.unshift(aliases.get(flag));
} else if (
flag === "debug" || // node debug foo.js
flag === "inspect" ||
v8Flags.indexOf(getNormalizedV8Flag(flag)) >= 0 ||
allowedNodeEnvironmentFlags.has(flag)
) {
args.unshift(arg);
} else {
args.push(arg);
}
});
}

// append arguments passed after --
if (argSeparator > -1) {
Expand Down

0 comments on commit 5da94bf

Please sign in to comment.