Skip to content

Commit 6bf9479

Browse files
committed
Don't record absolute paths for regexps used in dynamic requires
This avoids records like: "src/dependencies sync ../../../../^/.//.*$": "./src/dependencies sync recursive ^\\.\\/.*$" with the number of "../"s varying according to the depth of the folder you're building from. Fixes webpack#7339
1 parent 8a6db44 commit 6bf9479

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

lib/util/identifier.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const path = require("path");
1212
* @returns {boolean} returns true if path is "Absolute Path"-like
1313
*/
1414
const looksLikeAbsolutePath = maybeAbsolutePath => {
15+
if (/^\/.*\/$/.test(maybeAbsolutePath)) {
16+
// this 'path' is actually a regexp generated by dynamic requires.
17+
// Don't treat it as an absolute path.
18+
return false;
19+
}
1520
return /^(?:[a-z]:\\|\/)/i.test(maybeAbsolutePath);
1621
};
1722

test/__snapshots__/ConfigTestCases.test.js.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,35 @@ exports[`ConfigTestCases records issue-2991 exported tests should write relative
2727
}
2828
}"
2929
`;
30+
31+
exports[`ConfigTestCases records issue-7339 exported tests should write relative dynamic-require paths to records 1`] = `
32+
"{
33+
\\"modules\\": {
34+
\\"byIdentifier\\": {
35+
\\"dependencies/foo.js\\": 0,
36+
\\"dependencies/bar.js\\": 1,
37+
\\"external \\\\\\"path\\\\\\"\\": 2,
38+
\\"external \\\\\\"fs\\\\\\"\\": 3,
39+
\\"dependencies sync /^\\\\\\\\.\\\\\\\\/.*$/\\": 4,
40+
\\"test.js\\": 5
41+
},
42+
\\"usedIds\\": {
43+
\\"0\\": 0,
44+
\\"1\\": 1,
45+
\\"2\\": 2,
46+
\\"3\\": 3,
47+
\\"4\\": 4,
48+
\\"5\\": 5
49+
}
50+
},
51+
\\"chunks\\": {
52+
\\"byName\\": {
53+
\\"main\\": 0
54+
},
55+
\\"bySource\\": {},
56+
\\"usedIds\\": [
57+
0
58+
]
59+
}
60+
}"
61+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Bar"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "Foo"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function loadDependency(dep) {
2+
require("./dependencies/" + dep);
3+
}
4+
5+
it("should write relative dynamic-require paths to records", function() {
6+
var fs = require("fs");
7+
var path = require("path");
8+
var content = fs.readFileSync(path.join(__dirname, "records.json"), "utf-8");
9+
expect(content).toMatchSnapshot();
10+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var path = require("path");
2+
3+
module.exports = {
4+
entry: "./test",
5+
recordsOutputPath: path.resolve(
6+
__dirname,
7+
"../../../js/config/records/issue-7339/records.json"
8+
),
9+
target: "node",
10+
node: {
11+
__dirname: false
12+
}
13+
};

0 commit comments

Comments
 (0)