diff --git a/lib/NodeStuffPlugin.js b/lib/NodeStuffPlugin.js index 9ee407cd6df..75d2e73b063 100644 --- a/lib/NodeStuffPlugin.js +++ b/lib/NodeStuffPlugin.js @@ -108,6 +108,24 @@ class NodeStuffPlugin { "require.extensions is not supported by webpack. Use a loader instead." ) ); + parser.hooks.expression + .for("require.main.require") + .tap( + "NodeStuffPlugin", + ParserHelpers.expressionIsUnsupported( + parser, + "require.main.require is not supported by webpack." + ) + ); + parser.hooks.expression + .for("module.parent.require") + .tap( + "NodeStuffPlugin", + ParserHelpers.expressionIsUnsupported( + parser, + "module.parent.require is not supported by webpack." + ) + ); parser.hooks.expression .for("module.loaded") .tap("NodeStuffPlugin", expr => { diff --git a/lib/dependencies/CommonJsRequireDependencyParserPlugin.js b/lib/dependencies/CommonJsRequireDependencyParserPlugin.js index 29dc81749b2..875657348a7 100644 --- a/lib/dependencies/CommonJsRequireDependencyParserPlugin.js +++ b/lib/dependencies/CommonJsRequireDependencyParserPlugin.js @@ -125,6 +125,12 @@ class CommonJsRequireDependencyParserPlugin { parser.hooks.new .for("require") .tap("CommonJsRequireDependencyParserPlugin", createHandler(true)); + parser.hooks.call + .for("module.require") + .tap("CommonJsRequireDependencyParserPlugin", createHandler(false)); + parser.hooks.new + .for("module.require") + .tap("CommonJsRequireDependencyParserPlugin", createHandler(true)); } } module.exports = CommonJsRequireDependencyParserPlugin; diff --git a/test/Errors.test.js b/test/Errors.test.js index ab5c5afb332..8d5a8fbd1be 100644 --- a/test/Errors.test.js +++ b/test/Errors.test.js @@ -94,6 +94,42 @@ describe("Errors", () => { } ); }); + it("should report require.main.require as unsupported", done => { + getErrors( + { + mode: "development", + entry: "./require.main.require" + }, + (errors, warnings) => { + expect(errors).toHaveLength(0); + expect(warnings).toHaveLength(1); + const lines = warnings[0].split("\n"); + expect(lines[0]).toMatch(/require.main.require\.js/); + expect(lines[1]).toMatch( + /require.main.require is not supported by webpack/ + ); + done(); + } + ); + }); + it("should report module.parent.require as unsupported", done => { + getErrors( + { + mode: "development", + entry: "./module.parent.require" + }, + (errors, warnings) => { + expect(errors).toHaveLength(0); + expect(warnings).toHaveLength(1); + const lines = warnings[0].split("\n"); + expect(lines[0]).toMatch(/module.parent.require\.js/); + expect(lines[1]).toMatch( + /module.parent.require is not supported by webpack/ + ); + done(); + } + ); + }); it("should warn about case-sensitive module names", done => { getErrors( { diff --git a/test/cases/parsing/issue-7728/a.js b/test/cases/parsing/issue-7728/a.js new file mode 100644 index 00000000000..e6a3f0e24c7 --- /dev/null +++ b/test/cases/parsing/issue-7728/a.js @@ -0,0 +1,3 @@ +export default function test() { + return "OK"; +} diff --git a/test/cases/parsing/issue-7728/index.js b/test/cases/parsing/issue-7728/index.js new file mode 100644 index 00000000000..f685c315367 --- /dev/null +++ b/test/cases/parsing/issue-7728/index.js @@ -0,0 +1,4 @@ +it("should detect module.require dependency", function () { + var test1 = module.require('./a').default; + expect(test1()).toBe("OK"); +}); diff --git a/test/fixtures/errors/module.parent.require.js b/test/fixtures/errors/module.parent.require.js new file mode 100644 index 00000000000..f13326a68e1 --- /dev/null +++ b/test/fixtures/errors/module.parent.require.js @@ -0,0 +1 @@ +module.parent.require('./file'); diff --git a/test/fixtures/errors/require.main.require.js b/test/fixtures/errors/require.main.require.js new file mode 100644 index 00000000000..b29827f3ed4 --- /dev/null +++ b/test/fixtures/errors/require.main.require.js @@ -0,0 +1 @@ +require.main.require('./file');