Skip to content

Commit 2125e24

Browse files
committed
fixes webpack#2084
1 parent 59c0c4a commit 2125e24

File tree

8 files changed

+77
-10
lines changed

8 files changed

+77
-10
lines changed

lib/dependencies/AMDDefineDependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ AMDDefineDependency.prototype = Object.create(NullDependency.prototype);
1717
AMDDefineDependency.prototype.constructor = AMDDefineDependency;
1818
AMDDefineDependency.prototype.type = "amd define";
1919

20-
AMDDefineDependency.Template = function AMDRequireDependencyTemplate() {};
20+
AMDDefineDependency.Template = function AMDDefineDependencyTemplate() {};
2121

2222
AMDDefineDependency.Template.prototype.apply = function(dep, source) {
2323
var localModuleVar = dep.localModule && dep.localModule.used && dep.localModule.variableName();

lib/dependencies/AMDRequireDependenciesBlock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function AMDRequireDependenciesBlock(expr, arrayRange, functionRange, module, lo
1111
this.outerRange = expr.range;
1212
this.arrayRange = arrayRange;
1313
this.functionRange = functionRange;
14+
this.bindThis = true;
1415
this.range = arrayRange && functionRange ? [arrayRange[0], functionRange[1]] :
1516
arrayRange ? arrayRange :
1617
functionRange ? functionRange :

lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var LocalModuleDependency = require("./LocalModuleDependency");
1010
var ContextDependencyHelpers = require("./ContextDependencyHelpers");
1111
var LocalModulesHelpers = require("./LocalModulesHelpers");
1212
var ConstDependency = require("./ConstDependency");
13+
var getFunctionExpression = require("./getFunctionExpression");
1314

1415
function AMDRequireDependenciesBlockParserPlugin(options) {
1516
this.options = options;
@@ -46,15 +47,23 @@ AMDRequireDependenciesBlockParserPlugin.prototype.apply = function(parser) {
4647
result = this.applyPluginsBailResult("call require:amd:array", expr, param);
4748
}.bind(this));
4849
if(!result) return;
49-
if(expr.arguments[1].type === "FunctionExpression") {
50-
this.inScope(expr.arguments[1].params.filter(function(i) {
50+
var fnData = getFunctionExpression(expr.arguments[1]);
51+
if(fnData) {
52+
this.inScope(fnData.fn.params.filter(function(i) {
5153
return ["require", "module", "exports"].indexOf(i.name) < 0;
5254
}), function() {
53-
if(expr.arguments[1].body.type === "BlockStatement")
54-
this.walkStatement(expr.arguments[1].body);
55+
if(fnData.fn.body.type === "BlockStatement")
56+
this.walkStatement(fnData.fn.body);
5557
else
56-
this.walkExpression(expr.arguments[1].body);
58+
this.walkExpression(fnData.fn.body);
5759
}.bind(this));
60+
this.walkExpressions(fnData.expressions);
61+
if(fnData.needThis === false) {
62+
// smaller bundles for simple function expression
63+
dep.bindThis = false;
64+
}
65+
} else {
66+
this.walkExpression(expr.arguments[1]);
5867
}
5968
} finally {
6069
this.state.current = old;

lib/dependencies/AMDRequireDependency.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ AMDRequireDependency.Template.prototype.apply = function(dep, source, outputOpti
4646
source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
4747
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
4848
source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
49-
source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, "}" + wrapper[1]);
49+
source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, "}" + (depBlock.bindThis ? ".bind(this)" : "") + wrapper[1]);
5050
} else {
5151
source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1,
5252
"!/* require */(" + asComment(depBlock.chunkReason) + "function() { ");
5353
source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
5454
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
5555
source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
56-
source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, "}())");
56+
source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, "}" + (depBlock.bindThis ? ".call(this)" : "()") + ")");
5757
}
5858
}
5959
};

lib/dependencies/getFunctionExpression.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = function(expr) {
77
if(expr.type === "FunctionExpression") {
88
return {
99
fn: expr,
10-
expressions: []
10+
expressions: [],
11+
needThis: false
1112
};
1213
}
1314
// <FunctionExpression>.bind(<Expression>)
@@ -35,7 +36,8 @@ module.exports = function(expr) {
3536
expr.callee.body.body[0].argument.type === "FunctionExpression") {
3637
return {
3738
fn: expr.callee.body.body[0].argument,
38-
expressions: []
39+
expressions: [],
40+
needThis: true
3941
};
4042
}
4143
};

test/cases/parsing/issue-2084/file.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "file";
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function runWithThis(obj, fn) {
2+
fn.call(obj);
3+
}
4+
5+
it("should bind this context on require callback", function(done) {
6+
require("./file");
7+
runWithThis({ok: true}, function() {
8+
require([], function() {
9+
try {
10+
require("./file").should.be.eql("file");
11+
this.should.be.eql({ok: true});
12+
done();
13+
} catch(e) { done(e); }
14+
}.bind(this));
15+
})
16+
});
17+
18+
it("should bind this context on require callback (loaded)", function(done) {
19+
runWithThis({ok: true}, function() {
20+
require(["./load.js"], function(load) {
21+
try {
22+
require("./file").should.be.eql("file");
23+
load.should.be.eql("load");
24+
this.should.be.eql({ok: true});
25+
done();
26+
} catch(e) { done(e); }
27+
}.bind(this));
28+
})
29+
});
30+
31+
it("should bind this context on require.ensure callback", function(done) {
32+
runWithThis({ok: true}, function() {
33+
require.ensure([], function(require) {
34+
try {
35+
require("./file").should.be.eql("file");
36+
this.should.be.eql({ok: true});
37+
done();
38+
} catch(e) { done(e); }
39+
}.bind(this));
40+
})
41+
});
42+
43+
it("should bind this context on require.ensure callback (loaded)", function(done) {
44+
runWithThis({ok: true}, function() {
45+
require.ensure(["./load.js"], function(require) {
46+
try {
47+
require("./file").should.be.eql("file");
48+
this.should.be.eql({ok: true});
49+
done();
50+
} catch(e) { done(e); }
51+
}.bind(this));
52+
})
53+
});

test/cases/parsing/issue-2084/load.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "load";

0 commit comments

Comments
 (0)