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:
Diffstat (limited to 'lib/bundlers/webpack/webpackPostLoader.js')
-rw-r--r--lib/bundlers/webpack/webpackPostLoader.js40
1 files changed, 28 insertions, 12 deletions
diff --git a/lib/bundlers/webpack/webpackPostLoader.js b/lib/bundlers/webpack/webpackPostLoader.js
index 40c8cb2..a381f8d 100644
--- a/lib/bundlers/webpack/webpackPostLoader.js
+++ b/lib/bundlers/webpack/webpackPostLoader.js
@@ -6,8 +6,10 @@ var setterSrc = require("../../__set__.js").toString(),
injectRewire = require("../injectRewire.js"),
getRewireRegExp = require("../getRewireRegExp.js"),
- rewireLib = path.join("rewire", "lib"),
- webpackBuildin = path.join("webpack", "buildin", "__webpack"),
+ rewireLib = path.resolve(__dirname, "../../"),
+ projectBasePath = path.resolve(__dirname, "../../../../../"),
+ nodeModulesPath = path.join(projectBasePath, "node_modules"),
+ webpackPath = path.join("node_modules", "webpack"),
settersAndGettersSrc;
/**
@@ -20,16 +22,11 @@ var setterSrc = require("../../__set__.js").toString(),
* @param {!String} src
* @return {String} src
*/
-function webpackLoader(src) {
+function webpackPostLoader(src) {
var filename = this.request.split("!").pop(),
rewireRegExp = getRewireRegExp();
- // 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.
- // We're also omitting webpack's buildin because it doesn't makes sense to rewire these modules. There's also
- // a bug if the special code is injecting into these modules.
- if (filename.indexOf(rewireLib) === -1 && filename.indexOf(webpackBuildin) === -1) {
-
+ if (isRewireableModule(filename)) {
// replaces rewire("some/path") into rewire("some/path", require("some/path"))
src = src.replace(rewireRegExp, '$1rewire("$2", require("$2"))');
@@ -40,8 +37,27 @@ function webpackLoader(src) {
return src;
}
-webpackLoader.loader = __filename;
-webpackLoader.test = /\.js$/;
+webpackPostLoader.loader = __filename;
+webpackPostLoader.test = /\.js$/;
+
+/**
+ * Returns true if the module is rewireable. Rewireable are all modules of the project.
+ *
+ * Example:
+ * Imagine rewire lies under "~/myProject/node_modules/rewire". All files in "~/myProject" are rewireable except
+ * the "~/myProject/node_modules"-path.
+ *
+ * @param {!String} path
+ * @return {Boolean}
+ */
+function isRewireableModule(path) {
+ return path.indexOf(projectBasePath) !== -1 &&
+ path.indexOf(nodeModulesPath) === -1 &&
+
+ // "rewire/lib" and "node_modules/webpack" are explicitly excluded to make the tests work
+ path.indexOf(rewireLib) === -1 &&
+ path.indexOf(webpackPath) === -1;
+}
/**
* This string gets injected at the beginning of every module. Its purpose is to
@@ -61,4 +77,4 @@ settersAndGettersSrc = (
'rewire = undefined;'
).replace(/\s+/g, " "); // strip out unnecessary spaces to be unobtrusive in the debug view
-module.exports = webpackLoader; \ No newline at end of file
+module.exports = webpackPostLoader; \ No newline at end of file