Skip to content

Commit ad9b957

Browse files
authored
Merge pull request webpack#5980 from bschlenk/amd-arrow-functions
Handle amd define with arrow function
2 parents 3526dda + 28f2ffe commit ad9b957

16 files changed

Lines changed: 168 additions & 13 deletions

File tree

‎lib/dependencies/AMDDefineDependency.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate {
2525
return {
2626
f: [
2727
"var __WEBPACK_AMD_DEFINE_RESULT__;",
28-
`!(__WEBPACK_AMD_DEFINE_RESULT__ = #.call(exports, __webpack_require__, exports, module),
28+
`!(__WEBPACK_AMD_DEFINE_RESULT__ = (#).call(exports, __webpack_require__, exports, module),
2929
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
3030
],
3131
o: [
@@ -42,7 +42,7 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate {
4242
],
4343
af: [
4444
"var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
45-
`!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = #.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
45+
`!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = (#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
4646
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`
4747
],
4848
ao: [
@@ -70,7 +70,7 @@ AMDDefineDependency.Template = class AMDDefineDependencyTemplate {
7070
],
7171
laf: [
7272
"var __WEBPACK_AMD_DEFINE_ARRAY__, XXX;",
73-
"!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = (#.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)))"
73+
"!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = ((#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)))"
7474
],
7575
lao: [
7676
"var XXX;",

‎lib/dependencies/AMDDefineDependencyParserPlugin.js‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ function isBoundFunctionExpression(expr) {
2323
return true;
2424
}
2525

26+
function isUnboundFunctionExpression(expr) {
27+
if(expr.type === "FunctionExpression") return true;
28+
if(expr.type === "ArrowFunctionExpression") return true;
29+
return false;
30+
}
31+
32+
function isCallable(expr) {
33+
if(isUnboundFunctionExpression(expr)) return true;
34+
if(isBoundFunctionExpression(expr)) return true;
35+
return false;
36+
}
37+
2638
class AMDDefineDependencyParserPlugin {
2739
constructor(options) {
2840
this.options = options;
@@ -38,7 +50,7 @@ class AMDDefineDependencyParserPlugin {
3850
let array, fn, obj, namedModule;
3951
switch(expr.arguments.length) {
4052
case 1:
41-
if(expr.arguments[0].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[0])) {
53+
if(isCallable(expr.arguments[0])) {
4254
// define(f() {...})
4355
fn = expr.arguments[0];
4456
} else if(expr.arguments[0].type === "ObjectExpression") {
@@ -54,7 +66,7 @@ class AMDDefineDependencyParserPlugin {
5466
if(expr.arguments[0].type === "Literal") {
5567
namedModule = expr.arguments[0].value;
5668
// define("...", ...)
57-
if(expr.arguments[1].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[1])) {
69+
if(isCallable(expr.arguments[1])) {
5870
// define("...", f() {...})
5971
fn = expr.arguments[1];
6072
} else if(expr.arguments[1].type === "ObjectExpression") {
@@ -67,7 +79,7 @@ class AMDDefineDependencyParserPlugin {
6779
}
6880
} else {
6981
array = expr.arguments[0];
70-
if(expr.arguments[1].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[1])) {
82+
if(isCallable(expr.arguments[1])) {
7183
// define([...], f() {})
7284
fn = expr.arguments[1];
7385
} else if(expr.arguments[1].type === "ObjectExpression") {
@@ -84,7 +96,7 @@ class AMDDefineDependencyParserPlugin {
8496
// define("...", [...], f() {...})
8597
namedModule = expr.arguments[0].value;
8698
array = expr.arguments[1];
87-
if(expr.arguments[2].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[2])) {
99+
if(isCallable(expr.arguments[2])) {
88100
// define("...", [...], f() {})
89101
fn = expr.arguments[2];
90102
} else if(expr.arguments[2].type === "ObjectExpression") {
@@ -102,7 +114,7 @@ class AMDDefineDependencyParserPlugin {
102114
let fnParams = null;
103115
let fnParamsOffset = 0;
104116
if(fn) {
105-
if(fn.type === "FunctionExpression") fnParams = fn.params;
117+
if(isUnboundFunctionExpression(fn)) fnParams = fn.params;
106118
else if(isBoundFunctionExpression(fn)) {
107119
fnParams = fn.callee.object.params;
108120
fnParamsOffset = fn.arguments.length - 1;
@@ -134,7 +146,7 @@ class AMDDefineDependencyParserPlugin {
134146
});
135147
}
136148
let inTry;
137-
if(fn && fn.type === "FunctionExpression") {
149+
if(fn && isUnboundFunctionExpression(fn)) {
138150
inTry = parser.scope.inTry;
139151
parser.inScope(fnParams, () => {
140152
parser.scope.renames = fnRenames;

‎lib/dependencies/AMDRequireDependency.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ AMDRequireDependency.Template = class AMDRequireDependencyTemplate {
4444

4545
source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
4646
source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
47-
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
48-
source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
47+
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; ((");
48+
source.insert(depBlock.functionRange[1], ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
4949
source.replace(depBlock.functionRange[1], depBlock.errorCallbackRange[0] - 1, errorRangeBlock);
5050
source.replace(depBlock.errorCallbackRange[1], depBlock.outerRange[1] - 1, endBlock);
5151
return;
@@ -57,8 +57,8 @@ AMDRequireDependency.Template = class AMDRequireDependencyTemplate {
5757
const endBlock = `}${depBlock.functionBindThis ? ".bind(this)" : ""}${wrapper[1]}__webpack_require__.oe${wrapper[2]}`;
5858
source.replace(depBlock.outerRange[0], depBlock.arrayRange[0] - 1, startBlock);
5959
source.insert(depBlock.arrayRange[0] + 0.9, "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
60-
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; (");
61-
source.insert(depBlock.functionRange[1], ".apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
60+
source.replace(depBlock.arrayRange[1], depBlock.functionRange[0] - 1, "; ((");
61+
source.insert(depBlock.functionRange[1], ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__));");
6262
source.replace(depBlock.functionRange[1], depBlock.outerRange[1] - 1, endBlock);
6363
}
6464
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "a";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
define((require) => {
2+
return require("./a");
3+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "c";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = 1;
2+
module.exports = require("./circular");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function(value) {
2+
this.value = value;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "d";
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
var should = require("should");
2+
3+
it("should parse fancy function calls with arrow functions", function() {
4+
("function"==typeof define && define.amd ?
5+
define :
6+
(e,t) => {return t()}
7+
)(["./constructor"], (c) => {
8+
return new c(1324);
9+
});
10+
module.exports.should.have.property("value").be.eql(1324);
11+
(("function"==typeof define && define.amd ?
12+
define :
13+
(e,t) => {return t()}
14+
)(["./constructor"], (c) => {
15+
return new c(4231);
16+
}));
17+
module.exports.should.have.property("value").be.eql(4231);
18+
});
19+
20+
it("should parse fancy AMD calls with arrow functions", function() {
21+
require("./constructor ./a".split(" "));
22+
require("-> module module exports *constructor *a".replace("module", "require").substr(3).replace(/\*/g, "./").split(" "), (require, module, exports, constructor, a) => {
23+
(typeof require).should.be.eql("function");
24+
(typeof module).should.be.eql("object");
25+
(typeof exports).should.be.eql("object");
26+
(typeof require("./constructor")).should.be.eql("function");
27+
(typeof constructor).should.be.eql("function");
28+
a.should.be.eql("a");
29+
});
30+
define("-> module module exports *constructor *a".replace("module", "require").substr(3).replace(/\*/g, "./").split(" "), (require, module, exports, constructor, a) => {
31+
(typeof require).should.be.eql("function");
32+
(typeof module).should.be.eql("object");
33+
(typeof exports).should.be.eql("object");
34+
(typeof require("./constructor")).should.be.eql("function");
35+
(typeof constructor).should.be.eql("function");
36+
a.should.be.eql("a");
37+
});
38+
});
39+
40+
it("should be able to use AMD-style require with arrow functions", function(done) {
41+
var template = "b";
42+
require(["./circular", "./templates/" + template, true ? "./circular" : "fail"], (circular, testTemplate, circular2) => {
43+
circular.should.be.eql(1);
44+
circular2.should.be.eql(1);
45+
testTemplate.should.be.eql("b");
46+
done();
47+
});
48+
});
49+
50+
it("should be able to use require.js-style define with arrow functions", function(done) {
51+
define("name", ["./circular"], (circular) => {
52+
circular.should.be.eql(1);
53+
done();
54+
});
55+
});
56+
57+
it("should be able to use require.js-style define, optional dependancies, not exist, with arrow function", function(done) {
58+
define("name", ["./optional"], (optional) => {
59+
should(optional.b).not.exist;
60+
done();
61+
});
62+
});
63+
64+
it("should be able to use require.js-style define, special string, with arrow function", function(done) {
65+
define(["require"], (require) => {
66+
require("./circular").should.be.eql(1);
67+
done();
68+
});
69+
});
70+
71+
it("should be able to use require.js-style define, without name, with arrow function", function(done) {
72+
true && define(["./circular"], (circular) => {
73+
circular.should.be.eql(1);
74+
done();
75+
});
76+
});
77+
78+
it("should be able to use require.js-style define, with empty dependencies, with arrow function", function(done) {
79+
define("name", [], () => {
80+
done();
81+
});
82+
});
83+
84+
it("should be able to use require.js-style define, without dependencies, with arrow function", function(done) {
85+
true && define("name", () => {
86+
done();
87+
});
88+
});
89+
90+
it("should offer AMD-style define for CommonJs with arrow function", function(done) {
91+
var _test_exports = exports;
92+
var _test_module = module;
93+
define((require, exports, module) => {
94+
(typeof require).should.be.eql("function");
95+
exports.should.be.equal(_test_exports);
96+
module.should.be.equal(_test_module);
97+
require("./circular").should.be.eql(1);
98+
done();
99+
});
100+
});
101+
102+
it("should pull in all dependencies of an AMD module with arrow function", function(done) {
103+
define((require) => {
104+
require("./amdmodule").should.be.eql("a");
105+
done();
106+
});
107+
});
108+
109+
it("should create a chunk for require.js require, with arrow function", function(done) {
110+
var sameTick = true;
111+
require(["./c"], (c) => {
112+
sameTick.should.be.eql(false);
113+
c.should.be.eql("c");
114+
require("./d").should.be.eql("d");
115+
done();
116+
});
117+
sameTick = false;
118+
});

0 commit comments

Comments
 (0)