Skip to content

Commit

Permalink
fix: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 16, 2021
1 parent dced920 commit b7aaf64
Showing 1 changed file with 71 additions and 137 deletions.
208 changes: 71 additions & 137 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b7aaf64

Please sign in to comment.