Avoid using CJS globals in internal source files (#12963)

* Lint against CJS globals in modules

* Use `import.meta.url` instead of `__filename` in `src` files

* Prepare fixtures runner for `import.meta.url`

* Use `import.meta.url` instead of `__filename` in `test/index` files

* Remove `__dirname` from remaining test files

dirname

* Avoid using `module` in `src` files

* Avoid using `require` in `src` files

* Avoid using `require` in `test` files

* Update `@types/node`

* Compile dynamic import in `@babel/node`

* Fix windows

* Use `@babel/plugin-proposal-dynamic-import` from npm
This commit is contained in:
Nicolò Ribaudo
2021-03-05 19:55:36 +01:00
committed by GitHub
parent ea620e822e
commit d04842a700
147 changed files with 651 additions and 346 deletions

View File

@@ -8,6 +8,10 @@ import vm from "vm";
import "core-js/stable";
import "regenerator-runtime/runtime";
import register from "@babel/register";
import { fileURLToPath } from "url";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const program = new commander.Command("babel-node");
@@ -194,7 +198,7 @@ if (program.eval || program.print) {
// add back on node and concat the sliced args
process.argv = ["node"].concat(args);
process.execArgv.push(__filename);
process.execArgv.push(fileURLToPath(import.meta.url));
Module.runMain();
} else {

View File

@@ -5,8 +5,12 @@
import getV8Flags from "v8flags";
import path from "path";
import child_process from "child_process";
import { fileURLToPath } from "url";
let args = [path.join(__dirname, "_babel-node")];
let args = [
path.join(path.dirname(fileURLToPath(import.meta.url)), "_babel-node"),
];
let babelArgs = process.argv.slice(2);
let userArgs;
@@ -41,7 +45,7 @@ const aliases = new Map([
["-gc", "--expose-gc"],
]);
getV8Flags(function (err, v8Flags) {
getV8Flags(async function (err, v8Flags) {
for (let i = 0; i < babelArgs.length; i++) {
const arg = babelArgs[i];
const flag = arg.split("=")[0];
@@ -69,17 +73,17 @@ getV8Flags(function (err, v8Flags) {
}
try {
const kexec = require("kexec");
const { default: kexec } = await import("kexec");
kexec(process.argv[0], args);
} catch (err) {
if (
err.code !== "ERR_MODULE_NOT_FOUND" &&
err.code !== "MODULE_NOT_FOUND" &&
err.code !== "UNDECLARED_DEPENDENCY"
) {
throw err;
}
const child_process = require("child_process");
const proc = child_process.spawn(process.argv[0], args, {
stdio: ["inherit", "inherit", "inherit", "ipc"],
});