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:
@@ -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 {
|
||||
|
||||
@@ -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"],
|
||||
});
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
const includes = require("lodash/includes");
|
||||
const readdir = require("fs-readdir-recursive");
|
||||
const helper = require("@babel/helper-fixtures");
|
||||
const rimraf = require("rimraf");
|
||||
const { sync: makeDirSync } = require("make-dir");
|
||||
const child = require("child_process");
|
||||
const merge = require("lodash/merge");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
import includes from "lodash/includes";
|
||||
import readdir from "fs-readdir-recursive";
|
||||
import * as helper from "@babel/helper-fixtures";
|
||||
import rimraf from "rimraf";
|
||||
import { sync as makeDirSync } from "make-dir";
|
||||
import child from "child_process";
|
||||
import merge from "lodash/merge";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { fileURLToPath } from "url";
|
||||
import { createRequire } from "module";
|
||||
|
||||
const fixtureLoc = path.join(__dirname, "fixtures");
|
||||
const tmpLoc = path.join(__dirname, "tmp");
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const fixtureLoc = path.join(dirname, "fixtures");
|
||||
const tmpLoc = path.join(dirname, "tmp");
|
||||
|
||||
const fileFilter = function (x) {
|
||||
return x !== ".DS_Store";
|
||||
@@ -91,7 +96,7 @@ const assertTest = function (stdout, stderr, opts) {
|
||||
};
|
||||
|
||||
const buildTest = function (binName, testName, opts) {
|
||||
const binLoc = path.join(__dirname, "../lib", binName);
|
||||
const binLoc = path.join(dirname, "../lib", binName);
|
||||
|
||||
return function (callback) {
|
||||
saveInFiles(opts.inFiles);
|
||||
|
||||
Reference in New Issue
Block a user