diff options
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | lib/bundlers/webpack/getLoaderTestRegExp.js | 16 | ||||
-rw-r--r-- | lib/bundlers/webpack/webpackPostLoader.js | 40 | ||||
-rw-r--r-- | package.json | 6 |
5 files changed, 52 insertions, 17 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c879a1..4479e9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ ##Changelog +###v1.0.1 +- Fixed crash when a global module has been used in the browser + ###v1.0.0 -- Removed caching functionality. Now rewire doesn't modify `require.cache` at all. +- Removed caching functionality. Now rewire doesn't modify `require.cache` at all - Added support for [webpack](https://github.com/webpack/webpack)-bundler - Moved browserify-middleware from `rewire.browserify` to `rewire.bundlers.browserify` - Reached stable state :)
\ No newline at end of file @@ -114,7 +114,7 @@ Since rewire relies heavily on node's require mechanism it can't be used on the - [webpack](https://github.com/webpack/webpack) **Please note:** Unfortunately the line numbers in stack traces have an offset of +2 (browserify) / +1 (webpack). -This is caused by generated code that is added during the bundling process. +This is caused by generated code that is added during the bundling process. I'm working on that ... :) ###browserify diff --git a/lib/bundlers/webpack/getLoaderTestRegExp.js b/lib/bundlers/webpack/getLoaderTestRegExp.js new file mode 100644 index 0000000..40119c2 --- /dev/null +++ b/lib/bundlers/webpack/getLoaderTestRegExp.js @@ -0,0 +1,16 @@ +"use strict"; // run code in ES5 strict mode + +var pathUtil = require("path"), + match, + basePath; + + + +match = __dirname.match(/(.*)[\\\/].+[\\\/]rewire[\\\/]lib[\\\/]/); +if (match === null) { + return (/\.js$/); +} else { + basePath = match[1]; + basePath.replace(/([\\\/])/g, "\\$1"); + return new RegExp() +}
\ No newline at end of file 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 diff --git a/package.json b/package.json index 6565fa7..8dd8573 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "rewire", - "version" : "1.0.0", + "version" : "1.0.1", "description" : "Dependency injection for node.js applications", "keywords" : [ "dependency", @@ -17,9 +17,9 @@ "web" : "http://johannesewald.de" }, "main" : "lib/index.js", - "homepage": "http://jhnns.github.com/rewire", + "homepage": "https://github.com/jhnns/rewire", "bugs" : { - "url" : "http://github.com/jhnns/rewire/issues", + "url" : "https://github.com/jhnns/rewire/issues", "email" : "mail@johannesewald.de" }, "repository": { |