@@ -6,54 +6,87 @@ var setterSrc = require("../__set__.js").toString(),
66 getRewireRequires = require ( "./getRewireRequires.js" ) ,
77 detectStrictMode = require ( "../detectStrictMode.js" ) ,
88
9- appendix = fs . readFileSync ( __dirname + "/appendix .js" , "utf8" ) ,
9+ browserInit = fs . readFileSync ( __dirname + "/browserInit .js" , "utf8" ) ,
1010 importGlobalsSrc = getImportGlobalsSrc ( ) ,
1111 injectionSrc = getInjectionSrc ( ) . replace ( / \s + / g, " " ) ; // strip out unnecessary spaces to be unobtrusive in the debug view
1212
13+ /**
14+ * Returns a string that gets injected at the beginning of every module. Its purpose is to
15+ *
16+ * - register the setters and getters according to the module's filename
17+ * - override the internal require with a require proxy.
18+ *
19+ * @return {String }
20+ */
1321function 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.
1424 return 'require("rewire").register(__filename, ' + setterSrc + ', ' + getterSrc + ');' +
15- 'process = require("__browserify_process");' +
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().
1627 'require = window.browserifyRequire.getProxy(require, __filename);' ;
1728}
1829
19- function browserifyMiddleware ( b ) {
20- var strictMode ;
30+ function wrapCodeInDecorativeComments ( filename , src ) {
31+ var topLine = "" ,
32+ bottomLine = "" ,
33+ lineLength = 80 ;
34+
35+ while ( topLine . length <= lineLength ) {
2136
22- b . register ( ".js" , function injectRewire ( src , filename ) {
23- var rewireRequires = getRewireRequires ( src ) ,
37+ }
38+ }
39+
40+ function browserifyMiddleware ( b ) {
41+ function injectRewire ( src , filename ) {
42+ var rewireRequires ,
2443 strictMode = "" ;
2544
45+ // Search for all rewire() statements an return the required path.
46+ rewireRequires = getRewireRequires ( src ) ;
47+
2648 // Add all modules that are loaded by rewire() manually to browserify because browserify's
2749 // require-sniffing doesn't work here.
2850 rewireRequires . forEach ( function forEachRewireRequire ( requirePath ) {
29-
51+ // Resolve absolute paths
3052 if ( requirePath . charAt ( 0 ) === "." ) {
3153 requirePath = path . resolve ( path . dirname ( filename ) , requirePath ) ;
3254 }
3355 b . require ( requirePath ) ;
34-
3556 } ) ;
3657
58+ // If the module uses strict mode we must ensure that "use strict" stays at the beginning of the module.
3759 if ( detectStrictMode ( src ) === true ) {
3860 strictMode = ' "use strict"; ' ;
3961 }
4062
63+ // Convert back slashes to normal slashes.
4164 filename = filename . replace ( / \\ / g, "/" ) ;
65+
66+ // We don't want to inject this code at the beginning of a rewire/lib-module. Otherwise
67+ // it would cause a black hole that devours our universe.
4268 if ( filename . indexOf ( "/rewire/lib" ) === - 1 ) {
4369 src =
44- strictMode +
45- "var global = window; " +
70+ strictMode + // either '' or ' "use strict"; '
71+ "var global = window; " + // window is our new global object
4672 importGlobalsSrc +
47- injectionSrc +
73+ injectionSrc + "\n" +
4874 // For a better debugging experience we're adding a comment with the filename
49- "\n//// " + filename + " /////////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n" +
50- src +
51- "\n\n////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n" ;
75+ "//// " + filename + " /////////////////////////////////////////////////////////////////////////////////////////////////////////////\n" +
76+ "\n" +
77+ src + "\n" +
78+ "\n" +
79+ "/////" + filename . replace ( / ./ g, "/" ) + "//////////////////////////////////////////////////////////////////////////////////////////////////////////////\n" +
80+ "//@ sourceURL=" + filename + "\n" ;
5281 }
5382
5483 return src ;
55- } ) ;
56- b . append ( appendix ) ;
84+ }
85+
86+ // Register file handler
87+ b . register ( ".js" , injectRewire ) ;
88+ // Append rewire initialization at the end of the bundle
89+ b . append ( browserInit ) ;
5790
5891 return b ;
5992}
0 commit comments