@@ -7,7 +7,6 @@ var setterSrc = require("../__set__.js").toString(),
77 detectStrictMode = require ( "../detectStrictMode.js" ) ,
88
99 browserInit = fs . readFileSync ( __dirname + "/browserInit.js" , "utf8" ) ,
10- importGlobalsSrc = getImportGlobalsSrc ( ) ,
1110 injectionSrc = getInjectionSrc ( ) . replace ( / \s + / g, " " ) ; // strip out unnecessary spaces to be unobtrusive in the debug view
1211
1312/**
@@ -19,18 +18,20 @@ var setterSrc = require("../__set__.js").toString(),
1918 * @return {String }
2019 */
2120function 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;' ;
2830}
2931
3032function browserifyMiddleware ( b ) {
3133 function injectRewire ( src , filename ) {
32- var rewireRequires ,
33- strictMode = "" ;
34+ var rewireRequires ;
3435
3536 // Search for all rewire() statements an return the required path.
3637 rewireRequires = getRewireRequires ( src ) ;
@@ -45,24 +46,39 @@ function browserifyMiddleware(b) {
4546 b . require ( requirePath ) ;
4647 } ) ;
4748
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 , "/" ) ;
5152 }
5253
53- // Convert back slashes to normal slashes.
54- filename = filename . replace ( / \\ / g, "/" ) ;
55-
5654 // We don't want to inject this code at the beginning of a rewire/lib-module. Otherwise
5755 // it would cause a black hole that devours our universe.
5856 if ( filename . indexOf ( "/rewire/lib" ) === - 1 ) {
5957 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+
6478 injectionSrc + "\n" +
65- src ;
79+ src +
80+
81+ "})();" ;
6682 }
6783
6884 return src ;
0 commit comments