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

Commitff40e6a

Browse files
author
Johannes
committed
all tests running
1 parent0c86073 commitff40e6a

17 files changed

+583
-197
lines changed

‎lib/browserify/appendix.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* This code gets injected at the end of the browserify bundle via b.append().
3+
*/
4+
5+
if(typeofwindow.browserifyRequire!=="undefined"){
6+
thrownewError("Naming collision detected: window.browserifyRequire seems to be occupied.");
7+
}
8+
9+
// Saves the browserifyRequire under a new name. Necessary to call the original browserifyRequire within
10+
// a module where the variable name "require" is overridden by the module's internal require.
11+
window.browserifyRequire=require;
12+
13+
/**
14+
* Provides a special require-proxy. Every module calls window.browserifyRequire.getProxy(require, __filename) at the
15+
* beginning and overrides its own require with this proxy.
16+
*
17+
* This is necessary to call rewire() with the original __filename. Thus you can use rewire() like require().
18+
*
19+
*@param {!Function} internalRequire the module's own require
20+
*@param {String} filename the __filename of the module
21+
*@return {Function} requireProxy
22+
*/
23+
window.browserifyRequire.getProxy=function(internalRequire,filename){
24+
varrewireModule=internalRequire("rewire"),
25+
key;
26+
27+
functionrewireProxy(path,cache){
28+
returnrewireModule(filename,path,cache);
29+
}
30+
31+
for(keyinrewireModule){
32+
if(rewireModule.hasOwnProperty(key)){
33+
rewireProxy[key]=rewireModule[key];
34+
}
35+
}
36+
37+
returnfunctionrequireProxy(path){
38+
if(path==="rewire"){
39+
returnrewireProxy;
40+
}else{
41+
returninternalRequire(path);
42+
}
43+
};
44+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
varsetterSrc=require("../__set__.js").toString(),
2+
getterSrc=require("../__get__.js").toString(),
3+
fs=require("fs"),
4+
path=require("path"),
5+
getImportGlobalsSrc=require("../getImportGlobalsSrc.js"),
6+
getRewireRequires=require("./getRewireRequires.js"),
7+
detectStrictMode=require("../detectStrictMode.js"),
8+
9+
appendix=fs.readFileSync(__dirname+"/appendix.js","utf8"),
10+
importGlobalsSrc=getImportGlobalsSrc(),
11+
injectionSrc=getInjectionSrc().replace(/\s+/g," ");// strip out unnecessary spaces to be unobtrusive in the debug view
12+
13+
functiongetInjectionSrc(){
14+
return'require("rewire").register(__filename, '+setterSrc+', '+getterSrc+');'+
15+
'process = require("__browserify_process");'+
16+
'require = window.browserifyRequire.getProxy(require, __filename);';
17+
}
18+
19+
functionbrowserifyMiddleware(b){
20+
varstrictMode;
21+
22+
b.register(".js",functioninjectRewire(src,filename){
23+
varrewireRequires=getRewireRequires(src),
24+
strictMode="";
25+
26+
// Add all modules that are loaded by rewire() manually to browserify because browserify's
27+
// require-sniffing doesn't work here.
28+
rewireRequires.forEach(functionforEachRewireRequire(requirePath){
29+
30+
if(requirePath.charAt(0)==="."){
31+
requirePath=path.resolve(path.dirname(filename),requirePath);
32+
}
33+
b.require(requirePath);
34+
35+
});
36+
37+
if(detectStrictMode(src)===true){
38+
strictMode=' "use strict"; ';
39+
}
40+
41+
filename=filename.replace(/\\/g,"/");
42+
if(filename.indexOf("/rewire/lib")===-1){
43+
src=
44+
strictMode+
45+
"var global = window; "+
46+
importGlobalsSrc+
47+
injectionSrc+
48+
// For a better debugging experience we're adding a comment with the filename
49+
"\n//// "+filename+" /////////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n"+
50+
src+
51+
"\n\n////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n";
52+
}
53+
54+
returnsrc;
55+
});
56+
b.append(appendix);
57+
58+
returnb;
59+
}
60+
61+
module.exports=browserifyMiddleware;

‎lib/browserify/browserifyRewire.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
varpathUtil=require("path"),
2+
browserifyRequire=window.browserifyRequire;
3+
4+
varregistry={},
5+
rewiredModules=[];// cache for all rewired modules so it can be reset anytime
6+
7+
functionrewire(parentModulePath,targetPath,cache){
8+
varoriginalModule,
9+
rewiredModule={},
10+
registeredTargetModule;
11+
12+
if(cache===undefined){
13+
cache=true;
14+
}
15+
16+
// Make absolute paths
17+
if(targetPath.charAt(0)==="."){
18+
targetPath=pathUtil.resolve(pathUtil.dirname(parentModulePath),targetPath);
19+
}
20+
21+
// Normalize path with file extensions
22+
targetPath=require.resolve(targetPath);
23+
24+
// Deleting module from cache to trigger execution again
25+
deletebrowserifyRequire.modules[targetPath]._cached;
26+
27+
// Require module to trigger rewire.register() if it hasnt been required yet
28+
// Putting (require) within brackets is a hack to disable browserifys require sniffing
29+
//@see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
30+
originalModule=(require)(targetPath);
31+
32+
for(varkeyinoriginalModule){
33+
if(originalModule.hasOwnProperty(key)){
34+
rewiredModule[key]=originalModule[key];
35+
}
36+
}
37+
38+
if(cache){
39+
browserifyRequire.modules[targetPath]._cached=rewiredModule;
40+
}
41+
42+
// Get registry entry of the target module
43+
registeredTargetModule=registry[targetPath];
44+
45+
// Apply setter and getters
46+
rewiredModule.__set__=registeredTargetModule.setter;
47+
rewiredModule.__get__=registeredTargetModule.getter;
48+
49+
// Store rewired modules for rewire.reset()
50+
rewiredModules.push(targetPath);
51+
52+
returnrewiredModule;
53+
}
54+
55+
rewire.register=function(filename,setter,getter){
56+
registry[filename]={
57+
setter:setter,
58+
getter:getter
59+
};
60+
};
61+
62+
/**
63+
* Deletes all rewired modules from the cache
64+
*/
65+
rewire.reset=function(){
66+
varmodules=browserifyRequire.modules,
67+
i;
68+
69+
for(i=0;i<rewiredModules.length;i++){
70+
deletemodules[rewiredModules[i]]._cached;
71+
}
72+
73+
rewiredModules=[];
74+
};
75+
76+
module.exports=rewire;

‎lib/browserify/getRewireRequires.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
functiongetRewireRequires(src){
2+
varresult=[],
3+
regExp=/[^a-zA-Z0-9_]rewire\(["'](.+?)["']\)/g,
4+
match;
5+
6+
src=" "+src;// ensure that rewire() is not at index 0 otherwise the regexp won't work in this case
7+
match=regExp.exec(src);
8+
while(match!==null){
9+
result.push(match[1]);
10+
match=regExp.exec(src);
11+
}
12+
13+
returnresult;
14+
}
15+
16+
module.exports=getRewireRequires;

‎lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function rewire(request, cache) {
2222
if(process.title==="browser"){
2323
module.exports=require("./browserify/browserifyRewire.js");
2424
}else{
25-
// Putting (require) within brackets is a hack to disable browserify require sniffing
25+
// Putting (require) within brackets is a hack to disable browserify's require sniffing
2626
//@see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
2727
rewireModule=(require)("./rewire.js");
2828

‎lib/rewire.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function restoreOriginalWrappers() {
1717
/**
1818
* Does actual rewiring the module. For further documentation @see index.js
1919
*/
20-
functionrewire(parentModule,filename,cache){
20+
functionrewire(parentModulePath,targetPath,cache){
2121
vartestModule,
2222
nodeRequire,
2323
prepend,
@@ -37,21 +37,21 @@ function rewire(parentModule, filename, cache) {
3737
}
3838

3939
// Checking params
40-
if(typeoffilename!=="string"){
40+
if(typeoftargetPath!=="string"){
4141
thrownewTypeError("Filename must be a string");
4242
}
4343

4444
// Resolve full filename relative to the parent module
45-
filename=Module._resolveFilename(filename,parentModule);
45+
targetPath=Module._resolveFilename(targetPath,parentModulePath);
4646

4747
// Special support for older node versions that returned an array on Module._resolveFilename
4848
//@see https://github.com/joyent/node/blob/865b077819a9271a29f982faaef99dc635b57fbc/lib/module.js#L319
49-
if(Array.isArray(filename)){
50-
filename=filename[1];
49+
if(Array.isArray(targetPath)){
50+
targetPath=targetPath[1];
5151
}
5252

5353
// Create testModule as it would be created by require()
54-
testModule=newModule(filename,parentModule);
54+
testModule=newModule(targetPath,parentModulePath);
5555

5656
// Patching requireProxy
5757
nodeRequire=testModule.require;
@@ -66,7 +66,7 @@ function rewire(parentModule, filename, cache) {
6666

6767
// Check if the module uses the strict mode.
6868
// If so we must ensure that "use strict"; stays at the beginning of the module.
69-
src=fs.readFileSync(filename,"utf8");
69+
src=fs.readFileSync(targetPath,"utf8");
7070
if(detectStrictMode(src)===true){
7171
prepend=' "use strict"; '+prepend;
7272
}
@@ -82,8 +82,8 @@ function rewire(parentModule, filename, cache) {
8282

8383
// Store the rewired module in the cache when enabled
8484
if(cache){
85-
rewiredModules.push(filename);// save in private cache for .reset()
86-
require.cache[filename]=testModule;
85+
rewiredModules.push(targetPath);// save in private cache for .reset()
86+
require.cache[targetPath]=testModule;
8787
}
8888

8989
// This is only necessary if nothing has been required within the module

‎package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
},
3232
"devDependencies": {
3333
"mocha":"1.2.x",
34-
"expect.js":"0.1.x"
34+
"expect.js":"0.1.x",
35+
"browserify":"1.13.x"
3536
},
3637
"scripts" : {
3738
"test" :"mocha"

‎playground.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
varvm=require("vm");
2+
3+
varmine={};
4+
5+
mine.runInNewContext=vm.runInNewContext;
6+
7+
mine.runInNewContext("console.log('test');",{
8+
"console":console
9+
});

‎test/browser/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<head>
3+
<linkrel="stylesheet"href="../../node_modules/mocha/mocha.css"/>
4+
<scriptsrc="../../node_modules/mocha/mocha.js"type="text/javascript"></script>
5+
<scriptsrc="browseroutput.js"type="text/javascript"></script>
6+
</head>
7+
<body>
8+
<divid="mocha"></div>
9+
</body>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp