forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HotModuleReplacementPlugin.test.js
254 lines (246 loc) · 6.6 KB
/
HotModuleReplacementPlugin.test.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"use strict";
const path = require("path");
const fs = require("graceful-fs");
const webpack = require("..");
describe("HotModuleReplacementPlugin", () => {
jest.setTimeout(20000);
it("should not have circular hashes but equal if unmodified", done => {
const entryFile = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"entry.js"
);
const statsFile1 = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"HotModuleReplacementPlugin.test.stats1.txt"
);
const statsFile2 = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"HotModuleReplacementPlugin.test.stats2.txt"
);
const recordsFile = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"records.json"
);
try {
fs.mkdirSync(path.join(__dirname, "js", "HotModuleReplacementPlugin"), {
recursive: true
});
} catch (e) {
// empty
}
try {
fs.unlinkSync(recordsFile);
} catch (e) {
// empty
}
const compiler = webpack({
cache: false,
entry: entryFile,
recordsPath: recordsFile,
output: {
path: path.join(__dirname, "js", "HotModuleReplacementPlugin")
},
plugins: [new webpack.HotModuleReplacementPlugin()],
optimization: {
moduleIds: "size",
chunkIds: "size"
}
});
fs.writeFileSync(entryFile, "1", "utf-8");
compiler.run((err, stats) => {
if (err) throw err;
const oldHash1 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
compiler.run((err, stats) => {
if (err) throw err;
const lastHash1 = stats.toJson().hash;
fs.writeFileSync(statsFile2, stats.toString());
expect(lastHash1).toBe(oldHash1); // hash shouldn't change when bundle stay equal
fs.writeFileSync(entryFile, "2", "utf-8");
compiler.run((err, stats) => {
if (err) throw err;
const lastHash2 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
expect(lastHash2).not.toBe(lastHash1); // hash should change when bundle changes
fs.writeFileSync(entryFile, "1", "utf-8");
compiler.run((err, stats) => {
if (err) throw err;
const currentHash1 = stats.toJson().hash;
fs.writeFileSync(statsFile2, stats.toString());
expect(currentHash1).not.toBe(lastHash1); // hash shouldn't change to the first hash if bundle changed back to first bundle
fs.writeFileSync(entryFile, "2", "utf-8");
compiler.run((err, stats) => {
if (err) throw err;
const currentHash2 = stats.toJson().hash;
fs.writeFileSync(statsFile1, stats.toString());
compiler.run((err, stats) => {
if (err) throw err;
expect(stats.toJson().hash).toBe(currentHash2);
expect(currentHash2).not.toBe(lastHash2);
expect(currentHash1).not.toBe(currentHash2);
expect(lastHash1).not.toBe(lastHash2);
done();
});
});
});
});
});
});
}, 120000);
it("should correct working when entry is Object and key is a number", done => {
const outputPath = path.join(__dirname, "js", "HotModuleReplacementPlugin");
const entryFile = path.join(outputPath, "entry.js");
const statsFile3 = path.join(
outputPath,
"HotModuleReplacementPlugin.test.stats3.txt"
);
const statsFile4 = path.join(
outputPath,
"HotModuleReplacementPlugin.test.stats4.txt"
);
const recordsFile = path.join(outputPath, "records.json");
try {
fs.mkdirSync(outputPath, { recursive: true });
} catch (e) {
// empty
}
try {
fs.unlinkSync(recordsFile);
} catch (e) {
// empty
}
const compiler = webpack({
mode: "development",
cache: false,
entry: {
"0": entryFile
},
recordsPath: recordsFile,
output: {
path: outputPath
},
plugins: [new webpack.HotModuleReplacementPlugin()],
optimization: {
chunkIds: "named"
}
});
fs.writeFileSync(entryFile, "1", "utf-8");
compiler.run((err, stats) => {
if (err) throw err;
const jsonStats = stats.toJson();
const hash = jsonStats.hash;
const chunkName = Object.keys(jsonStats.assetsByChunkName)[0];
fs.writeFileSync(statsFile3, stats.toString());
compiler.run((err, stats) => {
if (err) throw err;
fs.writeFileSync(statsFile4, stats.toString());
fs.writeFileSync(entryFile, "2", "utf-8");
compiler.run((err, stats) => {
if (err) throw err;
fs.writeFileSync(statsFile3, stats.toString());
const result = JSON.parse(
fs.readFileSync(
path.join(outputPath, `${hash}.hot-update.json`),
"utf-8"
)
)["c"];
expect(result).toEqual([chunkName]);
done();
});
});
});
});
it("should handle entryFile that contains path variable", done => {
const entryFile = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"[name]",
"entry.js"
);
const statsFile3 = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"HotModuleReplacementPlugin.test.stats3.txt"
);
const statsFile4 = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"HotModuleReplacementPlugin.test.stats4.txt"
);
const recordsFile = path.join(
__dirname,
"js",
"HotModuleReplacementPlugin",
"records.json"
);
try {
fs.mkdirSync(
path.join(__dirname, "js", "HotModuleReplacementPlugin", "[name]"),
{
recursive: true
}
);
} catch (e) {
// empty
}
try {
fs.unlinkSync(recordsFile);
} catch (e) {
// empty
}
const compiler = webpack({
mode: "development",
cache: false,
entry: {
"[name]/entry.js": entryFile
},
recordsPath: recordsFile,
output: {
filename: "[name]",
chunkFilename: "[name].js",
path: path.join(__dirname, "js", "HotModuleReplacementPlugin"),
hotUpdateChunkFilename: "static/webpack/[id].[hash].hot-update.js",
hotUpdateMainFilename: "static/webpack/[hash].hot-update.json"
},
plugins: [new webpack.HotModuleReplacementPlugin()],
optimization: {
chunkIds: "named"
}
});
fs.writeFileSync(entryFile, "1", "utf-8");
compiler.run((err, stats) => {
if (err) return done(err);
fs.writeFileSync(statsFile3, stats.toString());
compiler.run((err, stats) => {
if (err) return done(err);
fs.writeFileSync(statsFile4, stats.toString());
fs.writeFileSync(entryFile, "2", "utf-8");
compiler.run((err, stats) => {
if (err) return done(err);
fs.writeFileSync(statsFile3, stats.toString());
let foundUpdates = false;
Object.keys(stats.compilation.assets).forEach(key => {
foundUpdates =
foundUpdates ||
!!key.match(
/static\/webpack\/\[name\]\/entry\.js\..*?\.hot-update\.js/
);
});
expect(foundUpdates).toBe(true);
done();
});
});
});
});
});