Skip to content

Commit 0b4dd62

Browse files
fix(regression): always get necessary stats for hmr
1 parent 028ceee commit 0b4dd62

File tree

3 files changed

+87
-83
lines changed

3 files changed

+87
-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

+67-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,81 @@ 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(
88+
JSON.stringify(server.getStats(s))
89+
).toMatchSnapshot();
90+
91+
server.close(() => {
92+
res();
93+
});
94+
});
95+
96+
compiler.run(() => {});
97+
server.listen(8080, 'localhost');
98+
})
99+
)
100+
.then(() => {
101+
i += 1;
102+
iterate(allStats[i], i);
103+
})
104+
.catch((e) => {
105+
reject(e);
106+
});
107+
})(allStats[0], 0);
108+
});
109+
});
75110

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

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

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

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

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-
});
126+
server.close(() => {
127+
res();
129128
});
130-
131-
compiler.run(() => {});
132-
server.listen(8080, 'localhost');
133129
});
134130

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);
131+
compiler.run(() => {});
132+
server.listen(8080, 'localhost');
133+
});
145134
});
146135
});
147136

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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`] = `"{\\"errors\\":[],\\"warnings\\":[],\\"hash\\":\\"92ee2d6538f048c6e86b\\",\\"assetsByChunkName\\":{\\"main\\":\\"main.js\\"},\\"assets\\":[{\\"name\\":\\"main.js\\",\\"size\\":357884,\\"chunks\\":[\\"main\\"],\\"chunkNames\\":[\\"main\\"],\\"emitted\\":true}],\\"filteredAssets\\":0}"`;
4+
5+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 2`] = `"{\\"errors\\":[],\\"warnings\\":[],\\"hash\\":\\"92ee2d6538f048c6e86b\\",\\"assetsByChunkName\\":{\\"main\\":\\"main.js\\"},\\"assets\\":[{\\"name\\":\\"main.js\\",\\"size\\":357884,\\"chunks\\":[\\"main\\"],\\"chunkNames\\":[\\"main\\"],\\"emitted\\":true}],\\"filteredAssets\\":0}"`;
6+
7+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 3`] = `"{\\"errors\\":[],\\"warnings\\":[],\\"hash\\":\\"92ee2d6538f048c6e86b\\",\\"assetsByChunkName\\":{\\"main\\":\\"main.js\\"},\\"assets\\":[{\\"name\\":\\"main.js\\",\\"size\\":357884,\\"chunks\\":[\\"main\\"],\\"chunkNames\\":[\\"main\\"],\\"emitted\\":true}],\\"filteredAssets\\":0}"`;
8+
9+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 4`] = `"{\\"errors\\":[],\\"warnings\\":[],\\"hash\\":\\"92ee2d6538f048c6e86b\\",\\"assetsByChunkName\\":{\\"main\\":\\"main.js\\"},\\"assets\\":[{\\"name\\":\\"main.js\\",\\"size\\":357884,\\"chunks\\":[\\"main\\"],\\"chunkNames\\":[\\"main\\"],\\"emitted\\":true}],\\"filteredAssets\\":0}"`;
10+
11+
exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 5`] = `"{\\"errors\\":[],\\"warnings\\":[],\\"hash\\":\\"92ee2d6538f048c6e86b\\",\\"assetsByChunkName\\":{\\"main\\":\\"main.js\\"},\\"assets\\":[{\\"name\\":\\"main.js\\",\\"size\\":357884,\\"chunks\\":[\\"main\\"],\\"chunkNames\\":[\\"main\\"],\\"emitted\\":true}],\\"filteredAssets\\":0}"`;

0 commit comments

Comments
 (0)