Skip to content

Commit 66b04a9

Browse files
evilebottnawihiroppy
authored andcommittedApr 9, 2019
fix(regression): always get necessary stats for hmr (#1780)
1 parent 028ceee commit 66b04a9

File tree

3 files changed

+130
-83
lines changed

3 files changed

+130
-83
lines changed
 

‎lib/Server.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,8 @@ class Server {
9797

9898
updateCompiler(compiler, options);
9999

100-
this.stats =
101-
options.stats && Object.keys(options.stats).length
102-
? options.stats
103-
: Server.DEFAULT_STATS;
100+
this.originalStats =
101+
options.stats && Object.keys(options.stats).length ? options.stats : {};
104102

105103
this.hot = options.hot || options.hotOnly;
106104
this.headers = options.headers;
@@ -724,7 +722,13 @@ class Server {
724722
}
725723

726724
getStats(statsObj) {
727-
return statsObj.toJson(this.stats);
725+
const stats = Server.DEFAULT_STATS;
726+
727+
if (this.originalStats.warningsFilter) {
728+
stats.warningsFilter = this.originalStats.warningsFilter;
729+
}
730+
731+
return statsObj.toJson(stats);
728732
}
729733

730734
use() {

‎test/Server.test.js

+65-78
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ const Server = require('../lib/Server');
66
const config = require('./fixtures/simple-config/webpack.config');
77
const helper = require('./helper');
88

9-
const allStats = [
10-
{},
11-
// eslint-disable-next-line no-undefined
12-
undefined,
13-
false,
14-
'errors-only',
15-
{
16-
assets: false,
17-
},
18-
];
19-
209
describe('Server', () => {
2110
// issue: https://github.com/webpack/webpack-dev-server/issues/1724
2211
describe('express.static.mine.types', () => {
@@ -67,81 +56,79 @@ describe('Server', () => {
6756
});
6857
});
6958

70-
it('should cascade warningsFilter', () => {
71-
const stats = { warningsFilter: 'test' };
72-
return new Promise((res) => {
73-
const compiler = webpack(config);
74-
const server = new Server(compiler, { stats });
59+
describe('stats', () => {
60+
it(`should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors')`, () => {
61+
const allStats = [
62+
{},
63+
// eslint-disable-next-line no-undefined
64+
undefined,
65+
false,
66+
'errors-only',
67+
{
68+
assets: false,
69+
},
70+
];
71+
72+
return new Promise((resolve, reject) => {
73+
(function iterate(stats, i) {
74+
if (i === allStats.length) {
75+
return resolve();
76+
}
77+
78+
// Iterate to cover each case.
79+
Promise.resolve()
80+
.then(
81+
() =>
82+
new Promise((res) => {
83+
const compiler = webpack(config);
84+
const server = new Server(compiler, { stats });
85+
86+
compiler.hooks.done.tap('webpack-dev-server', (s) => {
87+
expect(Object.keys(server.getStats(s))).toMatchSnapshot();
88+
89+
server.close(() => {
90+
res();
91+
});
92+
});
93+
94+
compiler.run(() => {});
95+
server.listen(8080, 'localhost');
96+
})
97+
)
98+
.then(() => {
99+
i += 1;
100+
iterate(allStats[i], i);
101+
})
102+
.catch((e) => {
103+
reject(e);
104+
});
105+
})(allStats[0], 0);
106+
});
107+
});
75108

76-
compiler.hooks.done.tap('webpack-dev-server', (s) => {
77-
s.compilation.warnings = ['test', 'another warning'];
109+
it('should respect warningsFilter', () => {
110+
return new Promise((res) => {
111+
const compiler = webpack(config);
112+
const server = new Server(compiler, {
113+
stats: { warningsFilter: 'test' },
114+
});
78115

79-
const output = server.getStats(s);
80-
expect(output.warnings.length).toBe(1);
81-
expect(output.warnings[0]).toBe('another warning');
116+
compiler.hooks.done.tap('webpack-dev-server', (s) => {
117+
s.compilation.warnings = ['test', 'another warning'];
82118

83-
server.close(() => {
84-
res();
85-
});
86-
});
119+
const output = server.getStats(s);
87120

88-
compiler.run(() => {});
89-
server.listen(8080, 'localhost');
90-
});
91-
});
121+
expect(output.warnings.length).toBe(1);
122+
expect(output.warnings[0]).toBe('another warning');
92123

93-
it(`should cascade stats options`, () => {
94-
return new Promise((resolve, reject) => {
95-
(function iterate(stats, i) {
96-
if (i === allStats.length) {
97-
return resolve();
98-
}
99-
100-
const prom = new Promise((res, rej) => {
101-
const compiler = webpack(config);
102-
const server = new Server(compiler, { stats });
103-
104-
compiler.hooks.done.tap('webpack-dev-server', (s) => {
105-
const finalStats = JSON.stringify(server.getStats(s));
106-
const defaultStats = JSON.stringify(
107-
server._stats.toJson(Server.DEFAULT_STATS)
108-
);
109-
110-
// If we're not over-riding stats configuration,
111-
// we get the same result as the DEFAULT_STATS
112-
if (!stats || !Object.keys(stats).length) {
113-
try {
114-
expect(finalStats).toBe(defaultStats);
115-
} catch (e) {
116-
rej(e);
117-
}
118-
} else {
119-
try {
120-
expect(finalStats).not.toBe(defaultStats);
121-
} catch (e) {
122-
rej(e);
123-
}
124-
}
125-
126-
server.close(() => {
127-
res();
128-
});
124+
server.close(() => {
125+
res();
129126
});
130-
131-
compiler.run(() => {});
132-
server.listen(8080, 'localhost');
133127
});
134128

135-
// Iterate to cover each case.
136-
prom
137-
.then(() => {
138-
i += 1;
139-
iterate(allStats[i], i);
140-
})
141-
.catch((e) => {
142-
reject(e);
143-
});
144-
})(allStats[0], 0);
129+
compiler.run(() => {});
130+
server.listen(8080, 'localhost');
131+
});
145132
});
146133
});
147134

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 1`] = `
4+
Array [
5+
"errors",
6+
"warnings",
7+
"hash",
8+
"assetsByChunkName",
9+
"assets",
10+
"filteredAssets",
11+
]
12+
`;
13+
14+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 2`] = `
15+
Array [
16+
"errors",
17+
"warnings",
18+
"hash",
19+
"assetsByChunkName",
20+
"assets",
21+
"filteredAssets",
22+
]
23+
`;
24+
25+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 3`] = `
26+
Array [
27+
"errors",
28+
"warnings",
29+
"hash",
30+
"assetsByChunkName",
31+
"assets",
32+
"filteredAssets",
33+
]
34+
`;
35+
36+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 4`] = `
37+
Array [
38+
"errors",
39+
"warnings",
40+
"hash",
41+
"assetsByChunkName",
42+
"assets",
43+
"filteredAssets",
44+
]
45+
`;
46+
47+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 5`] = `
48+
Array [
49+
"errors",
50+
"warnings",
51+
"hash",
52+
"assetsByChunkName",
53+
"assets",
54+
"filteredAssets",
55+
]
56+
`;

0 commit comments

Comments
 (0)
Please sign in to comment.