Created
July 5, 2018 07:56
-
-
Save dead-claudia/412eaf4cfa2685bac4113223405529f0 to your computer and use it in GitHub Desktop.
Babel reload script concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This tiny wrapper file checks for known node flags and appends them | |
* when found, before invoking the "real" _babel-node(1) executable. | |
*/ | |
import getV8Flags from "v8flags"; | |
import path from "path"; | |
getV8Flags(function(err, v8Flags) { | |
if (err != null) throw err; | |
const babelPath = path.join(__dirname, "_babel-node"); | |
let args = []; | |
let execArgv = []; | |
let native = new Set([ | |
...v8Flags, | |
"debug", "--debug", "--debug-brk", | |
"--inspect", "--inspect-brk", | |
"--expose-gc", | |
]); | |
let aliases = new Map([ | |
["-d", "--debug"], | |
["-gc", "--expose-gc"], | |
["-r", "--require"], | |
]); | |
for (let i = 2; i < process.argv.length; i++) { | |
let arg = process.argv[i]; | |
if (arg === "--") { | |
while (i < process.argv.length) args.push(process.argv[i++]); | |
break; | |
} | |
const delimIndex = arg.indexOf("=") | |
const flag = delimIndex < 0 ? arg : arg.slice(0, delimIndex); | |
const resolved = aliases.get(flag) || flag; | |
if (resolved === "--require") { | |
execArgv.push(resolved); | |
if (i + 1 < process.argv.length) { | |
let value = process.argv[i + 1] | |
if (value !== "--") { | |
execArgv.push(value); | |
i++; | |
} | |
} | |
} else { | |
/** | |
* Replace dashes with underscores in the v8Flag name | |
* Also ensure that if the arg contains a value (e.g. --arg=true) | |
* that only the flag is returned. | |
*/ | |
if (resolved.includes("--")) { | |
resolved = `--${resolved.slice(2).replace(/-/g, "_")}`; | |
} | |
if (native.includes(resolved)) { | |
execArgv.push(resolved); | |
} else { | |
args.push(arg); | |
} | |
} | |
} | |
require("child_process").replaceFork(babelPath, args, {execArgv}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment