Skip to content

Commit 3dc0d4e

Browse files
authored
Merge pull request webpack#5230 from webpack/bugfix/typeof-require-resolve
emit correct code for typeof require.resolve(Weak), require and requirejs.onError
2 parents 4bb3018 + dca2eae commit 3dc0d4e

6 files changed

Lines changed: 18 additions & 5 deletions

File tree

lib/RequireJsStuffPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = class RequireJsStuffPlugin {
2323
parser.plugin("call requirejs.config", ParserHelpers.toConstantDependency("undefined"));
2424

2525
parser.plugin("expression require.version", ParserHelpers.toConstantDependency(JSON.stringify("0.0.0")));
26-
parser.plugin("expression requirejs.onError", ParserHelpers.toConstantDependency(JSON.stringify("__webpack_require__.oe")));
26+
parser.plugin("expression requirejs.onError", ParserHelpers.toConstantDependency("__webpack_require__.oe"));
2727
});
2828
});
2929
}

lib/dependencies/CommonJsPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CommonJsPlugin {
5454

5555
const requireExpressions = ["require", "require.resolve", "require.resolveWeak"];
5656
for(let expression of requireExpressions) {
57-
parser.plugin(`typeof ${expression}`, ParserHelpers.toConstantDependency("function"));
57+
parser.plugin(`typeof ${expression}`, ParserHelpers.toConstantDependency(JSON.stringify("function")));
5858
parser.plugin(`evaluate typeof ${expression}`, ParserHelpers.evaluateToString("function"));
5959
parser.plugin(`evaluate Identifier ${expression}`, ParserHelpers.evaluateToIdentifier(expression, true));
6060
}

test/RequireJsStuffPlugin.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe("RequireJsStuffPlugin", () => {
170170
const addDependencySpy = parserEventContext.state.current.addDependency;
171171
const addedDependency = JSON.stringify(addDependencySpy.getCall(0).args[0]);
172172
addDependencySpy.callCount.should.be.exactly(1);
173-
addedDependency.should.be.exactly('{"module":null,"expression":"\\"__webpack_require__.oe\\"","range":10,"loc":5}');
173+
addedDependency.should.be.exactly('{"module":null,"expression":"__webpack_require__.oe","range":10,"loc":5}');
174174
});
175175
});
176176
});

test/cases/parsing/requirejs/file.js

Whitespace-only changes.
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
it("should ignore require.config", function() {
22
require.config({
3-
3+
44
});
55
requirejs.config({
6-
6+
77
});
88
});
99
it("should have a require.version", function() {
1010
require.version.should.be.type("string");
1111
});
12+
it("should have a requirejs.onError function", function() {
13+
function f(){}
14+
requirejs.onError.should.be.type("function"); // has default handler
15+
var org = requirejs.onError;
16+
requirejs.onError = f;
17+
requirejs.onError.should.be.eql(f);
18+
requirejs.onError = org;
19+
require(["./file.js"], function() {});
20+
});

test/cases/parsing/typeof/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ it("should answer typeof require.include correctly", function() {
2626
it("should answer typeof require.ensure correctly", function() {
2727
(typeof require.ensure).should.be.eql("function");
2828
});
29+
it("should answer typeof require.resolve correctly", function() {
30+
(typeof require.resolve).should.be.eql("function");
31+
});
2932
it("should answer typeof System correctly", function() {
3033
(typeof System).should.be.eql("object");
3134
});
@@ -41,6 +44,7 @@ it("should not parse filtered stuff", function() {
4144
if(!(typeof require === "function")) require("fail");
4245
if(typeof require == "undefined") require = require("fail");
4346
if(typeof require === "undefined") require = require("fail");
47+
if(typeof require.resolve !== "function") require("fail");
4448
if(typeof module == "undefined") module = require("fail");
4549
if(typeof module === "undefined") module = require("fail");
4650
if(typeof module != "object") module = require("fail");

0 commit comments

Comments
 (0)