@@ -6,8 +6,10 @@ var setterSrc = require("../../__set__.js").toString(),
66 injectRewire = require ( "../injectRewire.js" ) ,
77 getRewireRegExp = require ( "../getRewireRegExp.js" ) ,
88
9- rewireLib = path . join ( "rewire" , "lib" ) ,
10- webpackBuildin = path . join ( "webpack" , "buildin" , "__webpack" ) ,
9+ rewireLib = path . resolve ( __dirname , "../../" ) ,
10+ projectBasePath = path . resolve ( __dirname , "../../../../../" ) ,
11+ nodeModulesPath = path . join ( projectBasePath , "node_modules" ) ,
12+ webpackPath = path . join ( "node_modules" , "webpack" ) ,
1113 settersAndGettersSrc ;
1214
1315/**
@@ -20,16 +22,11 @@ var setterSrc = require("../../__set__.js").toString(),
2022 * @param {!String } src
2123 * @return {String } src
2224 */
23- function webpackLoader ( src ) {
25+ function webpackPostLoader ( src ) {
2426 var filename = this . request . split ( "!" ) . pop ( ) ,
2527 rewireRegExp = getRewireRegExp ( ) ;
2628
27- // We don't want to inject this code at the beginning of a rewire/lib-module. Otherwise
28- // it would cause a black hole that devours our universe.
29- // We're also omitting webpack's buildin because it doesn't makes sense to rewire these modules. There's also
30- // a bug if the special code is injecting into these modules.
31- if ( filename . indexOf ( rewireLib ) === - 1 && filename . indexOf ( webpackBuildin ) === - 1 ) {
32-
29+ if ( isRewireableModule ( filename ) ) {
3330 // replaces rewire("some/path") into rewire("some/path", require("some/path"))
3431 src = src . replace ( rewireRegExp , '$1rewire("$2", require("$2"))' ) ;
3532
@@ -40,8 +37,27 @@ function webpackLoader(src) {
4037 return src ;
4138}
4239
43- webpackLoader . loader = __filename ;
44- webpackLoader . test = / \. j s $ / ;
40+ webpackPostLoader . loader = __filename ;
41+ webpackPostLoader . test = / \. j s $ / ;
42+
43+ /**
44+ * Returns true if the module is rewireable. Rewireable are all modules of the project.
45+ *
46+ * Example:
47+ * Imagine rewire lies under "~/myProject/node_modules/rewire". All files in "~/myProject" are rewireable except
48+ * the "~/myProject/node_modules"-path.
49+ *
50+ * @param {!String } path
51+ * @return {Boolean }
52+ */
53+ function isRewireableModule ( path ) {
54+ return path . indexOf ( projectBasePath ) !== - 1 &&
55+ path . indexOf ( nodeModulesPath ) === - 1 &&
56+
57+ // "rewire/lib" and "node_modules/webpack" are explicitly excluded to make the tests work
58+ path . indexOf ( rewireLib ) === - 1 &&
59+ path . indexOf ( webpackPath ) === - 1 ;
60+ }
4561
4662/**
4763 * This string gets injected at the beginning of every module. Its purpose is to
@@ -61,4 +77,4 @@ settersAndGettersSrc = (
6177 'rewire = undefined;'
6278) . replace ( / \s + / g, " " ) ; // strip out unnecessary spaces to be unobtrusive in the debug view
6379
64- module . exports = webpackLoader ;
80+ module . exports = webpackPostLoader ;
0 commit comments