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

[babel-node] Do not hardcode node flags #9914

Merged
merged 1 commit into from May 4, 2019
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
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