Skip to content

Commit efaa740

Browse files
yoannmoinetevilebottnawi
authored andcommittedMar 21, 2019
fix: respect stats option from webpack config (#1665)
1 parent 7ea9ab9 commit efaa740

File tree

6 files changed

+209
-13
lines changed

6 files changed

+209
-13
lines changed
 

‎lib/Server.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,18 @@ if (semver.satisfies(process.version, '8.6.0 - 9')) {
6868
tls.DEFAULT_ECDH_CURVE = 'auto';
6969
}
7070

71-
const STATS = {
72-
all: false,
73-
hash: true,
74-
assets: true,
75-
warnings: true,
76-
errors: true,
77-
errorDetails: false,
78-
};
79-
8071
class Server {
72+
static get DEFAULT_STATS() {
73+
return {
74+
all: false,
75+
hash: true,
76+
assets: true,
77+
warnings: true,
78+
errors: true,
79+
errorDetails: false,
80+
};
81+
}
82+
8183
constructor(compiler, options = {}, _log) {
8284
this.log = _log || createLogger(options);
8385

@@ -87,6 +89,10 @@ class Server {
8789
throw new Error("'filename' option must be set in lazy mode.");
8890
}
8991

92+
this.stats =
93+
options.stats && Object.keys(options.stats).length
94+
? options.stats
95+
: Server.DEFAULT_STATS;
9096
this.hot = options.hot || options.hotOnly;
9197
this.headers = options.headers;
9298
this.progress = options.progress;
@@ -140,7 +146,7 @@ class Server {
140146
compile.tap('webpack-dev-server', invalidPlugin);
141147
invalid.tap('webpack-dev-server', invalidPlugin);
142148
done.tap('webpack-dev-server', (stats) => {
143-
this._sendStats(this.sockets, stats.toJson(STATS));
149+
this._sendStats(this.sockets, this.getStats(stats));
144150
this._stats = stats;
145151
});
146152
};
@@ -676,6 +682,10 @@ class Server {
676682
}, this);
677683
}
678684

685+
getStats(statsObj) {
686+
return statsObj.toJson(this.stats);
687+
}
688+
679689
use() {
680690
// eslint-disable-next-line
681691
this.app.use.apply(this.app, arguments);
@@ -848,7 +858,7 @@ class Server {
848858
return;
849859
}
850860

851-
this._sendStats([connection], this._stats.toJson(STATS), true);
861+
this._sendStats([connection], this.getStats(this._stats), true);
852862
});
853863

