Skip to content

Commit 28f2ffe

Browse files
committed
Add test for all amd arrow function cases & fix errors
Wrap interpolated functions in parenthesis so they work for both regular functions and arrow functions. Add amd arrow function tests and prevent from running through uglifyjs.
1 parent 007b14e commit 28f2ffe

File tree

16 files changed

+150
-21
lines changed

16 files changed

+150
-21
lines changed

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/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+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = 2;
2+
try { module.exports.a = require("./a"); } catch (e) {};
3+
try { module.exports.b = require("./b"); } catch (e) {};

0 commit comments

Comments
 (0)