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
path: root/lib
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 /lib
parentb4c182b65a7d3ad4f8ab539fd24e138f86db6f0a (diff)
added comments
Diffstat (limited to 'lib')
-rw-r--r--lib/browserify/browserInit.js (renamed from lib/browserify/appendix.js)0
-rw-r--r--lib/browserify/browserifyMiddleware.js65
2 files changed, 49 insertions, 16 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;
}