Skip to content

Commit

Permalink
Merge pull request #5230 from webpack/bugfix/typeof-require-resolve
Browse files Browse the repository at this point in the history
emit correct code for typeof require.resolve(Weak), require and requirejs.onError
  • Loading branch information
sokra committed Jul 7, 2017
2 parents 4bb3018 + dca2eae commit 3dc0d4e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/RequireJsStuffPlugin.js
Expand Up @@ -23,7 +23,7 @@ module.exports = class RequireJsStuffPlugin {
parser.plugin("call requirejs.config", ParserHelpers.toConstantDependency("undefined"));

parser.plugin("expression require.version", ParserHelpers.toConstantDependency(JSON.stringify("0.0.0")));
parser.plugin("expression requirejs.onError", ParserHelpers.toConstantDependency(JSON.stringify("__webpack_require__.oe")));
parser.plugin("expression requirejs.onError", ParserHelpers.toConstantDependency("__webpack_require__.oe"));
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies/CommonJsPlugin.js
Expand Up @@ -54,7 +54,7 @@ class CommonJsPlugin {

const requireExpressions = ["require", "require.resolve", "require.resolveWeak"];
for(let expression of requireExpressions) {
parser.plugin(`typeof ${expression}`, ParserHelpers.toConstantDependency("function"));
parser.plugin(`typeof ${expression}`, ParserHelpers.toConstantDependency(JSON.stringify("function")));
parser.plugin(`evaluate typeof ${expression}`, ParserHelpers.evaluateToString("function"));
parser.plugin(`evaluate Identifier ${expression}`, ParserHelpers.evaluateToIdentifier(expression, true));
}
Expand Down
2 changes: 1 addition & 1 deletion test/RequireJsStuffPlugin.test.js
Expand Up @@ -170,7 +170,7 @@ describe("RequireJsStuffPlugin", () => {
const addDependencySpy = parserEventContext.state.current.addDependency;
const addedDependency = JSON.stringify(addDependencySpy.getCall(0).args[0]);
addDependencySpy.callCount.should.be.exactly(1);
addedDependency.should.be.exactly('{"module":null,"expression":"\\"__webpack_require__.oe\\"","range":10,"loc":5}');
addedDependency.should.be.exactly('{"module":null,"expression":"__webpack_require__.oe","range":10,"loc":5}');
});
});
});
Expand Down
Empty file.
13 changes: 11 additions & 2 deletions test/cases/parsing/requirejs/index.js
@@ -1,11 +1,20 @@
it("should ignore require.config", function() {
require.config({

});
requirejs.config({

});
});
it("should have a require.version", function() {
require.version.should.be.type("string");
});
it("should have a requirejs.onError function", function() {
function f(){}
requirejs.onError.should.be.type("function"); // has default handler
var org = requirejs.onError;
requirejs.onError = f;
requirejs.onError.should.be.eql(f);
requirejs.onError = org;
require(["./file.js"], function() {});
});
4 changes: 4 additions & 0 deletions test/cases/parsing/typeof/index.js
Expand Up @@ -26,6 +26,9 @@ it("should answer typeof require.include correctly", function() {
it("should answer typeof require.ensure correctly", function() {
(typeof require.ensure).should.be.eql("function");
});
it("should answer typeof require.resolve correctly", function() {
(typeof require.resolve).should.be.eql("function");
});
it("should answer typeof System correctly", function() {
(typeof System).should.be.eql("object");
});
Expand All @@ -41,6 +44,7 @@ it("should not parse filtered stuff", function() {
if(!(typeof require === "function")) require("fail");
if(typeof require == "undefined") require = require("fail");
if(typeof require === "undefined") require = require("fail");
if(typeof require.resolve !== "function") require("fail");
if(typeof module == "undefined") module = require("fail");
if(typeof module === "undefined") module = require("fail");
if(typeof module != "object") module = require("fail");
Expand Down

0 comments on commit 3dc0d4e

Please sign in to comment.