diff options
author | Johannes <johannes.ewald@roomieplanet.de> | 2012-09-15 23:24:59 +0400 |
---|---|---|
committer | Johannes <johannes.ewald@roomieplanet.de> | 2012-09-15 23:24:59 +0400 |
commit | 203b81607a82eb360113efbe8f63d2ed454c5a48 (patch) | |
tree | 75e469213c1aa15b36f8f0122783023968d831d9 /lib | |
parent | 09f18083ddff2281b5ac85f5773ea5c9ba57e841 (diff) |
- Fixed crash when a global module has been used in the browserv1.0.1
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bundlers/webpack/getLoaderTestRegExp.js | 16 | ||||
-rw-r--r-- | lib/bundlers/webpack/webpackPostLoader.js | 40 |
2 files changed, 44 insertions, 12 deletions
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 |