jhnns / rewire-webpack

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

Not working with Webpack 2 beta #22

Open sudsy opened 8 years ago

sudsy commented 8 years ago

My tests that were working with Webpack 1 and rewire now fail under webpack 2 beta.

I receive the error:

C:\Users\sudsy\Development\gen3\PVL.Content\tests\node_modules\webpack\node_modules\enhanced-resolve\lib\Resolver.js:86
        if(callback.stack) {
                   ^

TypeError: Cannot read property 'stack' of undefined
    at Tapable.doResolve (C:\Users\sudsy\Development\gen3\PVL.Content\tests\node_modules\webpack\node_modules\enhanced-resolve\lib\Resolver.js:86:13)
    at Tapable.next (C:\Users\sudsy\Development\gen3\PVL.Content\tests\node_modules\enhanced-resolve\lib\ModuleAliasPlugin.js:37:19)
    at Tapable.<anonymous> (C:\Users\sudsy\Development\gen3\PVL.Content\tests\node_modules\enhanced-resolve\lib\ModuleAliasPlugin.js:42:5)
gkovacs commented 7 years ago

With webpack 2.2.0, the error is now:

geza@gezamac ~/D/habitlab> karma start
webpack: Using compiler.parser is deprecated.
Use compiler.plugin("compilation", function(compilation, data) {
  data.normalModuleFactory.plugin("parser", function(parser, options) { parser.plugin(/* ... */); });
}); instead. It was called at RewirePlugin.apply (/Users/geza/Dropbox/habitlab/node_modules/rewire-webpack/lib/RewirePlugin.js:22:21).
22 01 2017 01:01:42.072:ERROR [preprocess]: Can not load "webpack"!
  TypeError: Cannot read property 'apply' of null
    at RewirePlugin.apply (/Users/geza/Dropbox/habitlab/node_modules/rewire-webpack/lib/RewirePlugin.js:48:30)
    at Compiler.apply (/Users/geza/Dropbox/habitlab/node_modules/tapable/lib/Tapable.js:306:16)
    at webpack (/Users/geza/Dropbox/habitlab/node_modules/webpack/lib/webpack.js:32:19)
    at new Plugin (/Users/geza/Dropbox/habitlab/node_modules/karma-webpack/lib/karma-webpack.js:65:18)
    at invoke (/Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:75:15)
    at Array.instantiate (/Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:59:20)
    at get (/Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:48:43)
    at /Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:71:14
    at Array.map (native)
    at Array.invoke (/Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:70:31)
    at Injector.get (/Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:48:43)
    at instantiatePreprocessor (/Users/geza/Dropbox/habitlab/node_modules/karma/lib/preprocessor.js:55:20)
    at Array.forEach (native)
    at createPreprocessor (/Users/geza/Dropbox/habitlab/node_modules/karma/lib/preprocessor.js:74:20)
    at Array.invoke (/Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:75:15)
    at get (/Users/geza/Dropbox/habitlab/node_modules/di/lib/injector.js:48:43)
22 01 2017 01:01:42.148:WARN [karma]: No captured browser, open http://localhost:9876/
22 01 2017 01:01:42.160:INFO [karma]: Karma v1.4.0 server started at http://0.0.0.0:9876/
22 01 2017 01:01:42.161:INFO [launcher]: Launching browser Chrome_without_security with unlimited concurrency
22 01 2017 01:01:42.163:ERROR [karma]: Found 1 load error
gkovacs commented 7 years ago

I took a stab at fixing this issue of webpack 2 compatibility. If you apply my changes at https://github.com/gkovacs/rewire-webpack/commit/31da0a94242a3d86a81d09b38d74677f562ca88b it resolves both the issues above, however the .__get__ property is not available on the module, so apparently rewire.loader.js is not being correctly loaded. If someone can figure out how to fix that last issue then rewire-webpack will be fully compatible with webpack 2.

gkovacs commented 7 years ago

A hackish workaround for now until rewire-webpack is fixed to work with webpack 2 is to just insert the __get__ and __set__ manually into the module you're testing. ie (livescript/coffeescript)

export __get__ = (name) ->
  return eval(name)

export __set__ = (name, val) ->
  eval(name + ' = val')

I believe in javascript it would be

module.exports.__get__ = function(name) {
  return eval(name);
}

module.exports.__set__ = function(name, val) {
  eval(name + ' = val');
}

And clear the module cache before each require, via

delete require.cache[require.resolve('libs_frontend/dom_utils')]

To see a full example see https://github.com/habitlab/habitlab/blob/539443c123d335099cafdde2df35b40264a0acad/test/libs_frontend/dom_utils.test.ls and https://github.com/habitlab/habitlab/blob/539443c123d335099cafdde2df35b40264a0acad/src/libs_frontend/dom_utils.ls

rensbaardman commented 5 years ago

For webpack 4 support, you can use my fork rensbaardman/rewire-webpack-plugin.