Skip to content

Commit

Permalink
fix: postpone initialize (#3467)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 23, 2021
1 parent 9f237a0 commit 80087de
Show file tree
Hide file tree
Showing 43 changed files with 4,358 additions and 1,853 deletions.
18 changes: 13 additions & 5 deletions lib/Server.js
Expand Up @@ -45,7 +45,9 @@ class Server {
this.logger,
Server.findCacheDir()
);
}

initialize() {
this.applyDevServerPlugin();

this.webSocketServerImplementation = getSocketServerImplementation(
Expand Down Expand Up @@ -924,6 +926,8 @@ class Server {
.then((foundPort) => {
this.options.port = foundPort;

this.initialize();

return this.server.listen(
this.options.port,
this.options.host,
Expand Down Expand Up @@ -966,12 +970,16 @@ class Server {
);
this.staticWatchers = [];

this.server.kill(() => {
// watchers must be closed before closing middleware
prom.then(() => {
this.middleware.close(callback);
if (this.server) {
this.server.kill(() => {
// watchers must be closed before closing middleware
prom.then(() => {
this.middleware.close(callback);
});
});
});
} else if (callback) {
callback();
}
}

getStats(statsObj) {
Expand Down
2 changes: 0 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack4
Expand Up @@ -635,8 +635,6 @@ exports[`options validate should throw an error on the "webSocketServer" option
object { type?, options? }"
`;

exports[`options validate should throw an error on the "webSocketServer" option with 'nonexistent-implementation' value 1`] = `"Error: When you use custom web socket implementation you must explicitly specify client.transport. client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class "`;

exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.webSocketServer should be one of these:
Expand Down
2 changes: 0 additions & 2 deletions test/__snapshots__/validate-options.test.js.snap.webpack5
Expand Up @@ -635,8 +635,6 @@ exports[`options validate should throw an error on the "webSocketServer" option
object { type?, options? }"
`;

exports[`options validate should throw an error on the "webSocketServer" option with 'nonexistent-implementation' value 1`] = `"Error: When you use custom web socket implementation you must explicitly specify client.transport. client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class "`;

exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.webSocketServer should be one of these:
Expand Down
20 changes: 0 additions & 20 deletions test/cli/client-option.test.js
Expand Up @@ -172,24 +172,4 @@ describe('"client" CLI option', () => {

expect(exitCode).toEqual(0);
});

it('should add "/ws" web socket path by default', async () => {
const { exitCode, stdout } = await testBin(
null,
'./test/fixtures/dev-server/client-default-path-config.js'
);

expect(exitCode).toEqual(0);
expect(stdout).toContain('ws%3A%2F%2F0.0.0.0%2Fws');
});

it('should use "client.webSocketURL.pathname" from configuration', async () => {
const { exitCode, stdout } = await testBin(
null,
'./test/fixtures/dev-server/client-custom-path-config.js'
);

expect(exitCode).toEqual(0);
expect(stdout).toContain('ws%3A%2F%2F0.0.0.0%2Fcustom%2Fpath');
});
});
37 changes: 29 additions & 8 deletions test/client/bundle.test.js
@@ -1,8 +1,9 @@
'use strict';

const webpack = require('webpack');
const acorn = require('acorn');
const request = require('supertest');
const testServer = require('../helpers/test-server');
const Server = require('../../lib/Server');
const config = require('../fixtures/simple-config/webpack.config');
const port = require('../ports-map').bundle;
const isWebpack5 = require('../helpers/isWebpack5');
Expand All @@ -12,16 +13,36 @@ describe('bundle', () => {
let server;
let req;

beforeAll((done) => {
server = testServer.start(
{ ...config, target: isWebpack5 ? ['es5', 'web'] : 'web' },
{ port },
done
);
beforeAll(async () => {
const compiler = webpack({
...config,
target: isWebpack5 ? ['es5', 'web'] : 'web',
});

server = new Server({ port }, compiler);

await new Promise((resolve, reject) => {
server.listen(port, '127.0.0.1', (error) => {
if (error) {
reject(error);

return;
}

resolve();
});
});

req = request(server.app);
});

afterAll(testServer.close);
afterAll(async () => {
await new Promise((resolve) => {
server.close(() => {
resolve();
});
});
});

it('should get full user bundle and parse with ES5', async () => {
const { text } = await req
Expand Down
55 changes: 55 additions & 0 deletions test/e2e/__snapshots__/stats.test.js.snap.webpack4
@@ -0,0 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`stats should work using "{ assets: false }" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "{ warningsFilter: 'test' }" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "{}" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "errors-only" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "false" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "undefined" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;
64 changes: 64 additions & 0 deletions test/e2e/__snapshots__/stats.test.js.snap.webpack5
@@ -0,0 +1,64 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`stats should work and respect the "ignoreWarnings" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "{ assets: false }" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "{ warningsFilter: 'test' }" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "{}" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "errors-only" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "false" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`stats should work using "undefined" value for the "stats" option 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;
66 changes: 66 additions & 0 deletions test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4
Expand Up @@ -107,6 +107,72 @@ Array [

exports[`web socket server URL should work behind proxy, when hostnames are same and ports are different ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work when "host" option is "local-ip" ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work when "host" option is "local-ip" ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket server URL should work when "host" option is "local-ip" ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work when "host" option is "local-ip" ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work when "host" option is "local-ipv4" ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work when "host" option is "local-ipv4" ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket server URL should work when "host" option is "local-ipv4" ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work when "host" option is "local-ipv4" ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work when "host" option is IPv4 ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down

0 comments on commit 80087de

Please sign in to comment.