854864
socket.installHandlers(this.listeningApp, {

‎lib/utils/createConfig.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ function createConfig(config, argv, { port }) {
104104
}
105105

106106
if (!options.stats) {
107-
options.stats = {
107+
options.stats = defaultTo(firstWpOpt.stats, {
108108
cached: false,
109109
cachedAssets: false,
110-
};
110+
});
111111
}
112112

113113
if (

‎test/CreateConfig.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const path = require('path');
44
const createConfig = require('../lib/utils/createConfig');
55
const webpackConfig = require('./fixtures/schema/webpack.config.simple');
6+
const webpackConfigNoStats = require('./fixtures/schema/webpack.config.no-dev-stats');
67

78
const argv = {
89
port: 8080,
@@ -853,4 +854,11 @@ describe('createConfig', () => {
853854

854855
expect(config).toMatchSnapshot();
855856
});
857+
858+
it('use webpack stats', () => {
859+
expect(
860+
createConfig(webpackConfigNoStats, argv, { port: 8080 })
861+
).toMatchSnapshot();
862+
expect(webpackConfigNoStats).toMatchSnapshot();
863+
});
856864
});

‎test/Server.test.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
const webpack = require('webpack');
4+
const Server = require('../lib/Server');
5+
const config = require('./fixtures/simple-config/webpack.config');
6+
7+
const allStats = [
8+
{},
9+
// eslint-disable-next-line no-undefined
10+
undefined,
11+
false,
12+
'errors-only',
13+
{
14+
assets: false,
15+
},
16+
];
17+
18+
describe('Server', () => {
19+
it(`should cascade stats options`, () => {
20+
return new Promise((resolve, reject) => {
21+
(function iterate(stats, i) {
22+
if (i === allStats.length) {
23+
return resolve();
24+
}
25+
26+
const prom = new Promise((res, rej) => {
27+
const compiler = webpack(config);
28+
const server = new Server(compiler, { stats });
29+
30+
compiler.hooks.done.tap('webpack-dev-server', (s) => {
31+
const finalStats = JSON.stringify(server.getStats(s));
32+
const defaultStats = JSON.stringify(
33+
server._stats.toJson(Server.DEFAULT_STATS)
34+
);
35+
36+
// If we're not over-riding stats configuration,
37+
// we get the same result as the DEFAULT_STATS
38+
if (!stats || !Object.keys(stats).length) {
39+
try {
40+
expect(finalStats).toBe(defaultStats);
41+
} catch (e) {
42+
rej(e);
43+
}
44+
} else {
45+
try {
46+
expect(finalStats).not.toBe(defaultStats);
47+
} catch (e) {
48+
rej(e);
49+
}
50+
}
51+
52+
server.close(() => {
53+
res();
54+
});
55+
});
56+
57+
compiler.run(() => {});
58+
server.listen(8080, 'localhost');
59+
});
60+
61+
// Iterate to cover each case.
62+
prom
63+
.then(() => {
64+
i += 1;
65+
iterate(allStats[i], i);
66+
})
67+
.catch((e) => {
68+
reject(e);
69+
});
70+
})(allStats[0], 0);
71+
});
72+
});
73+
});

‎test/__snapshots__/CreateConfig.test.js.snap

+71
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,77 @@ Object {
10371037
}
10381038
`;
10391039

1040+
exports[`createConfig use webpack stats 1`] = `
1041+
Object {
1042+
"clientLogLevel": "_clientLogLevel",
1043+
"compress": "_compress",
1044+
"contentBase": "_contentBase",
1045+
"disableHostCheck": "_disableHostCheck",
1046+
"filename": "_filename",
1047+
"historyApiFallback": "_historyApiFallback",
1048+
"host": "_foo",
1049+
"hot": "_hot",
1050+
"hotOnly": "_hotOnly",
1051+
"https": "_https",
1052+
"inline": "_inline",
1053+
"lazy": "_lazy",
1054+
"noInfo": true,
1055+
"open": "_open",
1056+
"openPage": "_openPage",
1057+
"pfxPassphrase": "_pfxPassphrase",
1058+
"port": "_port",
1059+
"progress": "_progress",
1060+
"public": "_public",
1061+
"publicPath": "_publicPath",
1062+
"quiet": "_quiet",
1063+
"socket": "_socket",
1064+
"stats": Object {
1065+
"assetsSort": "size",
1066+
},
1067+
"useLocalIp": "_useLocalIp",
1068+
"watchContentBase": "_watchContentBase",
1069+
}
1070+
`;
1071+
1072+
exports[`createConfig use webpack stats 2`] = `
1073+
Object {
1074+
"devServer": Object {
1075+
"clientLogLevel": "_clientLogLevel",
1076+
"compress": "_compress",
1077+
"contentBase": "_contentBase",
1078+
"disableHostCheck": "_disableHostCheck",
1079+
"filename": "_filename",
1080+
"historyApiFallback": "_historyApiFallback",
1081+
"host": "_foo",
1082+
"hot": "_hot",
1083+
"hotOnly": "_hotOnly",
1084+
"https": "_https",
1085+
"inline": "_inline",
1086+
"lazy": "_lazy",
1087+
"noInfo": true,
1088+
"open": "_open",
1089+
"openPage": "_openPage",
1090+
"pfxPassphrase": "_pfxPassphrase",
1091+
"port": "_port",
1092+
"progress": "_progress",
1093+
"public": "_public",
1094+
"publicPath": "_publicPath",
1095+
"quiet": "_quiet",
1096+
"socket": "_socket",
1097+
"stats": Object {
1098+
"assetsSort": "size",
1099+
},
1100+
"useLocalIp": "_useLocalIp",
1101+
"watchContentBase": "_watchContentBase",
1102+
},
1103+
"entry": "./app.js",
1104+
"mode": "development",
1105+
"stats": Object {
1106+
"assetsSort": "size",
1107+
},
1108+
}
1109+
`;
1110+
10401111
exports[`createConfig useLocalIp option (in devServer config) 1`] = `
10411112
Object {
10421113
"hot": true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
module.exports = {
4+
entry: './app.js',
5+
stats: {
6+
assetsSort: 'size',
7+
},
8+
devServer: {
9+
host: '_foo',
10+
public: '_public',
11+
socket: '_socket',
12+
progress: '_progress',
13+
publicPath: '_publicPath',
14+
filename: '_filename',
15+
hot: '_hot',
16+
hotOnly: '_hotOnly',
17+
clientLogLevel: '_clientLogLevel',
18+
contentBase: '_contentBase',
19+
watchContentBase: '_watchContentBase',
20+
lazy: '_lazy',
21+
noInfo: '_noInfo',
22+
quiet: '_quiet',
23+
https: '_https',
24+
pfxPassphrase: '_pfxPassphrase',
25+
inline: '_inline',
26+
historyApiFallback: '_historyApiFallback',
27+
compress: '_compress',
28+
disableHostCheck: '_disableHostCheck',
29+
open: '_open',
30+
openPage: '_openPage',
31+
useLocalIp: '_useLocalIp',
32+
port: '_port',
33+
},
34+
};

0 commit comments

Comments
 (0)
Please sign in to comment.