Skip to content

Commit 80087de

Browse files
fix: postpone initialize (#3467)
1 parent 9f237a0 commit 80087de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4358
-1853
lines changed

lib/Server.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class Server {
4545
this.logger,
4646
Server.findCacheDir()
4747
);
48+
}
4849

50+
initialize() {
4951
this.applyDevServerPlugin();
5052

5153
this.webSocketServerImplementation = getSocketServerImplementation(
@@ -924,6 +926,8 @@ class Server {
924926
.then((foundPort) => {
925927
this.options.port = foundPort;
926928

929+
this.initialize();
930+
927931
return this.server.listen(
928932
this.options.port,
929933
this.options.host,
@@ -966,12 +970,16 @@ class Server {
966970
);
967971
this.staticWatchers = [];
968972

969-
this.server.kill(() => {
970-
// watchers must be closed before closing middleware
971-
prom.then(() => {
972-
this.middleware.close(callback);
973+
if (this.server) {
974+
this.server.kill(() => {
975+
// watchers must be closed before closing middleware
976+
prom.then(() => {
977+
this.middleware.close(callback);
978+
});
973979
});
974-
});
980+
} else if (callback) {
981+
callback();
982+
}
975983
}
976984

977985
getStats(statsObj) {

test/__snapshots__/validate-options.test.js.snap.webpack4

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,6 @@ exports[`options validate should throw an error on the "webSocketServer" option
635635
object { type?, options? }"
636636
`;
637637

638-
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 "`;
639-
640638
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
641639
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
642640
- options.webSocketServer should be one of these:

test/__snapshots__/validate-options.test.js.snap.webpack5

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,6 @@ exports[`options validate should throw an error on the "webSocketServer" option
635635
object { type?, options? }"
636636
`;
637637

638-
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 "`;
639-
640638
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
641639
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
642640
- options.webSocketServer should be one of these:

test/cli/client-option.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,4 @@ describe('"client" CLI option', () => {
172172

173173
expect(exitCode).toEqual(0);
174174
});
175-
176-
it('should add "/ws" web socket path by default', async () => {
177-
const { exitCode, stdout } = await testBin(
178-
null,
179-
'./test/fixtures/dev-server/client-default-path-config.js'
180-
);
181-
182-
expect(exitCode).toEqual(0);
183-
expect(stdout).toContain('ws%3A%2F%2F0.0.0.0%2Fws');
184-
});
185-
186-
it('should use "client.webSocketURL.pathname" from configuration', async () => {
187-
const { exitCode, stdout } = await testBin(
188-
null,
189-
'./test/fixtures/dev-server/client-custom-path-config.js'
190-
);
191-
192-
expect(exitCode).toEqual(0);
193-
expect(stdout).toContain('ws%3A%2F%2F0.0.0.0%2Fcustom%2Fpath');
194-
});
195175
});

test/client/bundle.test.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3+
const webpack = require('webpack');
34
const acorn = require('acorn');
45
const request = require('supertest');
5-
const testServer = require('../helpers/test-server');
6+
const Server = require('../../lib/Server');
67
const config = require('../fixtures/simple-config/webpack.config');
78
const port = require('../ports-map').bundle;
89
const isWebpack5 = require('../helpers/isWebpack5');
@@ -12,16 +13,36 @@ describe('bundle', () => {
1213
let server;
1314
let req;
1415

15-
beforeAll((done) => {
16-
server = testServer.start(
17-
{ ...config, target: isWebpack5 ? ['es5', 'web'] : 'web' },
18-
{ port },
19-
done
20-
);
16+
beforeAll(async () => {
17+
const compiler = webpack({
18+
...config,
19+
target: isWebpack5 ? ['es5', 'web'] : 'web',
20+
});
21+
22+
server = new Server({ port }, compiler);
23+
24+
await new Promise((resolve, reject) => {
25+
server.listen(port, '127.0.0.1', (error) => {
26+
if (error) {
27+
reject(error);
28+
29+
return;
30+
}
31+
32+
resolve();
33+
});
34+
});
35+
2136
req = request(server.app);
2237
});
2338

24-
afterAll(testServer.close);
39+
afterAll(async () => {
40+
await new Promise((resolve) => {
41+
server.close(() => {
42+
resolve();
43+
});
44+
});
45+
});
2546

2647
it('should get full user bundle and parse with ES5', async () => {
2748
const { text } = await req
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`stats should work using "{ assets: false }" value for the "stats" option 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"Hey.",
7+
"[webpack-dev-server] Hot Module Replacement enabled.",
8+
"[webpack-dev-server] Live Reloading enabled.",
9+
]
10+
`;
11+
12+
exports[`stats should work using "{ warningsFilter: 'test' }" value for the "stats" option 1`] = `
13+
Array [
14+
"[HMR] Waiting for update signal from WDS...",
15+
"Hey.",
16+
"[webpack-dev-server] Hot Module Replacement enabled.",
17+
"[webpack-dev-server] Live Reloading enabled.",
18+
]
19+
`;
20+
21+
exports[`stats should work using "{}" value for the "stats" option 1`] = `
22+
Array [
23+
"[HMR] Waiting for update signal from WDS...",
24+
"Hey.",
25+
"[webpack-dev-server] Hot Module Replacement enabled.",
26+
"[webpack-dev-server] Live Reloading enabled.",
27+
]
28+
`;
29+
30+
exports[`stats should work using "errors-only" value for the "stats" option 1`] = `
31+
Array [
32+
"[HMR] Waiting for update signal from WDS...",
33+
"Hey.",
34+
"[webpack-dev-server] Hot Module Replacement enabled.",
35+
"[webpack-dev-server] Live Reloading enabled.",
36+
]
37+
`;
38+
39+
exports[`stats should work using "false" value for the "stats" option 1`] = `
40+
Array [
41+
"[HMR] Waiting for update signal from WDS...",
42+
"Hey.",
43+
"[webpack-dev-server] Hot Module Replacement enabled.",
44+
"[webpack-dev-server] Live Reloading enabled.",
45+
]
46+
`;
47+
48+
exports[`stats should work using "undefined" value for the "stats" option 1`] = `
49+
Array [
50+
"[HMR] Waiting for update signal from WDS...",
51+
"Hey.",
52+
"[webpack-dev-server] Hot Module Replacement enabled.",
53+
"[webpack-dev-server] Live Reloading enabled.",
54+
]
55+
`;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`stats should work and respect the "ignoreWarnings" option 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"Hey.",
7+
"[webpack-dev-server] Hot Module Replacement enabled.",
8+
"[webpack-dev-server] Live Reloading enabled.",
9+
]
10+
`;
11+
12+
exports[`stats should work using "{ assets: false }" value for the "stats" option 1`] = `
13+
Array [
14+
"[HMR] Waiting for update signal from WDS...",
15+
"Hey.",
16+
"[webpack-dev-server] Hot Module Replacement enabled.",
17+
"[webpack-dev-server] Live Reloading enabled.",
18+
]
19+
`;
20+
21+
exports[`stats should work using "{ warningsFilter: 'test' }" value for the "stats" option 1`] = `
22+
Array [
23+
"[HMR] Waiting for update signal from WDS...",
24+
"Hey.",
25+
"[webpack-dev-server] Hot Module Replacement enabled.",
26+
"[webpack-dev-server] Live Reloading enabled.",
27+
]
28+
`;
29+
30+
exports[`stats should work using "{}" value for the "stats" option 1`] = `
31+
Array [
32+
"[HMR] Waiting for update signal from WDS...",
33+
"Hey.",
34+
"[webpack-dev-server] Hot Module Replacement enabled.",
35+
"[webpack-dev-server] Live Reloading enabled.",
36+
]
37+
`;
38+
39+
exports[`stats should work using "errors-only" value for the "stats" option 1`] = `
40+
Array [
41+
"[HMR] Waiting for update signal from WDS...",
42+
"Hey.",
43+
"[webpack-dev-server] Hot Module Replacement enabled.",
44+
"[webpack-dev-server] Live Reloading enabled.",
45+
]
46+
`;
47+
48+
exports[`stats should work using "false" value for the "stats" option 1`] = `
49+
Array [
50+
"[HMR] Waiting for update signal from WDS...",
51+
"Hey.",
52+
"[webpack-dev-server] Hot Module Replacement enabled.",
53+
"[webpack-dev-server] Live Reloading enabled.",
54+
]
55+
`;
56+
57+
exports[`stats should work using "undefined" value for the "stats" option 1`] = `
58+
Array [
59+
"[HMR] Waiting for update signal from WDS...",
60+
"Hey.",
61+
"[webpack-dev-server] Hot Module Replacement enabled.",
62+
"[webpack-dev-server] Live Reloading enabled.",
63+
]
64+
`;

test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,72 @@ Array [
107107

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

110+
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`] = `
111+
Array [
112+
"[HMR] Waiting for update signal from WDS...",
113+
"Hey.",
114+
"[webpack-dev-server] Hot Module Replacement enabled.",
115+
"[webpack-dev-server] Live Reloading enabled.",
116+
]
117+
`;
118+
119+
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 []`;
120+
121+
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`] = `
122+
Array [
123+
"[HMR] Waiting for update signal from WDS...",
124+
"Hey.",
125+
"[webpack-dev-server] Hot Module Replacement enabled.",
126+
"[webpack-dev-server] Live Reloading enabled.",
127+
]
128+
`;
129+
130+
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 []`;
131+
132+
exports[`web socket server URL should work when "host" option is "local-ip" ("sockjs"): console messages 1`] = `
133+
Array [
134+
"[HMR] Waiting for update signal from WDS...",
135+
"Hey.",
136+
"[webpack-dev-server] Hot Module Replacement enabled.",
137+
"[webpack-dev-server] Live Reloading enabled.",
138+
]
139+
`;
140+
141+
exports[`web socket server URL should work when "host" option is "local-ip" ("sockjs"): page errors 1`] = `Array []`;
142+
143+
exports[`web socket server URL should work when "host" option is "local-ip" ("ws"): console messages 1`] = `
144+
Array [
145+
"[HMR] Waiting for update signal from WDS...",
146+
"Hey.",
147+
"[webpack-dev-server] Hot Module Replacement enabled.",
148+
"[webpack-dev-server] Live Reloading enabled.",
149+
]
150+
`;
151+
152+
exports[`web socket server URL should work when "host" option is "local-ip" ("ws"): page errors 1`] = `Array []`;
153+
154+
exports[`web socket server URL should work when "host" option is "local-ipv4" ("sockjs"): console messages 1`] = `
155+
Array [
156+
"[HMR] Waiting for update signal from WDS...",
157+
"Hey.",
158+
"[webpack-dev-server] Hot Module Replacement enabled.",
159+
"[webpack-dev-server] Live Reloading enabled.",
160+
]
161+
`;
162+
163+
exports[`web socket server URL should work when "host" option is "local-ipv4" ("sockjs"): page errors 1`] = `Array []`;
164+
165+
exports[`web socket server URL should work when "host" option is "local-ipv4" ("ws"): console messages 1`] = `
166+
Array [
167+
"[HMR] Waiting for update signal from WDS...",
168+
"Hey.",
169+
"[webpack-dev-server] Hot Module Replacement enabled.",
170+
"[webpack-dev-server] Live Reloading enabled.",
171+
]
172+
`;
173+
174+
exports[`web socket server URL should work when "host" option is "local-ipv4" ("ws"): page errors 1`] = `Array []`;
175+
110176
exports[`web socket server URL should work when "host" option is IPv4 ("sockjs"): console messages 1`] = `
111177
Array [
112178
"[HMR] Waiting for update signal from WDS...",

0 commit comments

Comments
 (0)