From 3ec3e840ba98e1b9cc670e2843795c0771a975cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 16 Dec 2019 22:04:41 +0100 Subject: [PATCH] Allow -r from node_modules with @babel/node --- packages/babel-node/package.json | 1 + packages/babel-node/src/_babel-node.js | 9 ++++----- packages/babel-node/src/babel-node.js | 1 + .../test/fixtures/babel-node/--require/in-files/dep.js | 1 + .../test/fixtures/babel-node/--require/in-files/foo.js | 1 + .../test/fixtures/babel-node/--require/options.json | 3 +++ .../test/fixtures/babel-node/--require/stdout.txt | 2 ++ .../babel-node/--require_node_modules/in-files/foo.js | 1 + .../--require_node_modules/in-files/node_modules/dep.js | 1 + .../babel-node/--require_node_modules/options.json | 3 +++ .../babel-node/--require_node_modules/stdout.txt | 2 ++ 11 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 packages/babel-node/test/fixtures/babel-node/--require/in-files/dep.js create mode 100644 packages/babel-node/test/fixtures/babel-node/--require/in-files/foo.js create mode 100644 packages/babel-node/test/fixtures/babel-node/--require/options.json create mode 100644 packages/babel-node/test/fixtures/babel-node/--require/stdout.txt create mode 100644 packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/foo.js create mode 100644 packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/node_modules/dep.js create mode 100644 packages/babel-node/test/fixtures/babel-node/--require_node_modules/options.json create mode 100644 packages/babel-node/test/fixtures/babel-node/--require_node_modules/stdout.txt diff --git a/packages/babel-node/package.json b/packages/babel-node/package.json index 9813bd1a5e5a..8559a260d3f7 100644 --- a/packages/babel-node/package.json +++ b/packages/babel-node/package.json @@ -25,6 +25,7 @@ "lodash": "^4.17.13", "node-environment-flags": "^1.0.5", "regenerator-runtime": "^0.13.3", + "resolve": "^1.13.1", "v8flags": "^3.1.1" }, "peerDependencies": { diff --git a/packages/babel-node/src/_babel-node.js b/packages/babel-node/src/_babel-node.js index 6f39bb638529..8e09639a5dd7 100644 --- a/packages/babel-node/src/_babel-node.js +++ b/packages/babel-node/src/_babel-node.js @@ -8,6 +8,7 @@ import vm from "vm"; import "core-js/stable"; import "regenerator-runtime/runtime"; import register from "@babel/register"; +import resolve from "resolve"; import pkg from "../package.json"; @@ -188,11 +189,9 @@ if (program.eval || program.print) { // We have to handle require ourselves, as we want to require it in the context of babel-register if (program.require) { - let requireFileName = program.require; - if (!path.isAbsolute(requireFileName)) { - requireFileName = path.join(process.cwd(), requireFileName); - } - require(requireFileName); + require(resolve.sync(program.require, { + basedir: process.cwd(), + })); } // make the filename absolute diff --git a/packages/babel-node/src/babel-node.js b/packages/babel-node/src/babel-node.js index 4f0a7be101a8..f660262e3455 100755 --- a/packages/babel-node/src/babel-node.js +++ b/packages/babel-node/src/babel-node.js @@ -86,6 +86,7 @@ getV8Flags(function(err, v8Flags) { const child_process = require("child_process"); const proc = child_process.spawn(process.argv[0], args, { stdio: "inherit", + cwd: process.cwd(), }); proc.on("exit", function(code, signal) { process.on("exit", function() { diff --git a/packages/babel-node/test/fixtures/babel-node/--require/in-files/dep.js b/packages/babel-node/test/fixtures/babel-node/--require/in-files/dep.js new file mode 100644 index 000000000000..16f81e8b7061 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require/in-files/dep.js @@ -0,0 +1 @@ +console.log("dep"); \ No newline at end of file diff --git a/packages/babel-node/test/fixtures/babel-node/--require/in-files/foo.js b/packages/babel-node/test/fixtures/babel-node/--require/in-files/foo.js new file mode 100644 index 000000000000..85ce559e8f22 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require/in-files/foo.js @@ -0,0 +1 @@ +console.log("foo"); diff --git a/packages/babel-node/test/fixtures/babel-node/--require/options.json b/packages/babel-node/test/fixtures/babel-node/--require/options.json new file mode 100644 index 000000000000..1384a0394841 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require/options.json @@ -0,0 +1,3 @@ +{ + "args": ["foo", "-r", "./dep"] +} diff --git a/packages/babel-node/test/fixtures/babel-node/--require/stdout.txt b/packages/babel-node/test/fixtures/babel-node/--require/stdout.txt new file mode 100644 index 000000000000..4718ef0fa726 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require/stdout.txt @@ -0,0 +1,2 @@ +dep +foo diff --git a/packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/foo.js b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/foo.js new file mode 100644 index 000000000000..85ce559e8f22 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/foo.js @@ -0,0 +1 @@ +console.log("foo"); diff --git a/packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/node_modules/dep.js b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/node_modules/dep.js new file mode 100644 index 000000000000..16f81e8b7061 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/in-files/node_modules/dep.js @@ -0,0 +1 @@ +console.log("dep"); \ No newline at end of file diff --git a/packages/babel-node/test/fixtures/babel-node/--require_node_modules/options.json b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/options.json new file mode 100644 index 000000000000..473c909f3949 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/options.json @@ -0,0 +1,3 @@ +{ + "args": ["foo", "-r", "dep"] +} diff --git a/packages/babel-node/test/fixtures/babel-node/--require_node_modules/stdout.txt b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/stdout.txt new file mode 100644 index 000000000000..4718ef0fa726 --- /dev/null +++ b/packages/babel-node/test/fixtures/babel-node/--require_node_modules/stdout.txt @@ -0,0 +1,2 @@ +dep +foo