Skip to content

Commit

Permalink
fix(regression): always get necessary stats for hmr (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and hiroppy committed Apr 9, 2019
1 parent 028ceee commit 66b04a9
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 83 deletions.
14 changes: 9 additions & 5 deletions lib/Server.js
Expand Up @@ -97,10 +97,8 @@ class Server {

updateCompiler(compiler, options);

this.stats =
options.stats && Object.keys(options.stats).length
? options.stats
: Server.DEFAULT_STATS;
this.originalStats =
options.stats && Object.keys(options.stats).length ? options.stats : {};

this.hot = options.hot || options.hotOnly;
this.headers = options.headers;
Expand Down Expand Up @@ -724,7 +722,13 @@ class Server {
}

getStats(statsObj) {
return statsObj.toJson(this.stats);
const stats = Server.DEFAULT_STATS;

if (this.originalStats.warningsFilter) {
stats.warningsFilter = this.originalStats.warningsFilter;
}

return statsObj.toJson(stats);
}

use() {
Expand Down
143 changes: 65 additions & 78 deletions test/Server.test.js
Expand Up @@ -6,17 +6,6 @@ const Server = require('../lib/Server');
const config = require('./fixtures/simple-config/webpack.config');
const helper = require('./helper');

const allStats = [
{},
// eslint-disable-next-line no-undefined
undefined,
false,
'errors-only',
{
assets: false,
},
];

describe('Server', () => {
// issue: https://github.com/webpack/webpack-dev-server/issues/1724
describe('express.static.mine.types', () => {
Expand Down Expand Up @@ -67,81 +56,79 @@ describe('Server', () => {
});
});

it('should cascade warningsFilter', () => {
const stats = { warningsFilter: 'test' };
return new Promise((res) => {
const compiler = webpack(config);
const server = new Server(compiler, { stats });
describe('stats', () => {
it(`should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors')`, () => {
const allStats = [
{},
// eslint-disable-next-line no-undefined
undefined,
false,
'errors-only',
{
assets: false,
},
];

return new Promise((resolve, reject) => {
(function iterate(stats, i) {
if (i === allStats.length) {
return resolve();
}

// Iterate to cover each case.
Promise.resolve()
.then(
() =>
new Promise((res) => {
const compiler = webpack(config);
const server = new Server(compiler, { stats });

compiler.hooks.done.tap('webpack-dev-server', (s) => {
expect(Object.keys(server.getStats(s))).toMatchSnapshot();

server.close(() => {
res();
});
});

compiler.run(() => {});
server.listen(8080, 'localhost');
})
)
.then(() => {
i += 1;
iterate(allStats[i], i);
})
.catch((e) => {
reject(e);
});
})(allStats[0], 0);
});
});

compiler.hooks.done.tap('webpack-dev-server', (s) => {
s.compilation.warnings = ['test', 'another warning'];
it('should respect warningsFilter', () => {
return new Promise((res) => {
const compiler = webpack(config);
const server = new Server(compiler, {
stats: { warningsFilter: 'test' },
});

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

server.close(() => {
res();
});
});
const output = server.getStats(s);

compiler.run(() => {});
server.listen(8080, 'localhost');
});
});
expect(output.warnings.length).toBe(1);
expect(output.warnings[0]).toBe('another warning');

it(`should cascade stats options`, () => {
return new Promise((resolve, reject) => {
(function iterate(stats, i) {
if (i === allStats.length) {
return resolve();
}

const prom = new Promise((res, rej) => {
const compiler = webpack(config);
const server = new Server(compiler, { stats });

compiler.hooks.done.tap('webpack-dev-server', (s) => {
const finalStats = JSON.stringify(server.getStats(s));
const defaultStats = JSON.stringify(
server._stats.toJson(Server.DEFAULT_STATS)
);

// If we're not over-riding stats configuration,
// we get the same result as the DEFAULT_STATS
if (!stats || !Object.keys(stats).length) {
try {
expect(finalStats).toBe(defaultStats);
} catch (e) {
rej(e);
}
} else {
try {
expect(finalStats).not.toBe(defaultStats);
} catch (e) {
rej(e);
}
}

server.close(() => {
res();
});
server.close(() => {
res();
});

compiler.run(() => {});
server.listen(8080, 'localhost');
});

// Iterate to cover each case.
prom
.then(() => {
i += 1;
iterate(allStats[i], i);
})
.catch((e) => {
reject(e);
});
})(allStats[0], 0);
compiler.run(() => {});
server.listen(8080, 'localhost');
});
});
});

Expand Down
56 changes: 56 additions & 0 deletions test/__snapshots__/Server.test.js.snap
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 1`] = `
Array [
"errors",
"warnings",
"hash",
"assetsByChunkName",
"assets",
"filteredAssets",
]
`;

exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 2`] = `
Array [
"errors",
"warnings",
"hash",
"assetsByChunkName",
"assets",
"filteredAssets",
]
`;

exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 3`] = `
Array [
"errors",
"warnings",
"hash",
"assetsByChunkName",
"assets",
"filteredAssets",
]
`;

exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 4`] = `
Array [
"errors",
"warnings",
"hash",
"assetsByChunkName",
"assets",
"filteredAssets",
]
`;

exports[`Server stats should works with difference stats values (contains 'hash', 'assets', 'warnings' and 'errors') 5`] = `
Array [
"errors",
"warnings",
"hash",
"assetsByChunkName",
"assets",
"filteredAssets",
]
`;

0 comments on commit 66b04a9

Please sign in to comment.