speedskater / babel-plugin-rewire

A babel plugin adding the ability to rewire module dependencies. This enables to mock modules for testing purposes.
841 stars 90 forks source link

rc-2 keeps browserify from finding required files through other packages #117

Closed TheSavior closed 8 years ago

TheSavior commented 8 years ago

We use a package proxyquireify in order to stub out module dependencies as well as babel-plugin-rewire.

These have worked just fine together in past versions of this module, however now the transformation to the calls seems to keep browserify from detecting those modules bring required and including it in the bundle.

I'll try to create a failing test, but I just wanted to get this issue put up in the mean time.

TheSavior commented 8 years ago

Test case is here: https://github.com/TheSavior/babel-plugin-rewire-test-case

The problem is that browserify tries to figure out which modules are required when it creates the bundle. Proxyquireify helps by detecting uses of proxyquireify and adding in requires so that they are included. By babel-plugin-rewire rewriting the calls to proxyquireify, it keeps the module from working correctly.

The relevant calls to proxyquireify in the example repo look like this:

/* proxyquireify injected requires to make browserify include dependencies in the bundle */ /* istanbul ignore next */; (function __makeBrowserifyIncludeModule__() { require('./other_file');});var proxyquire = require('proxyquireify')(require);

var testModule = proxyquire('./other_file', {
  './imported_file': 'Success'
});

console.log('Success');

With babel-plugin-rewire, it looks like this:

var proxyquire = require('proxyquireify')(require);

var testModule = _get__('proxyquire')('./other_file', {
  './imported_file': 'Success'
});

console.log('Success');

While this seems specific to proxyquireify, it is up to babel-plugin-rewire to not directly break other packages. Perhaps this could be solved with a blacklist of things that it doesn't allow to be rewritten?

TheSavior commented 8 years ago

Flipping the order such that proxyquireify runs before babelify will fail if the code contains anything not parseable by acron, such as JSX.