-
Notifications
You must be signed in to change notification settings - Fork 20
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
rewire-webpack doesn't work with babel-loader #12
Comments
Just wanted to chime in here, experiencing the same issue. ES6 was working just fine with |
Also experiencing the same issue, with the same setup. Does anyone have any workarounds outside of downgrading babel? Seems like not using import statements works, and other ES6 syntax is fine. |
Mhmm ... I don't know much about |
From my debugging, the problem comes from this line: In older versions of Babel, the value of In Babel 5, the value of 'undefined' is not an object (evaluating '__webpack_require__.m[module].call') Best guess is that somewhere |
So, does it work if you deactivate the babel compiler for all modules within |
Babel doesn't touch |
See jhnns/rewire#55 as well. |
Welp, what I'm experiencing is exactly same as what people saying. (I'm using karma+mocha+webpack+rewire+sinon) I just switched most of my codebase to ES6 module syntax and a few tests using |
I found where the problems comes from. Babel and rewire-webpack both actually causing the issue... There is two problems (caused by Babel > 5 's new module name transform rule). First one is importing First one: import rewire from 'rewire'
describe('test-1', function () {
var fooModule = rewire('../foo-module'); gets transformed into var _rewire = __webpack_require__(456);
var _rewire2 = _interopRequireWildcard(_rewire);
describe('test-1', function () {
var fooModule = _rewire2['default']('../foo-module'); You see the problem. Second one, Rewire-webpack assumes modules to be declared within module scope as same varname (if When rewiring, Well, this is pretty much what I've found out so far, but not sure who's responsible to fix this. ;) I will also present this issue to Babel GH issues to see what is their response. |
Thanks for tracking that down @sairion! Yeah, I just started noticing how Babel 5 is rewriting var form for quite a few things. |
Awesome, @sairion 👍. I'm looking forward to any progress. Will see if I can help out too 😄. Just a question, would plain CommonJS |
Wow, that's tricky. Thx for investigating @sairion, that's really a big help! |
rewire and rewire-webpack are both affected. We could probably do something about the first problem (e.g. parsing the module when the rewire-loader is applied), but the second one is imho impossible to solve on my side. |
If it helps, I've also set up a small repository demoing the conflict. Just run webpack and load the resulting bundle in a script tag to see the issue. |
👍 What the latest on this? Encountering the same thing with webpack/karma/webpack-rewire |
@bradbarrow, this is the suggested alternative: https://github.com/speedskater/babel-plugin-rewire |
The suggested alternative is not working for me. See speedskater/babel-plugin-rewire#28. Did any of you encounter this before? |
+1 |
In order for rewire calls to be correctly handled like require() calls by Webpack, rewire currently needs to be imported using CommonJS style syntax. See jhnns/rewire-webpack#12 for a discussion and explanation.
Any news on this so far? I'm having the same issue with babel-plugin-rewire.
Attention: but this only works when index.js imports the module in curly brackets like:
Removing {} causes the rewire to fail. Here my deps:
|
I'm sorry, I don't know. ES2015 module have some new challenges, like immutable live-bindings, and since rewire is tightly coupled to the module system, it's not easy to find a good solution for this. The situation gets even more complicated since there is no JS engine yet that implements native ES2015 modules. So, a possible solution would be a workaround for the babel transpiler only. But I would like to find a solution, that will also work with native ES2015 modules. |
I'm experiencing the same issue using TypeScript as a loader in webpack:
It does not work with either ES6 or require syntax (using Any progress? I'm unsure how else I can mock my dependencies. I'm using webpack+karma+mocha+typescript (ts-loader), all the latest versions available in npm as of today. |
CommonJS syntax works
Currently I have the following karma config:
Running the following karma test:
The test pulls in the following dependency:
Which pulls in another dependency to be mocked:
Which all works like a charm.
ES6 syntax does not
The problem is our source code is all transpiled using ES6 and ES6 modules. So the following does not work and causes an error:
The error returned is:
Even if the test remains in commonJS syntax the test runs but the rewiring fails. Are there any work arounds here at all until the point at which rewire will work with ES6?
The text was updated successfully, but these errors were encountered: