forked from webpack/webpack
/
index.js
237 lines (232 loc) · 4.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
let warnings = [];
let oldWarn;
beforeEach(done => {
oldWarn = console.warn;
console.warn = m => warnings.push(m);
done();
});
afterEach(done => {
expectWarning();
console.warn = oldWarn;
done();
});
const expectWarning = regexp => {
if (!regexp) {
expect(warnings).toEqual([]);
} else {
expect(warnings).toEqual(
expect.objectContaining({
0: expect.stringMatching(regexp),
length: 1
})
);
}
warnings.length = 0;
};
it("should load the shared modules", async () => {
__webpack_share_scopes__["test-scope"] = {
package: {
"0": {
get: () => () => "shared package"
}
},
"@scoped/package": {
"0": {
get: () => Promise.resolve(() => "shared @scoped/package")
}
},
"prefix/a": {
"0": {
get: () => () => "shared prefix/a"
}
},
"prefix/deep/c": {
"0": {
get: () => () => "shared prefix/deep/c"
}
},
"./relative1": {
"0": {
get: () => () => "shared relative1"
}
}
};
__webpack_share_scopes__["other-scope"] = {
"advanced/123": {
"1.2.beta.1": {
get: () => () => "123"
}
},
"advanced/error1": {
"1.2.3": {
get: () => {
throw new Error("error1");
}
}
},
"advanced/error2": {
"1.2.3": {
get: () =>
Promise.resolve().then(() => {
throw new Error("error2");
})
}
},
"advanced/error3": {
"1.2.3": {
get: () =>
Promise.resolve().then(() => () => {
throw new Error("error3");
})
}
},
"advanced/error4": {
"1.0.0": {
get: () => () => "wrong"
}
}
};
{
const result = await import("package");
expect(result.default).toBe("shared package");
}
{
const result = await import("@scoped/package");
expect(result.default).toBe("shared @scoped/package");
}
{
const result = await import("prefix/a");
expect(result.default).toBe("shared prefix/a");
}
{
const result = await import("prefix/deep/b");
expect(result.default).toBe("b");
}
{
const result = await import("prefix/deep/c");
expect(result.default).toBe("shared prefix/deep/c");
}
{
const result = await import("./relative1");
expect(result.default).toBe("shared relative1");
}
{
const result = await import("./relative2");
expect(result.default).toBe("relative2");
}
{
const result = await import("advanced/123");
expect(result.default).toBe("123");
}
{
await expect(() => import("advanced/error0")).rejects.toEqual(
expect.objectContaining({
message: expect.stringContaining("advanced/error0")
})
);
}
{
await expect(() => import("advanced/error1")).rejects.toEqual(
expect.objectContaining({
message: expect.stringContaining("error1")
})
);
}
{
await expect(() => import("advanced/error2")).rejects.toEqual(
expect.objectContaining({
message: expect.stringContaining("error2")
})
);
}
{
await expect(() => import("advanced/error3")).rejects.toEqual(
expect.objectContaining({
message: expect.stringContaining("error3")
})
);
}
{
await expect(() => import("advanced/error4")).rejects.toEqual(
expect.objectContaining({
message: expect.stringContaining("1.2.3")
})
);
}
});
it("should handle version matching correctly in strict and singleton mode", async () => {
__webpack_share_scopes__["default"] = {
strict0: {
"1.1.1": {
get: () => () => "shared strict0"
}
},
strict1: {
"1.1.1": {
get: () => () => "shared strict1"
}
},
strict2: {
"1.1.1": {
get: () => () => "shared strict2"
}
},
strict3: {
"1.1.1": {
get: () => () => "shared strict3"
}
},
strict4: {
"1.1.1": {
get: () => () => "shared strict4"
}
},
strict5: {
"1.1.1": {
get: () => () => "shared strict5"
}
},
singleton: {
"1.1.1": {
get: () => () => "shared singleton",
from: 'container-a'
}
}
};
{
const result = await import("strict0");
expect(result.default).toBe("shared strict0");
expectWarning();
}
{
const result = await import("strict1");
expect(result.default).toBe("strict");
}
{
const result = await import("strict2");
expect(result.default).toBe("strict");
}
{
const result = await import("strict3");
expect(result.default).toBe("strict");
}
{
const result = await import("strict4");
expect(result.default).toBe("strict");
}
{
await expect(() => import("strict5")).rejects.toEqual(
expect.objectContaining({
message: expect.stringContaining("strict5")
})
);
expectWarning();
}
{
const result = await import("singleton");
expect(result.default).toBe("shared singleton");
expectWarning(
/Unsatisfied version 1\.1\.1 from container-a of shared singleton module singleton \(required =1\.1\.0\)/
);
}
});