From b7aaf64e71ec159a670b4a39d989114d86611b18 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Sun, 15 Aug 2021 22:14:38 +0300 Subject: [PATCH] fix: rebase --- lib/Server.js | 208 +++++++++++++++++--------------------------------- 1 file changed, 71 insertions(+), 137 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 6fb8f87396..f55762c0ed 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -742,6 +742,17 @@ class Server { this.app = new express(); } + getStats(statsObj) { + const stats = Server.DEFAULT_STATS; + const compilerOptions = this.getCompilerOptions(); + + if (compilerOptions.stats && compilerOptions.stats.warningsFilter) { + stats.warningsFilter = compilerOptions.stats.warningsFilter; + } + + return statsObj.toJson(stats); + } + setupHooks() { const addHooks = (compiler) => { compiler.hooks.invalid.tap("webpack-dev-server", () => { @@ -1455,143 +1466,6 @@ class Server { } } - listen(port, hostname, fn) { - this.logger = this.compiler.getInfrastructureLogger("webpack-dev-server"); - this.normalizeOptions(this.options); - - if (typeof port === "function") { - fn = port; - } - - if ( - typeof port !== "undefined" && - typeof this.options.port !== "undefined" && - port !== this.options.port - ) { - this.options.port = port; - - this.logger.warn( - 'The "port" specified in options is different from the port passed as an argument. Will be used from arguments.' - ); - } - - if (!this.options.port) { - this.options.port = port; - } - - if ( - typeof hostname !== "undefined" && - typeof this.options.host !== "undefined" && - hostname !== this.options.host - ) { - this.options.host = hostname; - - this.logger.warn( - 'The "host" specified in options is different from the host passed as an argument. Will be used from arguments.' - ); - } - - if (!this.options.host) { - this.options.host = hostname; - } - - this.options.host = Server.getHostname(this.options.host); - - const resolveFreePortOrIPC = () => { - if (this.options.ipc) { - return new Promise((resolve, reject) => { - const net = require("net"); - const socket = new net.Socket(); - - socket.on("error", (error) => { - if (error.code === "ECONNREFUSED") { - fs.unlinkSync(this.options.ipc); - - resolve(this.options.ipc); - - return; - } else if (error.code === "ENOENT") { - resolve(this.options.ipc); - - return; - } - - reject(error); - }); - - socket.connect({ path: this.options.ipc }, () => { - throw new Error(`IPC "${this.options.ipc}" is already used`); - }); - }); - } - - return Server.getFreePort(this.options.port).then((foundPort) => { - this.options.port = foundPort; - }); - }; - - return resolveFreePortOrIPC() - .then(() => { - this.initialize(); - - const listenOptions = this.options.ipc - ? { path: this.options.ipc } - : { - host: this.options.host, - port: this.options.port, - }; - - return this.server.listen(listenOptions, (error) => { - if (this.options.ipc) { - // chmod 666 (rw rw rw) - const READ_WRITE = 438; - - fs.chmodSync(this.options.ipc, READ_WRITE); - } - - if (this.options.webSocketServer) { - try { - this.createWebSocketServer(); - } catch (webSocketServerError) { - fn.call(this.server, webSocketServerError); - - return; - } - } - - if (this.options.bonjour) { - this.runBonjour(); - } - - this.logStatus(); - - if (fn) { - fn.call(this.server, error); - } - - if (typeof this.options.onListening === "function") { - this.options.onListening(this); - } - }); - }) - .catch((error) => { - if (fn) { - fn.call(this.server, error); - } - }); - } - - getStats(statsObj) { - const stats = Server.DEFAULT_STATS; - const compilerOptions = this.getCompilerOptions(); - - if (compilerOptions.stats && compilerOptions.stats.warningsFilter) { - stats.warningsFilter = compilerOptions.stats.warningsFilter; - } - - return statsObj.toJson(stats); - } - setHeaders(req, res, next) { let { headers } = this.options; @@ -1920,6 +1794,66 @@ class Server { this.stop().then(() => callback(null), callback); } + // TODO remove in the next major release + listen(port, hostname, fn) { + util.deprecate( + () => {}, + "'listen' is deprecated. Please use async 'start' or 'startCallback' methods.", + "DEP_WEBPACK_DEV_SERVER_LISTEN" + )(); + + this.logger = this.compiler.getInfrastructureLogger("webpack-dev-server"); + + if (typeof port === "function") { + fn = port; + } + + if ( + typeof port !== "undefined" && + typeof this.options.port !== "undefined" && + port !== this.options.port + ) { + this.options.port = port; + + this.logger.warn( + 'The "port" specified in options is different from the port passed as an argument. Will be used from arguments.' + ); + } + + if (!this.options.port) { + this.options.port = port; + } + + if ( + typeof hostname !== "undefined" && + typeof this.options.host !== "undefined" && + hostname !== this.options.host + ) { + this.options.host = hostname; + + this.logger.warn( + 'The "host" specified in options is different from the host passed as an argument. Will be used from arguments.' + ); + } + + if (!this.options.host) { + this.options.host = hostname; + } + + return this.start() + .then(() => { + if (fn) { + fn.call(this.server); + } + }) + .catch((error) => { + // Nothing + if (fn) { + fn.call(this.server, error); + } + }); + } + // TODO remove in the next major release close(callback) { util.deprecate(