-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
extract require hook logic into an external module #70
Comments
@bcoe: Can I take this on? I'd like to give it a shot. |
@sebmck, @gotwarlost: If I/we wrote a library for properly messing with require, would you switch to using it in your projects? I'd be happy to submit PRs if needed. (@gotwarlost, feel free to read babel/babel#3062 for some more context). |
Also, (sorry for so many separate comments), what about naming it pirates, since your hijacking require, and it involves hooks? The name is available. We could also do requiarrr/requiargh, but that kind of makes me cringe. |
@ariporad I like boring old, I think the logic @jamestalmage put into the require-hook is quite sound, I would use this as a jumping off point -- it overrides the There's been some discussion with @davglass, and @gotwarlost about moving some of the |
@bcoe: (In order):
var pirates = require('pirates'); // This auto-calls `pirates/register`, see below.
// ...
pirates.addCompiler('.js', function (module, filename) {
return this.addFile(module, filename).content; // Just return the new source for the module.
}); Pirates will take care of the rest, including support for auto-fixing modules that directly modify require('pirates/register');
// For some weird reason, part of my app is in coffee script, and some is in ES6.
require('coffee-script/register');
require('babel-core/regsister');
require('some-bad-module/register'); |
@ariporad awesome, hadn't noticed istanbul had been moved into an org like this 👍 It might be worth combining efforts and extending: https://github.com/istanbuljs/istanbul-lib-hook the main problem being that babel kicks this hook out of the way. |
@bcoe: The trouble is that |
Actually |
Also look at the require hooking mechanism that has changed courtesy a PR from @blakeembrey It allows for custom module extension loaders and hooks into |
Thanks for the credit, but it wasn't me. @novemberborn maybe?
I will handle AVA integration. |
@gotwarlost: Wow, I certainly just skimmed that... Never mind. Although, it still doesn't support multiple hooks using it. (They would just override each other). I'll submit a PR that fixes that. (I'd still like to advocate for changing the name to pirates, but...) I'll take babel. |
@gotwarlost, @bcoe: Ok: I submitted istanbuljs-archived-repos/istanbul-lib-hook#2. It supports multiple modules using it, but has no support what so ever for other modules hijacking require without it. I can add that, but it feels totally out of scope for that project. |
Ok, (I lied about the stopping the flow of comments bit): I think the best course of action is to close istanbuljs-archived-repos/istanbul-lib-hook#2, and someone (unless a volunteer comes forward before I get to it, I'll start, but I'm open to anyone) should combine
That should allow keeping it relatively simple, and making it easy to adopt. Sound good to everyone? Any objections/things I should change? (Also, if everyone's ok with it, I'd like to take it on, as I've never contributed (significantly) to an open source project that anyone actually uses before, and I'd like the experience). |
@ariporad I think you should take a crack at this, definitely a problem that everyone re-invents the wheel to solve -- I'm sure folks would be happy to move towards a well tested solution that covers the various edge-cases folks bump into. Let me know anything I can do to help. |
Oh, I almost forgot: @gotwarlost, @bcoe: can I have permission to (with attribution) modify and redistribute (well, really just take ideas from) your code? Thanks! (I'm going to release this under the MIT license, FYI) |
@ariporad sounds cool! Let me know if you run into any trouble. |
Ok everyone, let's move this discussion to the repo for the project, danez/pirates#1. |
Ok, @bcoe, @gotwarlost, @novemberborn, @jamestalmage, @sebmck: I got pirates mostly done! It's now available on npm, and I've opened pull requests for babel (babel/babel#3139) and istanbul (istanbuljs-archived-repos/istanbul-lib-hook#5), and a PR for nyc is in the works. If you have any other modules you like that use require hooks, convince them to adopt pirates! It's super easy and reduces complexity! After looking into having getters and setters for dealing with naughty require hooks, I ended up dropping that idea (for now), because basically any hook that misbehaves is using Enjoy! |
I think there is some fundamental confusion here about why it was necessary for For starters, I wrote this gist that is a step by step walkthrough of how a stack of The This is problematic for This is essentially impossible to guarantee, but @novemberborn's solution is the most elegant solution I have seen so far. It essentially uses a I have successfully generalized it. Doing so was a bit more complex than I expected, but it reliably ensures that your transform is always able to be applied last. It ultimately relies on every extension forwarding down the chain ( See the test suite here. (Ignore the current module name, I don't like it). @bcoe - The tests have 100% coverage, btw. I'll have a coveralls badge once I decide what to name the freaking thing. |
Reading your walkthrough I'm thinking that may have been by accident! 😉
I guess nyc gets away with using |
Sort of. From my reading of the source code, I think things will break down if two or more transforms are installed on top of |
Actually, @jamestalmage, they're run in reverse order, so as long as nyc can register it's hook first, it will always be last. |
Nope. I have thoroughly verified that they do execute in order through multiple tests. I was certainly confused when I realized this. At first glance at the code, it does seem the order would be reversed. Read this gist to fully understand why/how they execute in order. |
Wow, yeah, you're right. My bad. Sorry. |
No problem. I started with the same assumption you did. |
Ok, @bcoe, @jamestalmage, @novemberborn: Sorry, I totally misunderstood what you wanted. I'll try to build this into pirates, it doesn't seem unreasonable to want to do, and I'll try to do it in a way that allows a chain. |
Hmm... Thinking about this: I'm probably going to implement this as an option (ie. |
I think it's a bad idea to build it into I want to be able to just point people at The Let's say, as a user, I actually want this ordering: require('foo/register');
require('bar/register');
require('baz/register'); But if It is reasonable for I think |
@jamestalmage: Fair enough. But at some point (soon) I'd like to fix NYC's logic. I'm not sure exactly the way to go, but I'm guessing it's to wrap |
after reading this thread, I feel like sharing this link: https://xkcd.com/927/ :) |
@gotwarlost - Love that one, and it goes to the point I am making. I don't really want wide adoption of |
@jamestalmage: Ok, that sounds great.. How ever I really do insist that it properly supports a require chain. I'm pretty sure the way to go about that is to wrap |
|
@jamestalmage: oh, thanks! You rock! |
@novemberborn has done a great job of generalizing the logic around extending require hooks, I think this logic would be a great candidate for pulling into its own module -- with a thorough test suite.
CC: @jamestalmage, @ariporad
The text was updated successfully, but these errors were encountered: