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

added support Node's --require and -r flags in babel-node #7471

Merged
merged 2 commits into from Mar 27, 2018
Merged

added support Node's --require and -r flags in babel-node #7471

merged 2 commits into from Mar 27, 2018

Conversation

stuneak
Copy link

@stuneak stuneak commented Mar 2, 2018

Q                       A
Fixed Issues? #7465
Patch: Bug Fix? n
Major: Breaking Change? n
Minor: New Feature? y
Tests Added + Pass? n
Documentation PR
Any Dependency Changes? n
License MIT

@babel-bot
Copy link
Collaborator

babel-bot commented Mar 2, 2018

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/7238/

@stuneak stuneak changed the title added support Node's --require and -r flag added support Node's --require and -r flags in babel-node Mar 2, 2018
@nicolo-ribaudo nicolo-ribaudo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Mar 4, 2018
@@ -50,6 +50,11 @@ getV8Flags(function(err, v8Flags) {
args.unshift(arg);
break;

case "-r":
case "--require":
args.unshift(flag);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--require has an option according to the docu: https://nodejs.org/api/cli.html#cli_r_require_module
So using flag here is probably wrong and should be arg instead, but I haven't tested it, so correct me if I'm wrong.

Copy link
Member

@danez danez Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it seems I missed that the module is not separated by equal sign but with space in this case, so flag and arg are the same. Still I think this will not work as the preceding module name will be pushed to args (in the switch-default) whereas this here gets unshifted.

@mkovel
Copy link

mkovel commented Mar 14, 2018

Hi guys, can you push forward a little with this task? I am very looking forward it.
Thanks.

@danez
Copy link
Member

danez commented Mar 14, 2018

Okay I checked it and it wasn't working. I pushed a commit now it is working

@mkovel
Copy link

mkovel commented Mar 16, 2018

Guys, when do you plan, include this awesome feature in a release?

@hzoo hzoo merged commit 9a26c2b into babel:master Mar 27, 2018
@hzoo hzoo mentioned this pull request May 14, 2018
@jdalton
Copy link
Member

jdalton commented May 15, 2018

Hi all!

Thanks for adding this feature! At the moment though it looks like the -r option doesn't resolve modules like the traditional node -r by that I mean babel-node -r esm will result in an error

Cannot find module '/Users/jdalton/projects/bar/esm'

because it's not resolving esm to the node_modules folder of

'/Users/jdalton/projects/bar/node_modules/esm'

@danez
Copy link
Member

danez commented May 16, 2018

I see so we should change this line https://github.com/babel/babel/pull/7471/files#diff-bd092c1aa1f13cc3b620707dc4fc8a1eR153 to check if the path starts with dot and only then append cwd. In the other cases (absolute paths starting with / or modules starting with [a-zA-Z]) we can simply require them.

Not sure about Windows though.

@jdalton
Copy link
Member

jdalton commented May 16, 2018

@danez Node has an API, require.resolve(path, { paths:['.'] })], in Node 8+ that that uses Node's own path resolution to resolve path to the current working directly. This would handle resolving absolute, local, and package names. There may be a package on npm to backport the API for Node 6 (the actual bit of code internally is like 5 lines).

const Module = require('module')
const fakeParent = new Module('', null)

fakeParent.paths = Module._nodeModulePaths('.')
const paths = ['.', ...Module._resolveLookupPaths(request, fakeParent, true)]
const filename = Module._findPath(request, paths, false)

@loganfsmyth
Copy link
Member

loganfsmyth commented May 16, 2018

My vote would be to use resolve rather than rely on internals of Node, but otherwise sounds like a good plan.

@jdalton
Copy link
Member

jdalton commented May 16, 2018

The idea wouldn't be to use Node internals for all-the-things. It would be to fork and only use the internals for Node < 8. Though an external package is cool too.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: node outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants