Skip to content

Commit 699fe21

Browse files
authored
Merge pull request webpack#7750 from ak-14/node-dependencies
Handle module.require, require.main.require, and module.parent.require
2 parents 945a951 + 80c3e43 commit 699fe21

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

lib/NodeStuffPlugin.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ class NodeStuffPlugin {
108108
"require.extensions is not supported by webpack. Use a loader instead."
109109
)
110110
);
111+
parser.hooks.expression
112+
.for("require.main.require")
113+
.tap(
114+
"NodeStuffPlugin",
115+
ParserHelpers.expressionIsUnsupported(
116+
parser,
117+
"require.main.require is not supported by webpack."
118+
)
119+
);
120+
parser.hooks.expression
121+
.for("module.parent.require")
122+
.tap(
123+
"NodeStuffPlugin",
124+
ParserHelpers.expressionIsUnsupported(
125+
parser,
126+
"module.parent.require is not supported by webpack."
127+
)
128+
);
111129
parser.hooks.expression
112130
.for("module.loaded")
113131
.tap("NodeStuffPlugin", expr => {

lib/dependencies/CommonJsRequireDependencyParserPlugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ class CommonJsRequireDependencyParserPlugin {
125125
parser.hooks.new
126126
.for("require")
127127
.tap("CommonJsRequireDependencyParserPlugin", createHandler(true));
128+
parser.hooks.call
129+
.for("module.require")
130+
.tap("CommonJsRequireDependencyParserPlugin", createHandler(false));
131+
parser.hooks.new
132+
.for("module.require")
133+
.tap("CommonJsRequireDependencyParserPlugin", createHandler(true));
128134
}
129135
}
130136
module.exports = CommonJsRequireDependencyParserPlugin;

test/Errors.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,42 @@ describe("Errors", () => {
9494
}
9595
);
9696
});
97+
it("should report require.main.require as unsupported", done => {
98+
getErrors(
99+
{
100+
mode: "development",
101+
entry: "./require.main.require"
102+
},
103+
(errors, warnings) => {
104+
expect(errors).toHaveLength(0);
105+
expect(warnings).toHaveLength(1);
106+
const lines = warnings[0].split("\n");
107+
expect(lines[0]).toMatch(/require.main.require\.js/);
108+
expect(lines[1]).toMatch(
109+
/require.main.require is not supported by webpack/
110+
);
111+
done();
112+
}
113+
);
114+
});
115+
it("should report module.parent.require as unsupported", done => {
116+
getErrors(
117+
{
118+
mode: "development",
119+
entry: "./module.parent.require"
120+
},
121+
(errors, warnings) => {
122+
expect(errors).toHaveLength(0);
123+
expect(warnings).toHaveLength(1);
124+
const lines = warnings[0].split("\n");
125+
expect(lines[0]).toMatch(/module.parent.require\.js/);
126+
expect(lines[1]).toMatch(
127+
/module.parent.require is not supported by webpack/
128+
);
129+
done();
130+
}
131+
);
132+
});
97133
it("should warn about case-sensitive module names", done => {
98134
getErrors(
99135
{

test/cases/parsing/issue-7728/a.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function test() {
2+
return "OK";
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
it("should detect module.require dependency", function () {
2+
var test1 = module.require('./a').default;
3+
expect(test1()).toBe("OK");
4+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.parent.require('./file');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require.main.require('./file');

0 commit comments

Comments
 (0)