Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 203b816

Browse files
author
Johannes
committed
- Fixed crash when a global module has been used in the browser
1 parent 09f1808 commit 203b816

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
##Changelog
22

3+
###v1.0.1
4+
- Fixed crash when a global module has been used in the browser
5+
36
###v1.0.0
4-
- Removed caching functionality. Now rewire doesn't modify `require.cache` at all.
7+
- Removed caching functionality. Now rewire doesn't modify `require.cache` at all
58
- Added support for [webpack](https://github.com/webpack/webpack)-bundler
69
- Moved browserify-middleware from `rewire.browserify` to `rewire.bundlers.browserify`
710
- Reached stable state :)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Since rewire relies heavily on node's require mechanism it can't be used on the
114114
- [webpack](https://github.com/webpack/webpack)
115115

116116
**Please note:** Unfortunately the line numbers in stack traces have an offset of +2 (browserify) / +1 (webpack).
117-
This is caused by generated code that is added during the bundling process.
117+
This is caused by generated code that is added during the bundling process. I'm working on that ... :)
118118

119119
###browserify
120120

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict"; // run code in ES5 strict mode
2+
3+
var pathUtil = require("path"),
4+
match,
5+
basePath;
6+
7+
8+
9+
match = __dirname.match(/(.*)[\\\/].+[\\\/]rewire[\\\/]lib[\\\/]/);
10+
if (match === null) {
11+
return (/\.js$/);
12+
} else {
13+
basePath = match[1];
14+
basePath.replace(/([\\\/])/g, "\\$1");
15+
return new RegExp()
16+
}

lib/bundlers/webpack/webpackPostLoader.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ var setterSrc = require("../../__set__.js").toString(),
66
injectRewire = require("../injectRewire.js"),
77
getRewireRegExp = require("../getRewireRegExp.js"),
88

9-
rewireLib = path.join("rewire", "lib"),
10-
webpackBuildin = path.join("webpack", "buildin", "__webpack"),
9+
rewireLib = path.resolve(__dirname, "../../"),
10+
projectBasePath = path.resolve(__dirname, "../../../../../"),
11+
nodeModulesPath = path.join(projectBasePath, "node_modules"),
12+
webpackPath = path.join("node_modules", "webpack"),
1113
settersAndGettersSrc;
1214

1315
/**
@@ -20,16 +22,11 @@ var setterSrc = require("../../__set__.js").toString(),
2022
* @param {!String} src
2123
* @return {String} src
2224
*/
23-
function webpackLoader(src) {
25+
function webpackPostLoader(src) {
2426
var filename = this.request.split("!").pop(),
2527
rewireRegExp = getRewireRegExp();
2628

27-
// We don't want to inject this code at the beginning of a rewire/lib-module. Otherwise
28-
// it would cause a black hole that devours our universe.
29-
// We're also omitting webpack's buildin because it doesn't makes sense to rewire these modules. There's also
30-
// a bug if the special code is injecting into these modules.
31-
if (filename.indexOf(rewireLib) === -1 && filename.indexOf(webpackBuildin) === -1) {
32-
29+
if (isRewireableModule(filename)) {
3330
// replaces rewire("some/path") into rewire("some/path", require("some/path"))
3431
src = src.replace(rewireRegExp, '$1rewire("$2", require("$2"))');
3532

@@ -40,8 +37,27 @@ function webpackLoader(src) {
4037
return src;
4138
}
4239

43-
webpackLoader.loader = __filename;
44-
webpackLoader.test = /\.js$/;
40+
webpackPostLoader.loader = __filename;
41+
webpackPostLoader.test = /\.js$/;
42+
43+
/**
44+
* Returns true if the module is rewireable. Rewireable are all modules of the project.
45+
*
46+
* Example:
47+
* Imagine rewire lies under "~/myProject/node_modules/rewire". All files in "~/myProject" are rewireable except
48+
* the "~/myProject/node_modules"-path.
49+
*
50+
* @param {!String} path
51+
* @return {Boolean}
52+
*/
53+
function isRewireableModule(path) {
54+
return path.indexOf(projectBasePath) !== -1 &&
55+
path.indexOf(nodeModulesPath) === -1 &&
56+
57+
// "rewire/lib" and "node_modules/webpack" are explicitly excluded to make the tests work
58+
path.indexOf(rewireLib) === -1 &&
59+
path.indexOf(webpackPath) === -1;
60+
}
4561

4662
/**
4763
* This string gets injected at the beginning of every module. Its purpose is to
@@ -61,4 +77,4 @@ settersAndGettersSrc = (
6177
'rewire = undefined;'
6278
).replace(/\s+/g, " "); // strip out unnecessary spaces to be unobtrusive in the debug view
6379

64-
module.exports = webpackLoader;
80+
module.exports = webpackPostLoader;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "rewire",
3-
"version" : "1.0.0",
3+
"version" : "1.0.1",
44
"description" : "Dependency injection for node.js applications",
55
"keywords" : [
66
"dependency",
@@ -17,9 +17,9 @@
1717
"web" : "http://johannesewald.de"
1818
},
1919
"main" : "lib/index.js",
20-
"homepage": "http://jhnns.github.com/rewire",
20+
"homepage": "https://github.com/jhnns/rewire",
2121
"bugs" : {
22-
"url" : "http://github.com/jhnns/rewire/issues",
22+
"url" : "https://github.com/jhnns/rewire/issues",
2323
"email" : "[email protected]"
2424
},
2525
"repository": {

0 commit comments

Comments
 (0)