8000 - changed browserify version to 1.13.5 · ShMcK/rewire-coderoad@8bfdd97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bfdd97

Browse files
author
Johannes
committed
- changed browserify version to 1.13.5
- fixed global var injection in the browser
1 parent 7d7eca0 commit 8bfdd97

13 files changed

+441
-183
lines changed

lib/__set__.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ function __set__() {
2525
if (!arguments.env || Array.isArray(arguments.env)) {
2626
throw new TypeError("__set__ expects an object as env");
2727
}
28-
for (arguments.key in arguments.env) {
29-
if (arguments.env.hasOwnProperty(arguments.key)) {
30-
arguments.src += arguments.checkExistsSrc(arguments.key) + arguments.key + " = arguments.env." + arguments.key + ";";
28+
for (arguments.varName in arguments.env) {
29+
if (arguments.env.hasOwnProperty(arguments.varName)) {
30+
arguments.varValue = arguments.env[arguments.varName];
31+
arguments.src += arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.env." + arguments.varName + ";";
3132
}
3233
}
3334
} else if (typeof arguments.varName === "string") {

lib/browser/shims.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Taken from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf#Compatibility
2+
if (!Array.prototype.indexOf) {
3+
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
4+
"use strict";
5+
if (this == null) {
6+
throw new TypeError();
7+
}
8+
var t = Object(this);
9+
var len = t.length >>> 0;
10+
if (len === 0) {
11+
return -1;
12+
}
13+
var n = 0;
14+
if (arguments.length > 0) {
15+
n = Number(arguments[1]);
16+
if (n != n) { // shortcut for verifying if it's NaN
17+
n = 0;
18+
} else if (n != 0 && n != Infinity && n != -Infinity) {
19+
n = (n > 0 || -1) * Math.floor(Math.abs(n));
20+
}
21+
}
22+
if (n >= len) {
23+
return -1;
24+
}
25+
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
26+
for (; k < len; k++) {
27+
if (k in t && t[k] === searchElement) {
28+
return k;
29+
}
30+
}
31+
return -1;
32+
}
33+
}
34+
35+
36+
// Taken from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/trim#Compatibility
37+
if(!String.prototype.trim) {
38+
String.prototype.trim = function () {
39+
return this.replace(/^\s+|\s+$/g,'');
40+
};
41+
}
42+
43+
// Taken from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray#Compatibility
44+
if(!Array.isArray) {
45+
Array.isArray = function (vArg) {
46+
return Object.prototype.toString.call(vArg) === "[object Array]";
47+
};
48+
}

lib/browserify/browserInit.js

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,11 @@
22
* This code gets injected at the end of the browserify bundle via b.append().
33
*/
44

5-
if (typeof window.browserifyRequire !== "undefined") {
6-
throw new Error("Naming collision detected: window.browserifyRequire seems to be occupied.");
5+
if (typeof window.browserifyRequire === "undefined") {
6+
// Saves the browserifyRequire under a new name. Necessary to call the original browserifyRequire within
7+
// a module where the variable name "require" is overridden by the module's internal require.
8+
window.browserifyRequire = require;
9+
} else {
10+
throw new Error("(rewire/browserify) Naming collision detected: window.browserifyRequire seems to be occupied.");
711
}
812

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-
var rewireModule = internalRequire("rewire"),
25-
key;
26-
27-
function rewireProxy(path, cache) {
28-
return rewireModule(filename, path, cache);
29-
}
30-
31-
for (key in rewireModule) {
32-
if (rewireModule.hasOwnProperty(key)) {
33-
rewireProxy[key] = rewireModule[key];
34-
}
35-
}
36-
37-
return function requireProxy(path) {
38-
if (path === "rewire") {
39-
return rewireProxy;
40-
} else {
41-
return internalRequire(path);
42-
}
43-
};
44-
};

lib/browserify/browserifyMiddleware.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
2120
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.
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

3032
function 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

Comments
 (0)
0