Skip to content

Commit 86aa0f4

Browse files
authored
Merge pull request webpack#3478 from webpack/bugfix/escape-require-context-id
Bugfix/escape require context
2 parents 2b0bf69 + 69d4532 commit 86aa0f4

6 files changed

Lines changed: 42 additions & 4 deletions

File tree

lib/ContextModule.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
var path = require("path");
56
var Module = require("./Module");
67
var OriginalSource = require("webpack-sources").OriginalSource;
78
var RawSource = require("webpack-sources").RawSource;
@@ -56,6 +57,30 @@ ContextModule.prototype.readableIdentifier = function(requestShortener) {
5657
return identifier.replace(/ $/, "");
5758
};
5859

60+
function contextify(options, request) {
61+
return request.split("!").map(function(r) {
62+
var rp = path.relative(options.context, r);
63+
if(path.sep === "\\")
64+
rp = rp.replace(/\\/g, "/");
65+
if(rp.indexOf("../") !== 0)
66+
rp = "./" + rp;
67+
return rp;
68+
}).join("!");
69+
}
70+
71+
ContextModule.prototype.libIdent = function(options) {
72+
var identifier = contextify(options, this.context) + " ";
73+
if(this.async)
74+
identifier += "async ";
75+
if(this.recursive)
76+
identifier += "recursive ";
77+
if(this.addon)
78+
identifier += contextify(options, this.addon);
79+
if(this.regExp)
80+
identifier += prettyRegExp(this.regExp + "")
81+
return identifier.replace(/ $/, "");
82+
};
83+
5984
ContextModule.prototype.needRebuild = function(fileTimestamps, contextTimestamps) {
6085
var ts = contextTimestamps[this.context];
6186
if(!ts) return true;
@@ -121,7 +146,7 @@ ContextModule.prototype.source = function() {
121146
"};\n",
122147
"webpackContext.resolve = webpackContextResolve;\n",
123148
"module.exports = webpackContext;\n",
124-
"webpackContext.id = " + this.id + ";\n"
149+
"webpackContext.id = " + JSON.stringify(this.id) + ";\n"
125150
];
126151
} else if(this.blocks && this.blocks.length > 0) {
127152
var items = this.blocks.map(function(block) {
@@ -165,7 +190,7 @@ ContextModule.prototype.source = function() {
165190
"\treturn Object.keys(map);\n",
166191
"};\n",
167192
"module.exports = webpackAsyncContext;\n",
168-
"webpackAsyncContext.id = " + this.id + ";\n"
193+
"webpackAsyncContext.id = " + JSON.stringify(this.id) + ";\n"
169194
];
170195
} else {
171196
str = [
@@ -175,7 +200,7 @@ ContextModule.prototype.source = function() {
175200
"webpackEmptyContext.keys = function() { return []; };\n",
176201
"webpackEmptyContext.resolve = webpackEmptyContext;\n",
177202
"module.exports = webpackEmptyContext;\n",
178-
"webpackEmptyContext.id = " + this.id + ";\n"
203+
"webpackEmptyContext.id = " + JSON.stringify(this.id) + ";\n"
179204
];
180205
}
181206
if(this.useSourceMap) {

test/cases/context/issue-524/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
it("should support an empty context", function() {
22
var c = require.context(".", true, /^nothing$/);
3-
c.id.should.be.type("number");
3+
(typeof c.id).should.be.oneOf(["number", "string"]);
44
(function() {
55
c.resolve("");
66
}).should.throw();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "a";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "b";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
it("should escape require.context id correctly", function() {
2+
var context = require.context("./folder");
3+
context("./a").should.be.eql("a");
4+
context.id.should.be.type("string");
5+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var webpack = require("../../../../");
2+
module.exports = {
3+
plugins: [
4+
new webpack.HashedModuleIdsPlugin()
5+
]
6+
};

0 commit comments

Comments
 (0)