Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(regression): always get necessary stats for hmr #1780

Merged
merged 1 commit into from Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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",
]
`;