Skip to content

Commit b90136d

Browse files
committed
fix: apply client logging options directly on client loading
1 parent 9e3a481 commit b90136d

File tree

16 files changed

+570
-199
lines changed

16 files changed

+570
-199
lines changed

client-src/default/index.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const overlay = require('./overlay');
1212
const { log, setLogLevel } = require('./utils/log');
1313
const sendMessage = require('./utils/sendMessage');
1414
const reloadApp = require('./utils/reloadApp');
15+
const getUrlOptions = require('./utils/getUrlOptions');
1516
const createSocketUrl = require('./utils/createSocketUrl');
1617

1718
const status = {
@@ -27,12 +28,17 @@ const options = {
2728
useErrorOverlay: false,
2829
useProgress: false,
2930
};
30-
const socketUrl = createSocketUrl(__resourceQuery);
31+
const urlOptions = getUrlOptions(__resourceQuery);
32+
const socketUrl = createSocketUrl(urlOptions);
3133

3234
self.addEventListener('beforeunload', () => {
3335
status.isUnloading = true;
3436
});
3537

38+
if (urlOptions.query.logging) {
39+
setLogLevel(urlOptions.query.logging);
40+
}
41+
3642
if (typeof window !== 'undefined') {
3743
const qs = window.location.search.toLowerCase();
3844
options.hotReload = qs.indexOf('hotreload=false') === -1;
@@ -65,16 +71,6 @@ const onSocketMessage = {
6571
}
6672
sendMessage('StillOk');
6773
},
68-
logging: function logging(level) {
69-
// this is needed because the HMR logger operate separately from
70-
// dev server logger
71-
const hotCtx = require.context('webpack/hot', false, /^\.\/log$/);
72-
if (hotCtx.keys().indexOf('./log') !== -1) {
73-
hotCtx('./log').setLogLevel(level);
74-
}
75-
76-
setLogLevel(level);
77-
},
7874
overlay(value) {
7975
if (typeof document !== 'undefined') {
8076
if (typeof value === 'boolean') {

client-src/default/utils/createSocketUrl.js

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,19 @@
33
/* global self */
44

55
const url = require('url');
6-
const getCurrentScriptSource = require('./getCurrentScriptSource');
7-
8-
function createSocketUrl(resourceQuery, currentLocation) {
9-
let urlParts;
10-
11-
if (typeof resourceQuery === 'string' && resourceQuery !== '') {
12-
// If this bundle is inlined, use the resource query to get the correct url.
13-
// format is like `?http://0.0.0.0:8096&port=8097&host=localhost`
14-
urlParts = url.parse(
15-
resourceQuery
16-
// strip leading `?` from query string to get a valid URL
17-
.substr(1)
18-
// replace first `&` with `?` to have a valid query string
19-
.replace('&', '?'),
20-
true
21-
);
22-
} else {
23-
// Else, get the url from the <script> this file was called with.
24-
const scriptHost = getCurrentScriptSource();
25-
urlParts = url.parse(scriptHost || '/', true, true);
26-
}
27-
28-
// Use parameter to allow passing location in unit tests
29-
if (typeof currentLocation === 'string' && currentLocation !== '') {
30-
currentLocation = url.parse(currentLocation);
31-
} else {
32-
currentLocation = self.location;
33-
}
34-
35-
return getSocketUrl(urlParts, currentLocation);
36-
}
376

387
/*
398
* Gets socket URL based on Script Source/Location
409
* (scriptSrc: URL, location: URL) -> URL
4110
*/
42-
function getSocketUrl(urlParts, loc) {
11+
function createSocketUrl(urlParts, loc) {
12+
// Use parameter to allow passing location in unit tests
13+
if (typeof loc === 'string' && loc !== '') {
14+
loc = url.parse(loc);
15+
} else {
16+
loc = self.location;
17+
}
18+
4319
const { auth, query } = urlParts;
4420
let { hostname, protocol, port } = urlParts;
4521

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
/* global self */
4+
5+
const url = require('url');
6+
const getCurrentScriptSource = require('./getCurrentScriptSource');
7+
8+
function getUrlOptions(resourceQuery) {
9+
let options;
10+
11+
if (typeof resourceQuery === 'string' && resourceQuery !== '') {
12+
// If this bundle is inlined, use the resource query to get the correct url.
13+
// format is like `?http://0.0.0.0:8096&port=8097&host=localhost`
14+
options = url.parse(
15+
resourceQuery
16+
// strip leading `?` from query string to get a valid URL
17+
.substr(1)
18+
// replace first `&` with `?` to have a valid query string
19+
.replace('&', '?'),
20+
true
21+
);
22+
} else {
23+
// Else, get the url from the <script> this file was called with.
24+
const scriptHost = getCurrentScriptSource();
25+
options = url.parse(scriptHost || '/', true, true);
26+
}
27+
28+
return options;
29+
}
30+
31+
module.exports = getUrlOptions;

client-src/default/utils/log.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const webpackHotLog = require('webpack/hot/log');
34
const log = require('../../transpiled-modules/log');
45

56
const name = 'webpack-dev-server';
@@ -8,6 +9,7 @@ const name = 'webpack-dev-server';
89
const defaultLevel = 'info';
910

1011
function setLogLevel(level) {
12+
webpackHotLog.setLogLevel(level);
1113
log.configureDefaultLogger({
1214
level,
1315
});

lib/Server.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,6 @@ class Server {
556556
}
557557
});
558558

559-
if (this.options.client.logging) {
560-
this.sockWrite([connection], 'logging', this.options.client.logging);
561-
}
562-
563559
if (this.options.hot === true || this.options.hot === 'only') {
564560
this.sockWrite([connection], 'hot');
565561
}

lib/utils/DevServerPlugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ class DevServerPlugin {
5050
options.client && options.client.port
5151
? `&port=${options.client.port}`
5252
: '';
53+
const logging =
54+
options.client && options.client.logging
55+
? `&logging=${options.client.logging}`
56+
: '';
5357
/** @type {string} */
5458
const clientEntry = `${require.resolve(
5559
'../../client/default/'
56-
)}?${domain}${host}${path}${port}`;
60+
)}?${domain}${host}${path}${port}${logging}`;
5761

5862
/** @type {(string[] | string)} */
5963
let hotEntry;

test/cli/__snapshots__/cli.test.js.snap

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ exports[`CLI --hot webpack 4 1`] = `
1414
<i> [../../../client/default/overlay.js] Xdir/client/default/overlay.js X KiB {main} [built]
1515
<i> [../../../client/default/socket.js] Xdir/client/default/socket.js X KiB {main} [built]
1616
<i> [../../../client/default/utils/createSocketUrl.js] Xdir/client/default/utils/createSocketUrl.js X KiB {main} [built]
17+
<i> [../../../client/default/utils/getUrlOptions.js] Xdir/client/default/utils/getUrlOptions.js X bytes {main} [built]
1718
<i> [../../../client/default/utils/log.js] Xdir/client/default/utils/log.js X bytes {main} [built]
1819
<i> [../../../client/default/utils/reloadApp.js] Xdir/client/default/utils/reloadApp.js X KiB {main} [built]
1920
<i> [../../../client/default/utils/sendMessage.js] Xdir/client/default/utils/sendMessage.js X bytes {main} [built]
2021
<i> [../../../client/transpiled-modules/strip-ansi.js] Xdir/client/transpiled-modules/strip-ansi.js X bytes {main} [built]
21-
<i> [../../../node_modules/webpack/hot sync ^\\\\.\\\\/log$] (webpack)/hot sync nonrecursive ^\\\\.\\\\/log$ X bytes {main} [built]
2222
<i> [../../../node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js X KiB {main} [built]
2323
<i> [../../../node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js X bytes {main} [built]
2424
<i> [../../../node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js X KiB {main} [built]
@@ -33,16 +33,20 @@ exports[`CLI --hot webpack 5 1`] = `
3333
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
3434
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
3535
<i> runtime modules X KiB 10 modules
36-
<i> cacheable modules X KiB
37-
<i> modules by path ../../../node_modules/ X KiB 16 modules
36+
<i> modules by path ../../../ X KiB
37+
<i> modules by path ../../../node_modules/ X KiB
38+
<i> modules by path ../../../node_modules/webpack/hot/*.js X KiB 4 modules
39+
<i> modules by path ../../../node_modules/html-entities/lib/*.js X KiB 4 modules
40+
<i> modules by path ../../../node_modules/url/ X KiB 3 modules
41+
<i> modules by path ../../../node_modules/querystring/*.js X KiB 3 modules
42+
<i> 2 modules
3843
<i> modules by path ../../../client/ X KiB
39-
<i> modules by path ../../../client/default/ X KiB 8 modules
44+
<i> modules by path ../../../client/default/ X KiB 9 modules
4045
<i> modules by path ../../../client/transpiled-modules/*.js X KiB 2 modules
4146
<i> modules by path ../../../client/clients/*.js X KiB
4247
<i> ../../../client/clients/WebsocketClient.js X KiB [built] [code generated]
4348
<i> ../../../client/clients/BaseClient.js X KiB [built] [code generated]
44-
<i> ./foo.js X bytes [built] [code generated]
45-
<i> ../../../node_modules/webpack/hot/ sync nonrecursive ^\\\\.\\\\/log$ X bytes [built] [code generated]
49+
<i> ./foo.js X bytes [built] [code generated]
4650
<i> webpack x.x.x compiled successfully in X ms
4751
<i> [webpack-dev-middleware] Compiled successfully."
4852
`;
@@ -63,13 +67,13 @@ exports[`CLI --no-hot webpack 4 1`] = `
6367
<i> [../../../client/default/socket.js] Xdir/client/default/socket.js X KiB {main} [built]
6468
<i> [../../../client/default/utils/createSocketUrl.js] Xdir/client/default/utils/createSocketUrl.js X KiB {main} [built]
6569
<i> [../../../client/default/utils/getCurrentScriptSource.js] Xdir/client/default/utils/getCurrentScriptSource.js X bytes {main} [built]
70+
<i> [../../../client/default/utils/getUrlOptions.js] Xdir/client/default/utils/getUrlOptions.js X bytes {main} [built]
6671
<i> [../../../client/default/utils/log.js] Xdir/client/default/utils/log.js X bytes {main} [built]
6772
<i> [../../../client/default/utils/reloadApp.js] Xdir/client/default/utils/reloadApp.js X KiB {main} [built]
6873
<i> [../../../client/default/utils/sendMessage.js] Xdir/client/default/utils/sendMessage.js X bytes {main} [built]
6974
<i> [../../../client/transpiled-modules/log.js] Xdir/client/transpiled-modules/log.js X KiB {main} [built]
7075
<i> [../../../client/transpiled-modules/strip-ansi.js] Xdir/client/transpiled-modules/strip-ansi.js X bytes {main} [built]
7176
<i> [../../../node_modules/ansi-html/index.js] Xdir/node_modules/ansi-html/index.js X KiB {main} [built]
72-
<i> [../../../node_modules/webpack/hot sync ^\\\\.\\\\/log$] (webpack)/hot sync nonrecursive ^\\\\.\\\\/log$ X bytes {main} [built]
7377
<i> [./foo.js] X bytes {main} [built]
7478
<i> + 17 hidden modules
7579
<i> [webpack-dev-middleware] Compiled successfully."
@@ -79,17 +83,21 @@ exports[`CLI --no-hot webpack 5 1`] = `
7983
"<i> [webpack-dev-server] Project is running at http://localhost:8080/
8084
<i> [webpack-dev-server] Content not from webpack x.x.x served from 'Xdir/static' directory
8185
<i> [webpack-dev-middleware] asset main.js X KiB [emitted] (name: main)
82-
<i> runtime modules X bytes 3 modules
83-
<i> cacheable modules X KiB
84-
<i> modules by path ../../../node_modules/ X KiB 14 modules
86+
<i> runtime modules X bytes 2 modules
87+
<i> modules by path ../../../ X KiB
88+
<i> modules by path ../../../node_modules/ X KiB
89+
<i> modules by path ../../../node_modules/html-entities/lib/*.js X KiB 4 modules
90+
<i> modules by path ../../../node_modules/url/ X KiB 3 modules
91+
<i> modules by path ../../../node_modules/querystring/*.js X KiB 3 modules
92+
<i> modules by path ../../../node_modules/webpack/hot/*.js X KiB 2 modules
93+
<i> 2 modules
8594
<i> modules by path ../../../client/ X KiB
86-
<i> modules by path ../../../client/default/ X KiB 8 modules
95+
<i> modules by path ../../../client/default/ X KiB 9 modules
8796
<i> modules by path ../../../client/transpiled-modules/*.js X KiB 2 modules
8897
<i> modules by path ../../../client/clients/*.js X KiB
8998
<i> ../../../client/clients/WebsocketClient.js X KiB [built] [code generated]
9099
<i> ../../../client/clients/BaseClient.js X KiB [built] [code generated]
91-
<i> ./foo.js X bytes [built] [code generated]
92-
<i> ../../../node_modules/webpack/hot/ sync nonrecursive ^\\\\.\\\\/log$ X bytes [built] [code generated]
100+
<i> ./foo.js X bytes [built] [code generated]
93101
<i> webpack x.x.x compiled successfully in X ms
94102
<i> [webpack-dev-middleware] Compiled successfully."
95103
`;

test/client/__snapshots__/index.test.js.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ Array [
9494
"hot": [Function],
9595
"invalid": [Function],
9696
"liveReload": [Function],
97-
"logging": [Function],
9897
"ok": [Function],
9998
"overlay": [Function],
10099
"progress": [Function],
@@ -104,3 +103,5 @@ Array [
104103
},
105104
]
106105
`;
106+
107+
exports[`index should update log level if options is passed 1`] = `"none"`;

test/client/index.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('index', () => {
2222
warn: jest.fn(),
2323
error: jest.fn(),
2424
},
25+
setLogLevel: jest.fn(),
2526
});
2627
log = require('../../client-src/default/utils/log');
2728

@@ -44,6 +45,15 @@ describe('index', () => {
4445
jest.setMock('../../client-src/default/utils/sendMessage.js', jest.fn());
4546
sendMessage = require('../../client-src/default/utils/sendMessage');
4647

48+
// getUrlOptions
49+
jest.setMock('../../client-src/default/utils/getUrlOptions.js', () => {
50+
return {
51+
query: {
52+
logging: 'none',
53+
},
54+
};
55+
});
56+
4757
// createSocketUrl
4858
jest.setMock(
4959
'../../client-src/default/utils/createSocketUrl.js',
@@ -106,11 +116,6 @@ describe('index', () => {
106116
expect(overlay.clear).toBeCalled();
107117
});
108118

109-
// TODO: need to mock require.context
110-
test.skip("should run onSocketMessage['logging']", () => {
111-
onSocketMessage.logging();
112-
});
113-
114119
test("should run onSocketMessage.progress and onSocketMessage['progress-update']", () => {
115120
onSocketMessage.progress(false);
116121
onSocketMessage['progress-update']({
@@ -235,4 +240,8 @@ describe('index', () => {
235240
expect(log.log.error.mock.calls[0][0]).toMatchSnapshot();
236241
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
237242
});
243+
244+
test('should update log level if options is passed', () => {
245+
expect(log.setLogLevel.mock.calls[0][0]).toMatchSnapshot();
246+
});
238247
});

test/client/utils/__snapshots__/createSocketUrl.test.js.snap

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)