Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd432d90

Browse files
author
Johannes
committed
added comments
1 parentb4c182b commitd432d90

File tree

5 files changed

+52
-41
lines changed

5 files changed

+52
-41
lines changed
File renamed without changes.

‎lib/browserify/browserifyMiddleware.js

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
1321
functiongetInjectionSrc(){
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-
functionbrowserifyMiddleware(b){
20-
varstrictMode;
30+
functionwrapCodeInDecorativeComments(filename,src){
31+
vartopLine="",
32+
bottomLine="",
33+
lineLength=80;
34+
35+
while(topLine.length<=lineLength){
2136

22-
b.register(".js",functioninjectRewire(src,filename){
23-
varrewireRequires=getRewireRequires(src),
37+
}
38+
}
39+
40+
functionbrowserifyMiddleware(b){
41+
functioninjectRewire(src,filename){
42+
varrewireRequires,
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(functionforEachRewireRequire(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
returnsrc;
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
returnb;
5992
}

‎test/testModules/moduleA.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ function checkSomeGlobals() {
3131
if(typeofglobal==="undefined"){
3232
thrownewReferenceError("global is undefined");
3333
}
34-
if(typeofprocess==="undefined"){
35-
thrownewReferenceError("process is undefined");
36-
}
3734
if(typeofconsole==="undefined"){
3835
thrownewReferenceError("console is undefined");
3936
}
@@ -52,10 +49,6 @@ function getConsole() {
5249
returnconsole;
5350
}
5451

55-
functiongetProcess(){
56-
returnprocess;
57-
}
58-
5952
functiongetFilename(){
6053
return__filename;
6154
}
@@ -68,6 +61,5 @@ exports.getMyObj = getMyObj;
6861
exports.readFileSync=readFileSync;
6962
exports.checkSomeGlobals=checkSomeGlobals;
7063
exports.getConsole=getConsole;
71-
exports.getProcess=getProcess;
7264
exports.getFilename=getFilename;
7365
exports.someOtherModule=someOtherModule;

‎test/testModules/moduleB.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ function checkSomeGlobals() {
3131
if(typeofglobal==="undefined"){
3232
thrownewReferenceError("global is undefined");
3333
}
34-
if(typeofprocess==="undefined"){
35-
thrownewReferenceError("process is undefined");
36-
}
3734
if(typeofconsole==="undefined"){
3835
thrownewReferenceError("console is undefined");
3936
}
@@ -52,10 +49,6 @@ function getConsole() {
5249
returnconsole;
5350
}
5451

55-
functiongetProcess(){
56-
returnprocess;
57-
}
58-
5952
functiongetFilename(){
6053
return__filename;
6154
}
@@ -68,6 +61,5 @@ exports.getMyObj = getMyObj;
6861
exports.readFileSync=readFileSync;
6962
exports.checkSomeGlobals=checkSomeGlobals;
7063
exports.getConsole=getConsole;
71-
exports.getProcess=getProcess;
7264
exports.getFilename=getFilename;
7365
exports.someOtherModule=someOtherModule;

‎test/testModules/sharedTestCases.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function cleanRequireCache() {
3737
}
3838
}
3939

40-
describe("rewire "+(typeofwindow==="undefined"?"innode.js":"in thebrowser"),function(){
40+
describe("rewire "+(typeofwindow==="undefined"?"(node.js)":"(browser)"),function(){
4141
beforeEach(cleanRequireCache);// ensuring a clean test environment
4242
it("should work like require()",function(){
4343
expect(rewire("./moduleA.js")).to.be(require("./moduleA.js"));
@@ -109,25 +109,19 @@ describe("rewire " + (typeof window === "undefined"? "in node.js": "in the brows
109109
varrewiredModuleA=rewire("./moduleA.js"),
110110
rewiredModuleB=rewire("./moduleB.js"),
111111
consoleMock={},
112-
processMock={},
113112
newFilename="myFile.js";
114113

115114
rewiredModuleA.__set__({
116-
console:consoleMock,
117-
process:processMock
115+
console:consoleMock
118116
});
119117
rewiredModuleA.__set__("__filename",newFilename);
120118
rewiredModuleB.__set__({
121-
console:consoleMock,
122-
process:processMock
119+
console:consoleMock
123120
});
124121
rewiredModuleB.__set__("__filename",newFilename);
125122
expect(rewiredModuleA.getConsole()).to.be(consoleMock);
126123
expect(rewiredModuleB.getConsole()).to.be(consoleMock);
127124
expect(console).not.to.be(consoleMock);
128-
expect(rewiredModuleA.getProcess()).to.be(processMock);
129-
expect(rewiredModuleB.getProcess()).to.be(processMock);
130-
expect(process).not.to.be(processMock);
131125
expect(rewiredModuleA.getFilename()).to.be(newFilename);
132126
expect(rewiredModuleB.getFilename()).to.be(newFilename);
133127
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp