8000 - fixed bug when using b.addEntry() · ShMcK/rewire-coderoad@dfe9ba7 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfe9ba7

Browse files
author
Johannes
committed
- fixed bug when using b.addEntry()
- small fixes
1 parent 8bfdd97 commit dfe9ba7

14 files changed

+222
-232
lines changed

lib/__get__.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
*/
1212
function __get__() {
1313
arguments.varName = arguments[0];
14-
if (typeof arguments.varName !== "string" || arguments.varName.length === 0) {
14+
if (arguments.varName && typeof arguments.varName === "string") {
15+
return eval(arguments.varName);
16+
} else {
1517
throw new TypeError("__get__ expects a non-empty string");
1618
}
17-
18-
return eval(arguments.varName);
1919
}
2020

2121
module.exports = __get__;

lib/__set__.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function __set__() {
2020
varName + " is not declared within the module.'); } ";
2121
};
2222

23-
if (typeof arguments[0] === "object") {
23+
if (typeof arguments[0] === "object" && arguments.length === 1) {
2424
arguments.env = arguments.varName;
2525
if (!arguments.env || Array.isArray(arguments.env)) {
2626
throw new TypeError("__set__ expects an object as env");
@@ -31,7 +31,7 @@ function __set__() {
3131
arguments.src += arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.env." + arguments.varName + ";";
3232
}
3333
}
34-
} else if (typeof arguments.varName === "string") {
34+
} else if (typeof arguments.varName === "string" && arguments.length === 2) {
3535
if (!arguments.varName) {
3636
throw new TypeError("__set__ expects a non-empty string as a variable name");
3737
}

lib/browserify/browserInit.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/browserify/browserifyMiddleware.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var setterSrc = require("../__set__.js").toString(),
66
getRewireRequires = require("./getRewireRequires.js"),
77
detectStrictMode = require("../detectStrictMode.js"),
88

9-
browserInit = fs.readFileSync(__dirname + "/browserInit.js", "utf8"),
109
injectionSrc = getInjectionSrc().replace(/\s+/g, " "); // strip out unnecessary spaces to be unobtrusive in the debug view
1110

1211
/**
@@ -30,6 +29,14 @@ function getInjectionSrc() {
3029
}
3130

3231
function browserifyMiddleware(b) {
32+
33+
/**
34+
* Gets called for every module. Injects special code so rewire is able to access private variables.
35+
*
36+
* @param {String} src
37+
* @param {String} filename
38+
* @return {String}
39+
*/
3340
function injectRewire(src, filename) {
3441
var rewireRequires;
3542

@@ -67,12 +74,12 @@ function browserifyMiddleware(b) {
6774
// The module src is wrapped inside a self-executing function.
6875
// This is necessary to separate the module src from the preceding eval(importGlobalsSrc),
6976
// 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
77+
// In strict mode eval() can only declare vars in the current scope. In this case our setters
7178
// and getters won't work.
7279
// @see https://developer.mozilla.org/en/JavaScript/Strict_mode#Making_eval_and_arguments_simpler
7380
"(function () {" +
7481

75-
// If the module uses strict mode we must ensure that "use strict" stays at the beginning of the module.
82+
// If the module uses strict mode we must ensure that "use strict" stays at the beginning of the function.
7683
(detectStrictMode(src)? ' "use strict"; ': ' ') +
7784

7885
injectionSrc + "\n" +
@@ -86,8 +93,6 @@ function browserifyMiddleware(b) {
8693

8794
// Register file handler
8895
b.register(".js", injectRewire);
89-
// Append rewire initialization at the end of the bundle
90-
b.append(browserInit);
9196

9297
return b;
9398
}

lib/browserify/browserifyRewire.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require("../browser/shims.js"); // some shims for older browsers that are used by rewire()
1+
require("../browser/shims.js"); // some shims for older browsers that are necessary for rewire()
22

3-
var browserifyRequire = window.browserifyRequire,
3+
var pathUtil = require("path"),
44
getImportGlobalsSrc = require("../getImportGlobalsSrc.js");
55

66
/**
@@ -60,15 +60,21 @@ function browserifyRewire(parentModulePath, path, cache) {
6060
cache = true;
6161
}
6262

63-
// Normalize path with file extensions
64-
absPath = browserifyRequire.resolve(path, parentModulePath);
63+
// Normalize path with file extensions
64+
absPath = pathUtil.resolve(parentModulePath, path);
6565

66-
// Delete the original module from the cache so the next call to browserifyRequre()
67-
// executes the module
68-
delete browserifyRequire.cache[absPath];
66+
// Retrieve original module from cache
67+
originalModule = require.cache[absPath];
6968

70-
// Require module to trigger rewire.register()
71-
browserifyRequire(path, parentModulePath);
69+
if (originalModule && cache) {
70+
// Delete the original module from the cache so the next call to browserifyRequre()
71+
// executes the module
72+
delete require.cache[absPath];
73+
}
74+
75+
// Require module to trigger rewire.register().
76+
// Putting (require) in brackets hides it for browserify.
77+
(require)(absPath);
7278

7379
// Get registry entry of the target module
7480
registryEntry = registry[absPath];
@@ -84,9 +90,7 @@ function browserifyRewire(parentModulePath, path, cache) {
8490
rewiredExports.__get__ = registryEntry.getter;
8591

8692
if (cache) {
87-
browserifyRequire.cache[absPath] = rewiredModule;
88-
} else {
89-
browserifyRequire.cache[absPath] = originalModule; // returning originalModule to the cache
93+
require.cache[absPath] = rewiredModule;
9094
}
9195

9296
// Store rewired modules for rewire.reset()
@@ -114,7 +118,7 @@ browserifyRewire.register = function (filename, module, setter, getter) {
114118
* Deletes all rewired modules from the cache
115119
*/
116120
browserifyRewire.reset = function () {
117-
var cache = browserifyRequire.cache,
121+
var cache = require.cache,
118122
i,
119123
absPath;
120124

@@ -174,9 +178,9 @@ browserifyRewire.getProxy = function (internalRequire, dirname) {
174178
/**
175179
* Scans for global vars an returns an evalable string that declares all globals as a var.
176180
* This way a global variable can be overridden by __set__ without changing the global instance.
177-
* It is
181+
* It is executed each time again to include global variables that have been added later.
178182
*
179-
* @type {String}
183+
* @return {String}
180184
*/
181185
browserifyRewire.getImportGlobalsSrc = function () {
182186
return getImportGlobalsSrc(['require','module','exports','__dirname','__filename','process']);

test/__get__.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var expect = require("expect.js"),
77

88
function expectError(ErrConstructor) {
99
return function expectReferenceError(err) {
10-
expect(err.constructor.name).to.be(ErrConstructor.name);
10+
expect(err.constructor.name === ErrConstructor.name).to.be(true);
1111
};
1212
}
1313

@@ -17,6 +17,7 @@ describe("__get__", function () {
1717

1818
beforeEach(function () {
1919
moduleFake = {
20+
__filename: "some/file.js",
2021
myNumber: 0,
2122
myObj: {}
2223
};
@@ -25,20 +26,21 @@ describe("__get__", function () {
2526
"__get__ = " + __get__.toString() + "; " +
2627
"setNumber = function (value) { myNumber = value; }; " +
2728
"setObj = function (value) { myObj = value; }; ",
28-
moduleFake
29+
moduleFake,
30+
__filename
2931
);
3032
});
3133
it("should return the initial value", function () {
32-
expect(moduleFake.__get__("myNumber")).to.be(0);
34+
expect(moduleFake.__get__("myNumber") === 0).to.be(true);
3335
expect(moduleFake.__get__("myObj")).to.eql({});
3436
});
3537
it("should return the changed value of the number", function () {
3638
var newObj = { hello: "hello" };
3739

3840
moduleFake.setNumber(2);
3941
moduleFake.setObj(newObj);
40-
expect(moduleFake.__get__("myNumber")).to.be(2);
41-
expect(moduleFake.__get__("myObj")).to.be(newObj);
42+
expect(moduleFake.__get__("myNumber") === 2).to.be(true);
43+
expect(moduleFake.__get__("myObj") === newObj).to.be(true);
4244
});
4345
it("should throw a ReferenceError when getting not existing vars", function () {
4446
expect(function () {

test/__set__.test.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var expect = require("expect.js"),
77

88
function expectError(ErrConstructor) {
99
return function expectReferenceError(err) {
10-
expect(err.constructor.name).to.be(ErrConstructor.name);
10+
expect(err.constructor.name === ErrConstructor.name).to.be(true);
1111
};
1212
}
1313

@@ -35,31 +35,31 @@ describe("__set__", function () {
3535
);
3636
});
3737
it("should set the new number when calling with varName, varValue", function () {
38-
expect(moduleFake.getNumber()).to.be(0);
38+
expect(moduleFake.getNumber() === 0).to.be(true);
3939
moduleFake.__set__("myNumber", 2);
40-
expect(moduleFake.getNumber()).to.be(2);
40+
expect(moduleFake.getNumber() === 2).to.be(true);
4141
});
4242
it("should set the new object when calling with varName, varValue", function () {
4343
var newObj = { hello: "hello" };
4444

4545
expect(moduleFake.getObj()).to.eql({});
4646
moduleFake.__set__("myObj", newObj);
47-
expect(moduleFake.getObj()).to.be(newObj);
47+
expect(moduleFake.getObj() === newObj).to.be(true);
4848
});
4949
it("should set the new number and the new obj when calling with an env-obj", function () {
5050
var newObj = { hello: "hello" };
5151

52-
expect(moduleFake.getNumber()).to.be(0);
52+
expect(moduleFake.getNumber() === 0).to.be(true);
5353
expect(moduleFake.getObj()).to.eql({});
5454
moduleFake.__set__({
5555
myNumber: 2,
5656
myObj: newObj
5757
});
58-
expect(moduleFake.getNumber()).to.be(2);
59-
expect(moduleFake.getObj()).to.be(newObj);
58+
expect(moduleFake.getNumber() === 2).to.be(true);
59+
expect(moduleFake.getObj() === newObj).to.be(true);
6060
});
6161
it("should return undefined", function () {
62-
expect(moduleFake.__set__("myNumber", 4)).to.be(undefined);
62+
expect(moduleFake.__set__("myNumber", 4) === undefined).to.be(true);
6363
});
6464
it("should throw a ReferenceError when trying to set non-existing vars", function () {
6565
expect(function () {
@@ -71,9 +71,6 @@ describe("__set__", function () {
7171
notExistingAsWell: "blabla"
7272
});
7373
}).to.throwException(expectReferenceError);
74-
});
75-
it("should not clash with vars used within the set method", function () {
76-
7774
});
7875
it("should throw a TypeError when passing misfitting params", function () {
7976
expect(function () {
@@ -97,5 +94,11 @@ describe("__set__", function () {
9794
expect(function () {
9895
moduleFake.__set__(function () {});
9996
}).to.throwException(expectTypeError);
97+
expect(function () {
98+
moduleFake.__set__({}, true); // misfitting number of params
5750
99+
}).to.throwException(expectTypeError);
100+
expect(function () {
101+
moduleFake.__set__("someVar"); // misfitting number of params
102+
}).to.throwException(expectTypeError);
100103
});
101104
});

0 commit comments

Comments
 (0)
0