browserify 2 support #13

Open
jhnns opened this Issue Feb 23, 2013 · 67 comments

Comments

Projects
None yet
Owner

jhnns commented Feb 23, 2013

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

jhnns was assigned Feb 23, 2013

Owner

jhnns commented Feb 23, 2013

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.

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?

Owner

jhnns commented Oct 1, 2013

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.

Contributor

sokra commented Oct 1, 2013

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

👍

👍

jbaiter commented Mar 25, 2014

👍

👍

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

👍

Owner

jhnns commented Apr 16, 2014

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

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 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!

Owner

jhnns commented Apr 16, 2014

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.

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.

Owner

jhnns commented Apr 22, 2014

Could you make any progress yet?

jsdf commented Jun 28, 2014

👍

benatkin commented Jul 9, 2014

👍

👍

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

Owner

jhnns commented Aug 14, 2014

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

👍

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').

👍

Owner

jhnns commented Sep 8, 2014

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

i want browserify support

Browserify support would be great. Thanks!

oncletom commented Oct 9, 2014

Pretty much interested by this feature to reuse my tests in karma as well 👍

👍

how about requirejs?

Owner

jhnns commented Oct 28, 2014

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.

👍

👍

metrofun commented Nov 2, 2014

👍

+1

knpwrs commented Feb 3, 2015

👍

Lucas-C commented Feb 4, 2015

+1

+1

mik01aj commented Feb 18, 2015

+1

EDIT: I found https://github.com/i-like-robots/rewireify, which does the job for me. Thanks @i-like-robots !

👍 Yes, please. A fully compatible browserify transform would be awesome.

Owner

jhnns commented Feb 22, 2015

@mik01aj Please be aware that rewireify doesn't mimic rewire exactly. Code that runs in node might break in the browser. However, if it's working for you, that's just fine 😁

Hi, I am working in some isomorphic code with rendrjs and stumble with this issue of using rewirefy with browserify 3 and karma. Any one was able to make this work on karma?

By the way, nice job with this module it's seems to work really nice on node.

+1

+1

nsonnad commented Apr 15, 2015

+1

👍

stuff commented May 4, 2015

👍 I think we need an official browserify support...

joswhite commented May 4, 2015

Edit: This would be cool, but I found that webpack actually does the job a bit cleaner. All you have to do is add the few lines to your config file. If you are using Node you'll also need to specify empty modules for fs, etc. This is my config file.

var RewirePlugin = require("rewire-webpack");
module.exports = {
    plugins: [
        new RewirePlugin()
    ],
    node: {
        child_process:"empty",
        fs: "empty"
    }
};

allenm commented May 6, 2015

+1

aroc commented May 26, 2015

👍

djforth commented May 26, 2015

  • 1

BenGedi commented Jun 10, 2015

👍

+1

+1

+1

tran2 commented Jul 17, 2015

+1

+1

+1

+1

No matter how many +1s, it'll never happen ;) I've seen the +1s for months now... I believe rewirify uses rewire now?

i-like-robots referenced this issue in i-like-robots/rewireify Sep 10, 2015

Open

Sharing code between rewireify and rewire-global #17

👍

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment