Skip to content

Commit ef17f69

Browse files
committed
fixed polyfill in client code
1 parent 20b9d55 commit ef17f69

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

lib/parse.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,16 @@ function walkExpression(context, expression) {
160160
walkExpression(context, expression.argument);
161161
break;
162162
case "BinaryExpression":
163-
case "AssignmentExpression":
164163
case "LogicalExpression":
165164
walkExpression(context, expression.left);
166165
walkExpression(context, expression.right);
167166
break;
167+
case "AssignmentExpression":
168+
if(expression.left.type !== "Identifier" ||
169+
expression.left.name !== "require")
170+
walkExpression(context, expression.left);
171+
walkExpression(context, expression.right);
172+
break;
168173
case "ConditionalExpression":
169174
walkExpression(context, expression.test);
170175
walkExpression(context, expression.alternate);
@@ -277,6 +282,15 @@ function walkExpression(context, expression) {
277282
context.contexts.push(newContext);
278283
noCallee = true;
279284
}
285+
if(context.overwrite.indexOf("require") === -1 &&
286+
expression.callee &&
287+
expression.callee.type === "MemberExpression" &&
288+
expression.callee.object.type === "Identifier" &&
289+
expression.callee.object.name === "require" &&
290+
expression.callee.property.type === "Identifier" &&
291+
expression.callee.property.name in {valueOf:1}) {
292+
noCallee = true;
293+
}
280294

281295
if(expression.callee && !noCallee)
282296
walkExpression(context, expression.callee);

require-polyfill.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
// Polyfill for node.js
2-
// adds require.ensure
3-
// call it like this: require("webpack/require-polyfill")(require);
4-
// This is only required when you want to use require.ensure or require.context
2+
// - adds require.ensure
3+
// - adds require.context
4+
// call it like this:
5+
// require = require("webpack/require-polyfill")(require.valueOf());
6+
// This is only required when you want to use the special require.xxx methods
57
// in server-side code which should be so only in rar cases.
68
module.exports = function(req) {
9+
if(!req.webpackPolyfill) {
10+
var oldReq = req;
11+
req = function(name) {
12+
return oldReq(name);
13+
};
14+
req.__proto__ = oldReq;
15+
req.webpackPolyfill = true;
16+
}
717
if(!req.ensure) {
818
req.ensure = function(array, callback) {
919
callback(req);
@@ -16,4 +26,5 @@ module.exports = function(req) {
1626
}
1727
}
1828
}
29+
return req;
1930
}

require-polyfill.web.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// No polyfill needed when compiled with webpack
2-
module.exports = function(){}
2+
module.exports = function(r){return r}

test/browsertest/lib/index.web.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Should not break it... should not include complete directory...
2+
require = require("../../../require-polyfill")(require.valueOf());
3+
14
window.test(true, "index.js should be replaced with index.web.js");
25
window.test(window.libary1, "libary1 loaded");
36
window.test(window.libary2.ok, "libary2 loaded");

0 commit comments

Comments
 (0)