-
Notifications
You must be signed in to change notification settings - Fork 10
Sharing code between rewireify and rewire-global #17
Comments
Adding @speedskater from babel-plugin-rewire and @jhnns from rewire |
@TheSavior A global override of the In defence of the current approach though; it is very basic. This means that's been easy to maintain and test and it hasn't required any compatibility changes as Browserify has been updated from version 3 through to 11. |
Yep, I totally agree with that. I think rewire is the base functionality that defines the methods and Apis used to actually rewire things. Rewireify and Rewire-Global do the same thing for different environments (rewireify browser, rewire-global node), but they could be injecting the same code via their own mechanisms. Babel-plugin-rewire is in kind of an interesting place. It is a rewrite of rewire's main get and set functions to support es6 type things such as async functions, const values, etc. and then uses babel's transform api to inject it into every module just like rewireify / rewire-global. It seems like we could find some way to pull the shared functionality together. |
In regards to rewireify, rewire-global, and babel-plugin-rewire having a different api than rewire since they all inject the code into each and every module instead of requiring the developer to call We use Proxyquire with rewire via rewireify on the client, and rewire-global in node so that we can have isomorphic/universal code that works the same way in tests. |
@TheSavior @i-like-robots @speedskater thank you guys for your effort. I'm amazed about your motivation for bringing rewire to browserify/babel. However, I'm a bit concerned that things get complicated. Especially if your rewire adoptions have a slightly different API. I would like that users could just drop-in rewireify or the babel-plugin-rewire without needing to know about the differences behind it. So these are my thoughts about streamlining the modules:
Could you guys give me your opinion about it? Or is the current situation ok? What are your thoughts about API changes to rewire that might impact you? Especially jhnns/rewire#73 |
@jhnns I absolutely, wholeheartedly agree that our APIs should be identical. At our company we have three folders of js code, browser, common, and node. We have two test suites, karma which runs browser and common, and mocha which runs common and node tests. We have to make sure that our test suites work exactly the same way so that our developers are fluid in any of those folders (and that common will just work on both test suites). In our karma test suite we use rewireify to globally inject rewire into all modules via browserify. In our mocha test suite we use rewire-global to globally inject rewire into all modules via node's module require wrapper. We are looking to switch over to babel, and so we would want to just swap out those layers for babel-plugin-rewireify. So I very much agree that the APIs for all of these need to be the same. :) I spoke with @speedskater about using the modules inside the lib folder for babel-plugin-rewire and I believe these were some of the concerns he had.
It seems like the first problem could be solved in Rewire. We add support for these features and that shouldn't cause problems for people who aren't using ES6. The second problem is a bit more of a problem. One approach I've been toying with in my head is for us to move the babel transforms into rewire, write the core functions as transforms, and then on release run babel to compile the transforms into regular es5 strings like we have now. Then rewireify and rewire-global could continue using strings as we have them, and babel-plugin-rewire would be happy and we would all have the exact same API. In regards to sharing other code more, I agree that we should definitely be using the other modules in the lib folder as well. I didn't actually know they existed. I'll be updating to that. The other line of code that we could really gain something by sharing is this one: rewireify:
rewire-global: (I haven't updated to Object.isExtensible yet)
babel-plugin-rewire:
That one line is very hard to keep in sync across all three projects but there is no reason that I can think of for why it should be any different. We should try to figure out how we can pull that up into rewire as well. Which leads me to my proposal for what these different packages do:
|
@TheSavior @i-like-robots @jhnns Generally I like the idea of swapping out rewire like implementations depending on the environment. Therefore similar behavior is definitively desired. Regarding actually sharing the code I think i becomes a bit more complex. The reason is that in the case of babel-plugin-rewire we have to generate the rewirering code (set, get....) through AST transformations. Therefore in case of babel-plugin-rewire the functionality of setting and getting a variable value is quite different from the one used by rewire (which basically uses eval functions @jhnns or did i get that wrong)) or rewirefy (which I think uses rewire's functionality). Therefore I think only some parts of the code can be shared. e.g. storing of the original rewired values, reset functionality or similar. Also lines like the one mentioned by @TheSavior in the previous comment might be ideal candidates for sharing. Therefore I think for actually unifying and sharing parts of our code we should:
@TheSavior @i-like-robots @jhnns @shebson What do you think about this proposal. By the way is someone of you attending jsconf.eu? |
@speedskater It sounds like as you mention, the biggest problem is needing to have the rewire code It seems like the other things you mention are pretty consistent with my proposal as well. Are there other big problems you foresee? I am not attending jsconf.eu. I'm based in the San Francisco area so I try to go to most of the conferences out here instead. :) |
@jhnns Any more thoughts on how we can bring these together and possibly even write the rewire core functions using the tree transformers so that it would work with every project with the exact same api? |
I maintain rewire-global, which is very similar to rewireify except for node. Rewireify adds rewire to every module via a browserify transform. rewire-global adds rewire to every module by modifying node's internal require wrapper.
If you look at rewire-global's main file, you'll notice that things are pretty much identical to that of rewireify:
https://github.com/TheSavior/rewire-global/blob/master/index.js
I noticed this commit in rewireify that verifies that an object is extensible. Rewire-global would benefit from that.
I noticed this issue about rewireify not currently injecting the functions when module.exports is a function. Rewire-global currently does that.
It seems like it would be in both of our best interests to figure out how to pull out the necessary wrapping code into a common module and then support it being used via both browserify and node. Thoughts?
The text was updated successfully, but these errors were encountered: