@@ -7,7 +7,6 @@ var setterSrc = require("../__set__.js").toString(),
7
7
detectStrictMode = require ( "../detectStrictMode.js" ) ,
8
8
9
9
browserInit = fs . readFileSync ( __dirname + "/browserInit.js" , "utf8" ) ,
10
- importGlobalsSrc = getImportGlobalsSrc ( ) ,
11
10
injectionSrc = getInjectionSrc ( ) . replace ( / \s + / g, " " ) ; // strip out unnecessary spaces to be unobtrusive in the debug view
12
11
13
12
/**
@@ -19,18 +18,20 @@ var setterSrc = require("../__set__.js").toString(),
19
18
*@return {String }
20
19
*/
21
20
function getInjectionSrc ( ) {
22
- // Registers the setters and getters of every module according to their filename. The setters and getters must be
23
- // injected as string here to gain access to the private scope of the module.
24
- return 'require("rewire").register(__filename, ' + setterSrc + ', ' + getterSrc + ');' +
25
- // Overrides the module internal require with a require proxy. This proxy is necessary to call rewire with the
26
- // module's filename at the first parameter to resolve the path. This way rewire() works exactly like require().
27
- 'require = window.browserifyRequire.getProxy(require, __filename);' ;
21
+ return 'var rewire = require("rewire"); ' +
22
+ // Registers the setters and getters of every module according to their filename. The setters and getters must be
23
+ // injected as string here to gain access to the private scope of the module.
24
+ 'rewire.register(__filename, module, ' + setterSrc + ', ' + getterSrc + ');' +
25
+ // Overrides the module internal require with a require proxy. This proxy is necessary to call rewire with the
26
+ // module's filename at the first parameter to resolve the path. This way rewire() works exactly like require().
27
+ 'require = rewire.getProxy(require, __dirname);' +
28
+ // Cleaning up
29
+ 'rewire = undefined;' ;
28
30
}
29
31
30
32
function browserifyMiddleware ( b ) {
31
33
function injectRewire ( src , filename ) {
32
- var rewireRequires ,
33
- strictMode = "" ;
34
+ var rewireRequires ;
34
35
35
36
// Search for all rewire() statements an return the required path.
36
37
rewireRequires = getRewireRequires ( src ) ;
@@ -45,24 +46,39 @@ function browserifyMiddleware(b) {
45
46
b . require ( requirePath ) ;
46
47
} ) ;
47
48
48
- //If the module uses strict mode we must ensure that "use strict" stays at the beginning of the module .
49
- if ( detectStrictMode ( src ) === true ) {
50
- strictMode = ' "use strict"; ' ;
49
+ //Convert back slashes to normal slashes on windows .
50
+ if ( process . platform . indexOf ( "win" ) === 0 ) {
51
+ filename = filename . replace ( / \\ / g , "/" ) ;
51
52
}
52
53
53
- // Convert back slashes to normal slashes.
54
- filename = filename . replace ( / \\ / g, "/" ) ;
55
-
56
54
// We don't want to inject this code at the beginning of a rewire/lib-module. Otherwise
57
55
// it would cause a black hole that devours our universe.
58
56
if ( filename . indexOf ( "/rewire/lib" ) === - 1 ) {
59
57
src =
60
- strictMode + // either '' or ' "use strict"; '
61
- "/* this line was injected by rewire() */" +
62
- "var global = window; " + // window is our new global object
63
- importGlobalsSrc +
58
+ // Trying to hide the injected line in the debug view with extra whitespaces.
59
+ ' ' +
60
+ '/* this line was injected by rewire() */ ' + // Comment for the curious developer
61
+
62
+ // Now all global variables are declared with a var statement so they can be changed via __set__()
63
+ // without influencing global objects.
64
+ 'var global = window; ' + // window is our new global object
65
+ 'eval(require("rewire").getImportGlobalsSrc()); ' +
66
+
67
+ // The module src is wrapped inside a self-executing function.
68
+ // This is necessary to separate the module src from the preceding eval(importGlobalsSrc),
69
+ // because the module src can be in strict mode.
70
+ // In strict mode eval() can only declare vars the current scope. In this case our setters
71
+ // and getters won't work.
72
+ //@see https://developer.mozilla.org/en/JavaScript/Strict_mode#Making_eval_and_arguments_simpler
73
+ "(function () {" +
74
+
75
+ // If the module uses strict mode we must ensure that "use strict" stays at the beginning of the module.
76
+ ( detectStrictMode ( src ) ?' "use strict"; ' :' ' ) +
77
+
64
78
injectionSrc + "\n" +
65
- src ;
79
+ src +
80
+
81
+ "})();" ;
66
82
}
67
83
68
84
return src ;