@@ -6,8 +6,10 @@ var setterSrc = require("../../__set__.js").toString(),
6
6
injectRewire = require ( "../injectRewire.js" ) ,
7
7
getRewireRegExp = require ( "../getRewireRegExp.js" ) ,
8
8
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" ) ,
11
13
settersAndGettersSrc ;
12
14
13
15
/**
@@ -20,16 +22,11 @@ var setterSrc = require("../../__set__.js").toString(),
20
22
* @param {!String } src
21
23
* @return {String } src
22
24
*/
23
- function webpackLoader ( src ) {
25
+ function webpackPostLoader ( src ) {
24
26
var filename = this . request . split ( "!" ) . pop ( ) ,
25
27
rewireRegExp = getRewireRegExp ( ) ;
26
28
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 ) ) {
33
30
// replaces rewire("some/path") into rewire("some/path", require("some/path"))
34
31
src = src . replace ( rewireRegExp , '$1rewire("$2", require("$2"))' ) ;
35
32
@@ -40,8 +37,27 @@ function webpackLoader(src) {
40
37
return src ;
41
38
}
42
39
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
+ }
45
61
46
62
/**
47
63
* This string gets injected at the beginning of every module. Its purpose is to
@@ -61,4 +77,4 @@ settersAndGettersSrc = (
61
77
'rewire = undefined;'
62
78
) . replace ( / \s + / g, " " ) ; // strip out unnecessary spaces to be unobtrusive in the debug view
63
79
64
- module . exports = webpackLoader ;
80
+ module . exports = webpackPostLoader ;
0 commit comments