jhnns / rewire-webpack

Dependency injection for webpack bundles
The Unlicense
121 stars 20 forks source link

enhanced-resolve doesn't always have /lib/ModuleAliasPlugin.js #25

Open robin-surge opened 7 years ago

robin-surge commented 7 years ago

I currently have a project with enhanced-resolve v3.4.1 and it doesn't have /lib/ModuleAliasPlugin.js; this causes RewirePlugin.js to fail in line 48.

See also: https://github.com/webpack/enhanced-resolve/tree/master/lib

Additionally, in webpack the resolvers are initialised with null in the Compiler constructor and you cannot use apply on null: https://github.com/webpack/webpack/blob/master/lib/Compiler.js#L184

gempain commented 7 years ago

Same problem here. Getting TypeError: Cannot read property 'apply' of null as a consequence of the mentioned problem.

robin-surge commented 7 years ago

For everyone having the same problem: You can use babel and babel-rewire-plugin. It sidesteps the issue (rewire is then handled by babel) and is a nice workaround to get unblocked.

gempain commented 7 years ago

Can you provide an example :)?

robin-surge commented 7 years ago

For what, exactly? Using babel-rewire or for the issue described?

gempain commented 7 years ago

Well, I'm not sure but I guess a karma / wepack config file example :)? It's just so I understand how you use Babel-rewire as a workaround :)

robin-surge commented 7 years ago

Its nothing special about what I did. I just used a different set of frameworks. Instead of going karma -> webpack -> webpack-rewire -> mocha/chai I now use karma -> webpack -> babel -> babel-plugin-rewire -> mocha/chai

You can check out the babel plugin here: https://github.com/speedskater/babel-plugin-rewire

gempain commented 7 years ago

Sweet, thanks for the help ! I'll be looking into it. The only thing I am worried about when using babel is that I've always used webpack, and I am testing a lib that is packed with it. But I guess for unit testing it doesn't really matter how you pack scripts. I am somehow worried that packing something differently in a testing environment and in a production environment could cause issues :/

robin-surge commented 7 years ago

My setup still uses webpack :)

rensbaardman commented 5 years ago

For a solution without babel, you can use my fork rensbaardman/rewire-webpack-plugin, which has webpack 4 support.