rewire icon indicating copy to clipboard operation
rewire copied to clipboard

browserify 2 support

Open jhnns opened this issue 12 years ago • 66 comments

If anyone wants to have browserify 2 support, let me know.

jhnns avatar Feb 23 '13 15:02 jhnns

Everything rewire needs is bundler-independent, so it basically "just" has to be adapted to the new browserify api. But since I'm not using browserify anymore I'm currently not implementing it.

jhnns avatar Feb 23 '13 15:02 jhnns

you prefer webpack, i guess .. (i see you developed an adapter) why? i'm using browserify, i don't know webpack iyho, whats worth for me? learning fast webpack or write an adapter for rewire?

aleclofabbro avatar Oct 01 '13 10:10 aleclofabbro

Did you use the browserify plugin? Did it work with browserify 2 as well? Or do you still use browserify 1?

Personally I prefer webpack, because it is more flexible. It comes with many loaders (like CoffeeScript, LESS, style, mocha) and supports different styles (commonjs, amd, components). But since browserify 2 a lot has changed, so browserify might be as good as webpack.

webpack's documentation is still in progress, but the maintainer @sokra is responding fast so this was not an issue for me.

jhnns avatar Oct 01 '13 12:10 jhnns

I was mentioned... (Just to prove that I'll respond fast :smile:)

sokra avatar Oct 01 '13 12:10 sokra

:+1:

tphummel avatar Feb 10 '14 22:02 tphummel

:+1:

davidguttman avatar Mar 05 '14 02:03 davidguttman

:+1:

jbaiter avatar Mar 25 '14 10:03 jbaiter

:+1:

focusaurus avatar Apr 02 '14 19:04 focusaurus

Because I really needed something like Rewire for Browserify I went ahead and did the port, so far it's been working out nicely: https://github.com/i-like-robots/rewireify

i-like-robots avatar Apr 15 '14 22:04 i-like-robots

:+1:

jprichardson avatar Apr 16 '14 06:04 jprichardson

@i-like-robots Cool! Since I'm not using browserify anymore I'm not too motivated to write a plugin :wink:.

Your implementation is simple and straight-forward. It doesn't work exactly like rewire, but that's ok for client-side modules. It will be a problem for isomorphic modules that run in node.js and in the browser. That's why I decided to stick straight to the node-rewire API for my webpack plugin.

If you're planing to rewrite rewireify to be compatible to the node-rewire API here's some advice:

Rewire circumvents the require.cache so other modules don't require the rewired version. Additionally every rewire()-call creates a new module instance. Furthermore a call of rewire("./moduleA.js") needs to add moduleA as dependency to the bundle. I don't know if browserify exposes the parser api to hack in these kind of things. Concerning globals I think you should also prepend the getImportGlobalsSrc()-content. If you add rewire as dependency of rewireify you can just require all the rewire utility modules like __get__, __set__, getImportGlobalsSrc and detectStrictMode.

Back in the browserify 1 days I hacked a browserify plugin for rewire. May that's some inspiration (although there were some crazy hacks necessary because browserify didn't provide an useful api).

jhnns avatar Apr 16 '14 09:04 jhnns

@jhnns Thanks for more information, that's really helpful. I put this together extremely quickly as a proof of concept but I plan on working on it to make it more robust. I studied your implementation thoroughly and it was interesting working out how to lever it into Browserify. Unfortunately Browserify does not readily expose a require function to override but I think I can definitely improve on simply injecting the functions into each and every module!

i-like-robots avatar Apr 16 '14 10:04 i-like-robots

Well, you could just take my browserify 1 middleware and adjust it to the new transform api of browserify > 2. All the magic/crazy client-side stuff should still work. You just need to find the browserify-2-equivalents of bundle.register and bundle.require.

jhnns avatar Apr 16 '14 11:04 jhnns

That covers a lot of the things I've been thinking about, glad to see some of this stuff has already been solved! Thanks for digging that out for me.

i-like-robots avatar Apr 16 '14 11:04 i-like-robots

Could you make any progress yet?

jhnns avatar Apr 22 '14 23:04 jhnns

:+1:

jsdf avatar Jun 28 '14 03:06 jsdf

:thumbsup:

wookiehangover avatar Jun 29 '14 20:06 wookiehangover

:+1:

benatkin avatar Jul 09 '14 22:07 benatkin

:+1:

iamrandys avatar Aug 13 '14 12:08 iamrandys

Thanks @i-like-robots!! Your rewireify works perfectly! I simply added it to my karma.conf and presto! Thanks again!

    browserify: {
        debug: true,
        transform: [ 'reactify', 'rewireify' ]
    },

https://github.com/i-like-robots/rewireify

iamrandys avatar Aug 13 '14 12:08 iamrandys

Yep, it's cool. Just wanted to point out that the implementation differs a bit (see above).

jhnns avatar Aug 14 '14 09:08 jhnns

:+1:

Ah as you stated, I see that rewire is clearing the require cache each time and returning a fresh instance. This is very important when developing unit tests. Rewireify is really close, but my unit tests are failing using because they are all using the same "required" instance and my mock call counts are not as expected when running multiple tests together. If I run the tests one at a time they run fine.

Rewire is exactly what browserify needs, but I don't see any way to clear the require cache in browserify.

Difference to require()

Every call of rewire() executes the module again and returns a fresh instance.

rewire("./myModule.js") === rewire("./myModule.js"); // = false


Furthermore a call of rewire("./moduleA.js") needs to add moduleA as dependency to the bundle.

I can easily add moduleA as a dependency in Browserify by calling bundler.require('/moduleA.js', {expose: './moduleA.js').

iamrandys avatar Sep 07 '14 04:09 iamrandys

:+1:

traviskaufman avatar Sep 08 '14 01:09 traviskaufman

There's no require.cache in browserify? They somehow must have reverted my pull-request with browserify 2...

jhnns avatar Sep 08 '14 15:09 jhnns

i want browserify support

blairanderson avatar Sep 24 '14 18:09 blairanderson

Browserify support would be great. Thanks!

danielyogel avatar Oct 08 '14 15:10 danielyogel

Pretty much interested by this feature to reuse my tests in karma as well :+1:

thom4parisot avatar Oct 09 '14 18:10 thom4parisot

👍

jonykrause avatar Oct 14 '14 15:10 jonykrause

how about requirejs?

cyrilthomas avatar Oct 28 '14 06:10 cyrilthomas

I don't know enough about requirejs but since there is no compilation step, I'd assume that it's impossible to inject the __set__- and __get__-method into the private module scope.

jhnns avatar Oct 28 '14 08:10 jhnns