@@ -6,54 +6,87 @@ var setterSrc = require("../__set__.js").toString(),
6
6
getRewireRequires = require ( "./getRewireRequires.js" ) ,
7
7
detectStrictMode = require ( "../detectStrictMode.js" ) ,
8
8
9
- appendix = fs . readFileSync ( __dirname + "/appendix .js" , "utf8" ) ,
9
+ browserInit = fs . readFileSync ( __dirname + "/browserInit .js" , "utf8" ) ,
10
10
importGlobalsSrc = getImportGlobalsSrc ( ) ,
11
11
injectionSrc = getInjectionSrc ( ) . replace ( / \s + / g, " " ) ; // strip out unnecessary spaces to be unobtrusive in the debug view
12
12
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
+ */
13
21
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.
14
24
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().
16
27
'require = window.browserifyRequire.getProxy(require, __filename);' ;
17
28
}
18
29
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 ) {
21
36
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 ,
24
43
strictMode = "" ;
25
44
45
+ // Search for all rewire() statements an return the required path.
46
+ rewireRequires = getRewireRequires ( src ) ;
47
+
26
48
// Add all modules that are loaded by rewire() manually to browserify because browserify's
27
49
// require-sniffing doesn't work here.
28
50
rewireRequires . forEach ( function forEachRewireRequire ( requirePath ) {
29
-
51
+ // Resolve absolute paths
30
52
if ( requirePath . charAt ( 0 ) === "." ) {
31
53
requirePath = path . resolve ( path . dirname ( filename ) , requirePath ) ;
32
54
}
33
55
b . require ( requirePath ) ;
34
-
35
56
} ) ;
36
57
58
+ // If the module uses strict mode we must ensure that "use strict" stays at the beginning of the module.
37
59
if ( detectStrictMode ( src ) === true ) {
38
60
strictMode = ' "use strict"; ' ;
39
61
}
40
62
63
+ // Convert back slashes to normal slashes.
41
64
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.
42
68
if ( filename . indexOf ( "/rewire/lib" ) === - 1 ) {
43
69
src =
44
- strictMode +
45
- "var global = window; " +
70
+ strictMode + // either '' or ' "use strict"; '
71
+ "var global = window; " + // window is our new global object
46
72
importGlobalsSrc +
47
- injectionSrc +
73
+ injectionSrc + "\n" +
48
74
// 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" ;
52
81
}
53
82
54
83
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 ) ;
57
90
58
91
return b ;
59
92
}