Skip to content

Commit 6ff5b8f

Browse files
authored
Merge pull request webpack#7350 from jdelStrother/dynamic-requires
Record relative dynamic-require paths
2 parents 182cf52 + 6bf9479 commit 6ff5b8f

File tree

7 files changed

+93
-26
lines changed

7 files changed

+93
-26
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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`ConfigTestCases records issue-2991 exported tests should write relative paths to records 1`] = `
4+
"{
5+
\\"modules\\": {
6+
\\"byIdentifier\\": {
7+
\\"external \\\\\\"path\\\\\\"\\": 0,
8+
\\"external \\\\\\"fs\\\\\\"\\": 1,
9+
\\"ignored pkgs/somepackage/foo\\": 2,
10+
\\"test.js\\": 3
11+
},
12+
\\"usedIds\\": {
13+
\\"0\\": 0,
14+
\\"1\\": 1,
15+
\\"2\\": 2,
16+
\\"3\\": 3
17+
}
18+
},
19+
\\"chunks\\": {
20+
\\"byName\\": {
21+
\\"main\\": 0
22+
},
23+
\\"bySource\\": {},
24+
\\"usedIds\\": [
25+
0
26+
]
27+
}
28+
}"
29+
`;
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: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,10 @@
11
try {
22
require("pkgs/somepackage/foo");
3-
} catch(e){}
3+
} catch (e) {}
44

55
it("should write relative paths to records", function() {
66
var fs = require("fs");
77
var path = require("path");
88
var content = fs.readFileSync(path.join(__dirname, "records.json"), "utf-8");
9-
expect(content).toEqual(`{
10-
"modules": {
11-
"byIdentifier": {
12-
"external \\"path\\"": 0,
13-
"external \\"fs\\"": 1,
14-
"ignored pkgs/somepackage/foo": 2,
15-
"test.js": 3
16-
},
17-
"usedIds": {
18-
"0": 0,
19-
"1": 1,
20-
"2": 2,
21-
"3": 3
22-
}
23-
},
24-
"chunks": {
25-
"byName": {
26-
"main": 0
27-
},
28-
"bySource": {},
29-
"usedIds": [
30-
0
31-
]
32-
}
33-
}`);
9+
expect(content).toMatchSnapshot();
3410
});
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)