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(Server): cascade stats options #1665

Merged
merged 7 commits into from
Mar 21, 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
32 changes: 21 additions & 11 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,18 @@ if (semver.satisfies(process.version, '8.6.0 - 9')) {
tls.DEFAULT_ECDH_CURVE = 'auto';
}

const STATS = {
all: false,
hash: true,
assets: true,
warnings: true,
errors: true,
errorDetails: false,
};

class Server {
static get DEFAULT_STATS() {
return {
all: false,
hash: true,
assets: true,
warnings: true,
errors: true,
errorDetails: false,
};
}

constructor(compiler, options = {}, _log) {
this.log = _log || createLogger(options);

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

this.stats =
options.stats && Object.keys(options.stats).length
? options.stats
: Server.DEFAULT_STATS;
this.hot = options.hot || options.hotOnly;
this.headers = options.headers;
this.progress = options.progress;
Expand Down Expand Up @@ -140,7 +146,7 @@ class Server {
compile.tap('webpack-dev-server', invalidPlugin);
invalid.tap('webpack-dev-server', invalidPlugin);
done.tap('webpack-dev-server', (stats) => {
this._sendStats(this.sockets, stats.toJson(STATS));
this._sendStats(this.sockets, this.getStats(stats));
this._stats = stats;
});
};
Expand Down Expand Up @@ -676,6 +682,10 @@ class Server {
}, this);
}

getStats(statsObj) {
return statsObj.toJson(this.stats);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move

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

logic here, we don't need new property like stats

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good point, will update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to keep the property this.stats to have access to options.stats.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it would be useful to move the logic since I still need to keep that this.stats so I can use options.stats in getStats().
What do you think @evilebottnawi ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you are right, we should rewrite this in next major release


use() {
// eslint-disable-next-line
this.app.use.apply(this.app, arguments);
Expand Down Expand Up @@ -848,7 +858,7 @@ class Server {
return;
}

this._sendStats([connection], this._stats.toJson(STATS), true);
this._sendStats([connection], this.getStats(this._stats), true);
});

socket.installHandlers(this.listeningApp, {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ function createConfig(config, argv, { port }) {
}

if (!options.stats) {
options.stats = {
options.stats = defaultTo(firstWpOpt.stats, {
cached: false,
cachedAssets: false,
};
});
}

if (
Expand Down
8 changes: 8 additions & 0 deletions test/CreateConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const createConfig = require('../lib/utils/createConfig');
const webpackConfig = require('./fixtures/schema/webpack.config.simple');
const webpackConfigNoStats = require('./fixtures/schema/webpack.config.no-dev-stats');

const argv = {
port: 8080,
Expand Down Expand Up @@ -853,4 +854,11 @@ describe('createConfig', () => {

expect(config).toMatchSnapshot();
});

it('use webpack stats', () => {
expect(
createConfig(webpackConfigNoStats, argv, { port: 8080 })
).toMatchSnapshot();
expect(webpackConfigNoStats).toMatchSnapshot();
});
});
73 changes: 73 additions & 0 deletions test/Server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

const webpack = require('webpack');
const Server = require('../lib/Server');
const config = require('./fixtures/simple-config/webpack.config');

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

describe('Server', () => {
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();
});
});

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);
});
});
});
71 changes: 71 additions & 0 deletions test/__snapshots__/CreateConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,77 @@ Object {
}
`;

exports[`createConfig use webpack stats 1`] = `
Object {
"clientLogLevel": "_clientLogLevel",
"compress": "_compress",
"contentBase": "_contentBase",
"disableHostCheck": "_disableHostCheck",
"filename": "_filename",
"historyApiFallback": "_historyApiFallback",
"host": "_foo",
"hot": "_hot",
"hotOnly": "_hotOnly",
"https": "_https",
"inline": "_inline",
"lazy": "_lazy",
"noInfo": true,
"open": "_open",
"openPage": "_openPage",
"pfxPassphrase": "_pfxPassphrase",
"port": "_port",
"progress": "_progress",
"public": "_public",
"publicPath": "_publicPath",
"quiet": "_quiet",
"socket": "_socket",
"stats": Object {
"assetsSort": "size",
},
"useLocalIp": "_useLocalIp",
"watchContentBase": "_watchContentBase",
}
`;

exports[`createConfig use webpack stats 2`] = `
Object {
"devServer": Object {
"clientLogLevel": "_clientLogLevel",
"compress": "_compress",
"contentBase": "_contentBase",
"disableHostCheck": "_disableHostCheck",
"filename": "_filename",
"historyApiFallback": "_historyApiFallback",
"host": "_foo",
"hot": "_hot",
"hotOnly": "_hotOnly",
"https": "_https",
"inline": "_inline",
"lazy": "_lazy",
"noInfo": true,
"open": "_open",
"openPage": "_openPage",
"pfxPassphrase": "_pfxPassphrase",
"port": "_port",
"progress": "_progress",
"public": "_public",
"publicPath": "_publicPath",
"quiet": "_quiet",
"socket": "_socket",
"stats": Object {
"assetsSort": "size",
},
"useLocalIp": "_useLocalIp",
"watchContentBase": "_watchContentBase",
},
"entry": "./app.js",
"mode": "development",
"stats": Object {
"assetsSort": "size",
},
}
`;

exports[`createConfig useLocalIp option (in devServer config) 1`] = `
Object {
"hot": true,
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/schema/webpack.config.no-dev-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

module.exports = {
entry: './app.js',
stats: {
assetsSort: 'size',
},
devServer: {
host: '_foo',
public: '_public',
socket: '_socket',
progress: '_progress',
publicPath: '_publicPath',
filename: '_filename',
hot: '_hot',
hotOnly: '_hotOnly',
clientLogLevel: '_clientLogLevel',
contentBase: '_contentBase',
watchContentBase: '_watchContentBase',
lazy: '_lazy',
noInfo: '_noInfo',
quiet: '_quiet',
https: '_https',
pfxPassphrase: '_pfxPassphrase',
inline: '_inline',
historyApiFallback: '_historyApiFallback',
compress: '_compress',
disableHostCheck: '_disableHostCheck',
open: '_open',
openPage: '_openPage',
useLocalIp: '_useLocalIp',
port: '_port',
},
};