Skip to content

Migrate server on infrastructure logger #2614

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

Merged
merged 1 commit into from
May 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions bin/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ const options = {
group: DISPLAY_GROUP,
describe: 'Enables/Disables colors on the console',
},
info: {
type: 'boolean',
group: DISPLAY_GROUP,
default: true,
describe: 'Info',
},
quiet: {
type: 'boolean',
group: DISPLAY_GROUP,
describe: 'Quiet',
},
'client-log-level': {
type: 'string',
group: DISPLAY_GROUP,
Expand Down
9 changes: 3 additions & 6 deletions bin/webpack-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const Server = require('../lib/Server');
const setupExitSignals = require('../lib/utils/setupExitSignals');
const colors = require('../lib/utils/colors');
const processOptions = require('../lib/utils/processOptions');
const createLogger = require('../lib/utils/createLogger');
const getVersions = require('../lib/utils/getVersions');
const options = require('./options');

Expand Down Expand Up @@ -86,15 +85,13 @@ const config = require(convertArgvPath)(yargs, argv, {
});

function startDevServer(config, options) {
const log = createLogger(options);

let compiler;

try {
compiler = webpack(config);
} catch (err) {
if (err instanceof webpack.WebpackOptionsValidationError) {
log.error(colors.error(options.stats.colors, err.message));
console.error(colors.error(options.stats.colors, err.message));
Copy link
Member Author

Choose a reason for hiding this comment

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

let's use console

// eslint-disable-next-line no-process-exit
process.exit(1);
}
Expand All @@ -103,11 +100,11 @@ function startDevServer(config, options) {
}

try {
server = new Server(compiler, options, log);
server = new Server(compiler, options);
serverData.server = server;
} catch (err) {
if (err.name === 'ValidationError') {
log.error(colors.error(options.stats.colors, err.message));
console.error(colors.error(options.stats.colors, err.message));
Copy link
Member Author

@alexander-akait alexander-akait May 18, 2020

Choose a reason for hiding this comment

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

let's use console

// eslint-disable-next-line no-process-exit
process.exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = {
testURL: 'http://localhost/',
collectCoverage: false,
coveragePathIgnorePatterns: ['test'],
coveragePathIgnorePatterns: ['test', '<rootDir>/node_modules'],
moduleFileExtensions: ['js', 'json'],
testMatch: ['**/test/**/*.test.js'],
setupFilesAfterEnv: ['<rootDir>/setupTest.js'],
Expand Down
46 changes: 33 additions & 13 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const validateOptions = require('schema-utils');
const isAbsoluteUrl = require('is-absolute-url');
const normalizeOptions = require('./utils/normalizeOptions');
const updateCompiler = require('./utils/updateCompiler');
const createLogger = require('./utils/createLogger');
const getCertificate = require('./utils/getCertificate');
const status = require('./utils/status');
const createDomain = require('./utils/createDomain');
Expand All @@ -38,16 +37,16 @@ if (!process.env.WEBPACK_DEV_SERVER) {
}

class Server {
constructor(compiler, options = {}, _log) {
constructor(compiler, options = {}) {
validateOptions(schema, options, 'webpack Dev Server');

this.compiler = compiler;
this.options = options;
this.logger = this.compiler.getInfrastructureLogger('webpack-dev-server');
this.sockets = [];
this.contentBaseWatchers = [];
// Keep track of websocket proxies for external websocket upgrade.
this.websocketProxies = [];
this.log = _log || createLogger(options);
// this value of ws can be overwritten for tests
this.wsHeartbeatInterval = 30000;

Expand Down Expand Up @@ -149,7 +148,9 @@ class Server {
// middleware for serving webpack bundle
this.middleware = webpackDevMiddleware(
this.compiler,
Object.assign({}, this.options, { logLevel: this.log.options.level })
Object.assign({}, this.options, {
logger: this.logger,
})
);
}

Expand Down Expand Up @@ -189,7 +190,26 @@ class Server {
proxyOptions.context = correctedContext;
}

proxyOptions.logLevel = proxyOptions.logLevel || 'warn';
const getLogLevelForProxy = (level) => {
if (level === 'none') {
return 'silent';
}

if (level === 'log') {
return 'info';
}

if (level === 'verbose') {
return 'debug';
}

return level;
};

proxyOptions.logLevel = getLogLevelForProxy(
this.compiler.options.infrastructureLogging.level
);
proxyOptions.logProvider = () => this.logger;
Copy link
Member Author

Choose a reason for hiding this comment

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

Improve logging for proxy


return proxyOptions;
});
Expand Down Expand Up @@ -302,11 +322,11 @@ class Server {
this.app.use(publicPath, express.static(item));
});
} else if (isAbsoluteUrl(String(contentBase))) {
this.log.warn(
this.logger.warn(
'Using a URL as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.'
);

this.log.warn(
this.logger.warn(
'proxy: {\n\t"*": "<your current contentBase configuration>"\n}'
);

Expand All @@ -319,11 +339,11 @@ class Server {
res.end();
});
} else if (typeof contentBase === 'number') {
this.log.warn(
this.logger.warn(
'Using a number as contentBase is deprecated and will be removed in the next major version. Please use the proxy option instead.'
);

this.log.warn(
this.logger.warn(
'proxy: {\n\t"*": "//localhost:<your current contentBase configuration>"\n}'
);

Expand Down Expand Up @@ -548,7 +568,7 @@ class Server {
let fakeCert;

if (!this.options.https.key || !this.options.https.cert) {
fakeCert = getCertificate(this.log);
fakeCert = getCertificate(this.logger);
}

this.options.https.key = this.options.https.key || fakeCert;
Expand Down Expand Up @@ -577,7 +597,7 @@ class Server {
}

this.listeningApp.on('error', (err) => {
this.log.error(err);
this.logger.error(err);
});
}

Expand All @@ -590,7 +610,7 @@ class Server {
}

if (!headers) {
this.log.warn(
this.logger.warn(
'transportMode.server implementation must pass headers to the callback of onConnection(f) ' +
'via f(connection, headers) in order for clients to pass a headers security check'
);
Expand Down Expand Up @@ -650,7 +670,7 @@ class Server {
status(
uri,
this.options,
this.log,
this.logger,
this.options.stats && this.options.stats.colors
);
}
Expand Down
28 changes: 0 additions & 28 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,9 @@
"liveReload": {
"type": "boolean"
},
"log": {
"instanceof": "Function"
},
"logLevel": {
"enum": ["info", "warn", "error", "debug", "trace", "silent"]
},
"logTime": {
"type": "boolean"
},
"mimeTypes": {
"type": "object"
},
"noInfo": {
"type": "boolean"
},
"onAfterSetupMiddleware": {
"instanceof": "Function"
},
Expand Down Expand Up @@ -326,12 +314,6 @@
"publicPath": {
"type": "string"
},
"quiet": {
"type": "boolean"
},
"reporter": {
"instanceof": "Function"
},
"requestCert": {
"type": "boolean"
},
Expand Down Expand Up @@ -396,9 +378,6 @@
"useLocalIp": {
"type": "boolean"
},
"warn": {
"instanceof": "Function"
},
"watchContentBase": {
"type": "boolean"
},
Expand Down Expand Up @@ -436,11 +415,7 @@
"injectClient": "should be {Boolean|Function} (https://webpack.js.org/configuration/dev-server/#devserverinjectclient)",
"injectHot": "should be {Boolean|Function} (https://webpack.js.org/configuration/dev-server/#devserverinjecthot)",
"liveReload": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverlivereload-)",
"log": "should be {Function}",
"logLevel": "should be {String} and equal to one of the allowed values\n\n [ 'info', 'warn', 'error', 'debug', 'trace', 'silent' ]\n\n (https://github.com/webpack/webpack-dev-middleware#loglevel)",
"logTime": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#logtime)",
"mimeTypes": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devservermimetypes-)",
"noInfo": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devservernoinfo-)",
"onAfterSetupMiddleware": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserverafter)",
"onBeforeSetupMiddleware": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserverbefore)",
"onListening": "should be {Function} (https://webpack.js.org/configuration/dev-server/#onlistening)",
Expand All @@ -453,8 +428,6 @@
"proxy": "should be {Object|Array} (https://webpack.js.org/configuration/dev-server/#devserverproxy)",
"public": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserverpublic)",
"publicPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserverpublicpath-)",
"quiet": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverquiet-)",
"reporter": "should be {Function} (https://github.com/webpack/webpack-dev-middleware#reporter)",
"requestCert": "should be {Boolean}",
"contentBasePublicPath": "should be {String|Array} (https://webpack.js.org/configuration/dev-server/#devservercontentbasepublicpath)",
"serveIndex": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverserveindex)",
Expand All @@ -464,7 +437,6 @@
"stats": "should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserverstats-)",
"transportMode": "should be {String|Object} (https://webpack.js.org/configuration/dev-server/#devservertransportmode)",
"useLocalIp": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserveruselocalip)",
"warn": "should be {Function}",
"watchContentBase": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverwatchcontentbase)",
"watchOptions": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverwatchoptions-)",
"writeToDisk": "should be {Boolean|Function} (https://webpack.js.org/configuration/dev-server/#devserverwritetodisk-)"
Expand Down
9 changes: 6 additions & 3 deletions lib/servers/SockJSServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ module.exports = class SockJSServer extends BaseServer {
// options has: error (function), debug (function), server (http/s server), path (string)
constructor(server) {
super(server);

this.socket = sockjs.createServer({
// Use provided up-to-date sockjs-client
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
// Limit useless logs
// Default logger is very annoy. Limit useless logs.
log: (severity, line) => {
if (severity === 'error') {
this.server.log.error(line);
this.server.logger.error(line);
} else if (severity === 'info') {
this.server.logger.log(line);
} else {
this.server.log.debug(line);
this.server.logger.debug(line);
}
},
});
Expand Down
3 changes: 2 additions & 1 deletion lib/servers/WebsocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const BaseServer = require('./BaseServer');
module.exports = class WebsocketServer extends BaseServer {
constructor(server) {
super(server);

this.wsServer = new ws.Server({
noServer: true,
path: this.server.options.clientOptions.path,
Expand All @@ -25,7 +26,7 @@ module.exports = class WebsocketServer extends BaseServer {
});

this.wsServer.on('error', (err) => {
this.server.log.error(err.message);
this.server.logger.error(err.message);
});

const noop = () => {};
Expand Down
10 changes: 0 additions & 10 deletions lib/utils/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,6 @@ function createConfig(config, argv, { port }) {
options.stats = Object.assign({}, options.stats, { colors: argv.color });
}

// TODO remove in `v4`
if (!argv.info) {
options.noInfo = true;
}

// TODO remove in `v4`
if (argv.quiet) {
options.quiet = true;
}

if (argv.https) {
options.https = true;
}
Expand Down
23 changes: 0 additions & 23 deletions lib/utils/createLogger.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/utils/runOpen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const open = require('open');
const isAbsoluteUrl = require('is-absolute-url');

function runOpen(uri, options, log) {
function runOpen(uri, options, logger) {
// https://github.com/webpack/webpack-dev-server/issues/1990
let openOptions = { wait: false };
let openOptionValue = '';
Expand All @@ -26,7 +26,7 @@ function runOpen(uri, options, log) {
const pageUrl = page && isAbsoluteUrl(page) ? page : `${uri}${page}`;

return open(pageUrl, openOptions).catch(() => {
log.warn(
logger.warn(
`Unable to open "${pageUrl}" in browser${openOptionValue}. If you are running in a headless environment, please do not use the --open flag`
);
});
Expand Down
Loading