Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes <johannes.ewald@roomieplanet.de>2012-06-23 16:10:34 +0400
committerJohannes <johannes.ewald@roomieplanet.de>2012-06-23 16:10:34 +0400
commitd432d902513b69eb216c95505f755f616b9cf698 (patch)
tree7b488730232696aacd8ede60defc4c39a65c9960
parentb4c182b65a7d3ad4f8ab539fd24e138f86db6f0a (diff)
added comments
-rw-r--r--lib/browserify/browserInit.js (renamed from lib/browserify/appendix.js)0
-rw-r--r--lib/browserify/browserifyMiddleware.js65
-rw-r--r--test/testModules/moduleA.js8
-rw-r--r--test/testModules/moduleB.js8
-rw-r--r--test/testModules/sharedTestCases.js12
5 files changed, 52 insertions, 41 deletions
diff --git a/lib/browserify/appendix.js b/lib/browserify/browserInit.js
index 02fd9c1..02fd9c1 100644
--- a/lib/browserify/appendix.js
+++ b/lib/browserify/browserInit.js
diff --git a/lib/browserify/browserifyMiddleware.js b/lib/browserify/browserifyMiddleware.js
index 5b5e042..619049c 100644
--- a/lib/browserify/browserifyMiddleware.js
+++ b/lib/browserify/browserifyMiddleware.js
@@ -6,54 +6,87 @@ var setterSrc = require("../__set__.js").toString(),
getRewireRequires = require("./getRewireRequires.js"),
detectStrictMode = require("../detectStrictMode.js"),
- appendix = fs.readFileSync(__dirname + "/appendix.js", "utf8"),
+ browserInit = fs.readFileSync(__dirname + "/browserInit.js", "utf8"),
importGlobalsSrc = getImportGlobalsSrc(),
injectionSrc = getInjectionSrc().replace(/\s+/g, " "); // strip out unnecessary spaces to be unobtrusive in the debug view
+/**
+ * Returns a string that gets injected at the beginning of every module. Its purpose is to
+ *
+ * - register the setters and getters according to the module's filename
+ * - override the internal require with a require proxy.
+ *
+ * @return {String}
+ */
function getInjectionSrc() {
+ // Registers the setters and getters of every module according to their filename. The setters and getters must be
+ // injected as string here to gain access to the private scope of the module.
return 'require("rewire").register(__filename, ' + setterSrc + ', ' + getterSrc + ');' +
- 'process = require("__browserify_process");' +
+ // Overrides the module internal require with a require proxy. This proxy is necessary to call rewire with the
+ // module's filename at the first parameter to resolve the path. This way rewire() works exactly like require().
'require = window.browserifyRequire.getProxy(require, __filename);';
}
-function browserifyMiddleware(b) {
- var strictMode;
+function wrapCodeInDecorativeComments(filename, src) {
+ var topLine = "",
+ bottomLine = "",
+ lineLength = 80;
+
+ while (topLine.length <= lineLength) {
- b.register(".js", function injectRewire(src, filename) {
- var rewireRequires = getRewireRequires(src),
+ }
+}
+
+function browserifyMiddleware(b) {
+ function injectRewire(src, filename) {
+ var rewireRequires,
strictMode = "";
+ // Search for all rewire() statements an return the required path.
+ rewireRequires = getRewireRequires(src);
+
// Add all modules that are loaded by rewire() manually to browserify because browserify's
// require-sniffing doesn't work here.
rewireRequires.forEach(function forEachRewireRequire(requirePath) {
-
+ // Resolve absolute paths
if (requirePath.charAt(0) === ".") {
requirePath = path.resolve(path.dirname(filename), requirePath);
}
b.require(requirePath);
-
});
+ // If the module uses strict mode we must ensure that "use strict" stays at the beginning of the module.
if (detectStrictMode(src) === true) {
strictMode = ' "use strict"; ';
}
+ // Convert back slashes to normal slashes.
filename = filename.replace(/\\/g, "/");
+
+ // We don't want to inject this code at the beginning of a rewire/lib-module. Otherwise
+ // it would cause a black hole that devours our universe.
if (filename.indexOf("/rewire/lib") === -1) {
src =
- strictMode +
- "var global = window; " +
+ strictMode + // either '' or ' "use strict"; '
+ "var global = window; " + // window is our new global object
importGlobalsSrc +
- injectionSrc +
+ injectionSrc + "\n" +
// For a better debugging experience we're adding a comment with the filename
- "\n//// " + filename + " /////////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n" +
- src +
- "\n\n////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\n";
+ "//// " + filename + " /////////////////////////////////////////////////////////////////////////////////////////////////////////////\n" +
+ "\n" +
+ src + "\n" +
+ "\n" +
+ "/////" + filename.replace(/./g, "/") + "//////////////////////////////////////////////////////////////////////////////////////////////////////////////\n" +
+ "//@ sourceURL=" + filename + "\n";
}
return src;
- });
- b.append(appendix);
+ }
+
+ // Register file handler
+ b.register(".js", injectRewire);
+ // Append rewire initialization at the end of the bundle
+ b.append(browserInit);
return b;
}
diff --git a/test/testModules/moduleA.js b/test/testModules/moduleA.js
index fd665f7..6e655ab 100644
--- a/test/testModules/moduleA.js
+++ b/test/testModules/moduleA.js
@@ -31,9 +31,6 @@ function checkSomeGlobals() {
if (typeof global === "undefined") {
throw new ReferenceError("global is undefined");
}
- if (typeof process === "undefined") {
- throw new ReferenceError("process is undefined");
- }
if (typeof console === "undefined") {
throw new ReferenceError("console is undefined");
}
@@ -52,10 +49,6 @@ function getConsole() {
return console;
}
-function getProcess() {
- return process;
-}
-
function getFilename() {
return __filename;
}
@@ -68,6 +61,5 @@ exports.getMyObj = getMyObj;
exports.readFileSync = readFileSync;
exports.checkSomeGlobals = checkSomeGlobals;
exports.getConsole = getConsole;
-exports.getProcess = getProcess;
exports.getFilename = getFilename;
exports.someOtherModule = someOtherModule; \ No newline at end of file
diff --git a/test/testModules/moduleB.js b/test/testModules/moduleB.js
index fd665f7..6e655ab 100644
--- a/test/testModules/moduleB.js
+++ b/test/testModules/moduleB.js
@@ -31,9 +31,6 @@ function checkSomeGlobals() {
if (typeof global === "undefined") {
throw new ReferenceError("global is undefined");
}
- if (typeof process === "undefined") {
- throw new ReferenceError("process is undefined");
- }
if (typeof console === "undefined") {
throw new ReferenceError("console is undefined");
}
@@ -52,10 +49,6 @@ function getConsole() {
return console;
}
-function getProcess() {
- return process;
-}
-
function getFilename() {
return __filename;
}
@@ -68,6 +61,5 @@ exports.getMyObj = getMyObj;
exports.readFileSync = readFileSync;
exports.checkSomeGlobals = checkSomeGlobals;
exports.getConsole = getConsole;
-exports.getProcess = getProcess;
exports.getFilename = getFilename;
exports.someOtherModule = someOtherModule; \ No newline at end of file
diff --git a/test/testModules/sharedTestCases.js b/test/testModules/sharedTestCases.js
index 01f7e7b..b705e40 100644
--- a/test/testModules/sharedTestCases.js
+++ b/test/testModules/sharedTestCases.js
@@ -37,7 +37,7 @@ function cleanRequireCache() {
}
}
-describe("rewire " + (typeof window === "undefined"? "in node.js": "in the browser"), function () {
+describe("rewire " + (typeof window === "undefined"? "(node.js)": "(browser)"), function () {
beforeEach(cleanRequireCache); // ensuring a clean test environment
it("should work like require()", function () {
expect(rewire("./moduleA.js")).to.be(require("./moduleA.js"));
@@ -109,25 +109,19 @@ describe("rewire " + (typeof window === "undefined"? "in node.js": "in the brows
var rewiredModuleA = rewire("./moduleA.js"),
rewiredModuleB = rewire("./moduleB.js"),
consoleMock = {},
- processMock = {},
newFilename = "myFile.js";
rewiredModuleA.__set__({
- console: consoleMock,
- process: processMock
+ console: consoleMock
});
rewiredModuleA.__set__("__filename", newFilename);
rewiredModuleB.__set__({
- console: consoleMock,
- process: processMock
+ console: consoleMock
});
rewiredModuleB.__set__("__filename", newFilename);
expect(rewiredModuleA.getConsole()).to.be(consoleMock);
expect(rewiredModuleB.getConsole()).to.be(consoleMock);
expect(console).not.to.be(consoleMock);
- expect(rewiredModuleA.getProcess()).to.be(processMock);
- expect(rewiredModuleB.getProcess()).to.be(processMock);
- expect(process).not.to.be(processMock);
expect(rewiredModuleA.getFilename()).to.be(newFilename);
expect(rewiredModuleB.getFilename()).to.be(newFilename);
});