Skip to content

Commit 145b5d0

Browse files
authoredDec 11, 2024··
fix: speed up initial client bundling
1 parent b1e549f commit 145b5d0

21 files changed

+764
-880
lines changed
 

‎.cspell.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"version": "0.2",
33
"language": "en,en-gb",
44
"words": [
5+
"apos",
56
"camelcase",
67
"tapable",
78
"sockjs",

‎client-src/index.js

+354-16
Large diffs are not rendered by default.

‎client-src/overlay.js

+364-18
Large diffs are not rendered by default.

‎client-src/overlay/fsm.js

-64
This file was deleted.

‎client-src/overlay/runtime-error.js

-50
This file was deleted.

‎client-src/overlay/state-machine.js

-104
This file was deleted.

‎client-src/overlay/styles.js

-89
This file was deleted.

‎client-src/utils/createSocketURL.js

-163
This file was deleted.

‎client-src/utils/getCurrentScriptSource.js

-29
This file was deleted.

‎client-src/utils/log.js

+1-20
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,4 @@ setLogLevel(defaultLevel);
1818

1919
const log = logger.getLogger(name);
2020

21-
const logEnabledFeatures = (features) => {
22-
const enabledFeatures = Object.keys(features);
23-
if (!features || enabledFeatures.length === 0) {
24-
return;
25-
}
26-
27-
let logString = "Server started:";
28-
29-
// Server started: Hot Module Replacement enabled, Live Reloading enabled, Overlay disabled.
30-
for (let i = 0; i < enabledFeatures.length; i++) {
31-
const key = enabledFeatures[i];
32-
logString += ` ${key} ${features[key] ? "enabled" : "disabled"},`;
33-
}
34-
// replace last comma with a period
35-
logString = logString.slice(0, -1).concat(".");
36-
37-
log.info(logString);
38-
};
39-
40-
export { log, logEnabledFeatures, setLogLevel };
21+
export { log, setLogLevel };

‎client-src/utils/parseURL.js

-44
This file was deleted.

‎client-src/utils/reloadApp.js

-72
This file was deleted.

‎client-src/utils/stripAnsi.js

-26
This file was deleted.

‎client-src/webpack.config.js

-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ const baseForModules = {
2222
path: path.resolve(__dirname, "../client/modules"),
2323
...library,
2424
},
25-
optimization: {
26-
minimize: false,
27-
},
2825
target: ["web", "es5"],
2926
module: {
3027
rules: [
@@ -44,7 +41,6 @@ module.exports = [
4441
merge(baseForModules, {
4542
entry: path.join(__dirname, "modules/logger/index.js"),
4643
output: {
47-
// @ts-ignore
4844
filename: "logger/index.js",
4945
},
5046
module: {
@@ -54,7 +50,6 @@ module.exports = [
5450
use: [
5551
{
5652
loader: "babel-loader",
57-
// @ts-ignore
5853
options: {
5954
plugins: ["@babel/plugin-transform-object-assign"],
6055
},
@@ -78,7 +73,6 @@ module.exports = [
7873
entry: path.join(__dirname, "modules/sockjs-client/index.js"),
7974
output: {
8075
filename: "sockjs-client/index.js",
81-
// @ts-ignore
8276
library: "SockJS",
8377
libraryTarget: "umd",
8478
globalObject: "(typeof self !== 'undefined' ? self : this)",

‎lib/Server.js

+1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ class Server {
404404
let host;
405405

406406
const networks = Object.values(os.networkInterfaces())
407+
// eslint-disable-next-line no-shadow
407408
.flatMap((networks) => networks ?? [])
408409
.filter((network) => {
409410
if (!network || !network.address) {

‎package.json

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"connect-history-api-fallback": "^2.0.0",
6262
"express": "^4.21.1",
6363
"graceful-fs": "^4.2.6",
64-
"html-entities": "^2.4.0",
6564
"http-proxy-middleware": "^2.0.7",
6665
"ipaddr.js": "^2.1.0",
6766
"launch-editor": "^2.6.1",

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

+2-15
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ exports[`index should run onSocketMessage.error 1`] = `"error!!"`;
1616

1717
exports[`index should run onSocketMessage.ok 1`] = `"Ok"`;
1818

19-
exports[`index should run onSocketMessage.ok 2`] = `
20-
{
21-
"hot": false,
22-
"liveReload": false,
23-
"logging": "info",
24-
"overlay": false,
25-
"progress": false,
26-
"reconnect": 10,
27-
}
28-
`;
29-
3019
exports[`index should run onSocketMessage.progress and onSocketMessage['progress-update'] 1`] = `"Progress"`;
3120

3221
exports[`index should run onSocketMessage.progress and onSocketMessage['progress-update'] 2`] = `"12% - mock-msg."`;
@@ -66,7 +55,7 @@ exports[`index should run onSocketMessage['still-ok'] 2`] = `"StillOk"`;
6655

6756
exports[`index should set arguments into socket function 1`] = `
6857
[
69-
"mock-url",
58+
"ws://localhost/ws",
7059
{
7160
"close": [Function],
7261
"error": [Function],
@@ -85,8 +74,6 @@ exports[`index should set arguments into socket function 1`] = `
8574
"still-ok": [Function],
8675
"warnings": [Function],
8776
},
88-
10,
77+
undefined,
8978
]
9079
`;
91-
92-
exports[`index should update log level if options is passed 1`] = `"info"`;

‎test/client/index.test.js

+9-36
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ describe("index", () => {
88
let log;
99
let socket;
1010
let overlay;
11-
let reloadApp;
1211
let sendMessage;
1312
let onSocketMessage;
1413
const locationValue = self.location;
1514
const resourceQueryValue = global.__resourceQuery;
1615

1716
beforeEach(() => {
18-
global.__resourceQuery = "foo";
17+
global.__resourceQuery = "?mock-url";
1918
global.__webpack_hash__ = "mock-hash";
2019

2120
// log
@@ -51,25 +50,10 @@ describe("index", () => {
5150
const { createOverlay } = require("../../client-src/overlay");
5251
overlay = createOverlay();
5352

54-
// reloadApp
55-
jest.setMock("../../client-src/utils/reloadApp.js", jest.fn());
56-
reloadApp = require("../../client-src/utils/reloadApp");
57-
5853
// sendMessage
5954
jest.setMock("../../client-src/utils/sendMessage.js", jest.fn());
6055
sendMessage = require("../../client-src/utils/sendMessage");
6156

62-
// getUrlOptions
63-
jest.setMock("../../client-src/utils/parseURL.js", () => {
64-
return {
65-
logging: "info",
66-
reconnect: 10,
67-
};
68-
});
69-
70-
// createSocketUrl
71-
jest.setMock("../../client-src/utils/createSocketURL.js", () => "mock-url");
72-
7357
// issue: https://github.com/jsdom/jsdom/issues/2112
7458
delete window.location;
7559

@@ -93,7 +77,7 @@ describe("index", () => {
9377
test("should run onSocketMessage['still-ok']", () => {
9478
onSocketMessage["still-ok"]();
9579

96-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
80+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
9781
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
9882
expect(overlay.send).not.toBeCalledWith({ type: "DISMISS" });
9983

@@ -111,7 +95,6 @@ describe("index", () => {
11195
percent: "12",
11296
});
11397

114-
expect(log.log.info).not.toBeCalled();
11598
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
11699

117100
onSocketMessage.progress(true);
@@ -120,7 +103,7 @@ describe("index", () => {
120103
percent: "12",
121104
});
122105

123-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
106+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
124107
});
125108

126109
test("should run onSocketMessage.progress and onSocketMessage['progress-update'] and log plugin name", () => {
@@ -131,7 +114,6 @@ describe("index", () => {
131114
pluginName: "mock-plugin",
132115
});
133116

134-
expect(log.log.info).not.toBeCalled();
135117
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
136118

137119
onSocketMessage.progress(true);
@@ -141,7 +123,7 @@ describe("index", () => {
141123
pluginName: "mock-plugin",
142124
});
143125

144-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
126+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
145127
});
146128

147129
test("should run onSocketMessage.ok", () => {
@@ -154,23 +136,21 @@ describe("index", () => {
154136

155137
const res = onSocketMessage.ok();
156138

157-
expect(reloadApp).toBeCalled();
158-
expect(reloadApp.mock.calls[0][0]).toMatchSnapshot();
159139
// eslint-disable-next-line no-undefined
160140
expect(res).toEqual(undefined);
161141
});
162142

163143
test("should run onSocketMessage['static-changed']", () => {
164144
onSocketMessage["static-changed"]();
165145

166-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
146+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
167147
expect(self.location.reload).toBeCalled();
168148
});
169149

170150
test("should run onSocketMessage['static-changed'](file)", () => {
171151
onSocketMessage["static-changed"]("/static/assets/index.html");
172152

173-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
153+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
174154
expect(self.location.reload).toBeCalled();
175155
});
176156

@@ -201,7 +181,6 @@ describe("index", () => {
201181
)}`;
202182
overlay.send.mockReset();
203183
socket.mockReset();
204-
jest.unmock("../../client-src/utils/parseURL.js");
205184
require("../../client-src");
206185
onSocketMessage = socket.mock.calls[0][1];
207186

@@ -224,7 +203,6 @@ describe("index", () => {
224203
)}`;
225204
overlay.send.mockReset();
226205
socket.mockReset();
227-
jest.unmock("../../client-src/utils/parseURL.js");
228206
require("../../client-src");
229207
onSocketMessage = socket.mock.calls[0][1];
230208

@@ -243,7 +221,6 @@ describe("index", () => {
243221
jest.isolateModules(() => {
244222
// Use simple boolean
245223
global.__resourceQuery = "?overlay=true";
246-
jest.unmock("../../client-src/utils/parseURL.js");
247224
socket.mockReset();
248225
overlay.send.mockReset();
249226
require("../../client-src");
@@ -276,7 +253,7 @@ describe("index", () => {
276253
test("should run onSocketMessage.close", () => {
277254
onSocketMessage.close();
278255

279-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
256+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
280257
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
281258
});
282259

@@ -285,7 +262,7 @@ describe("index", () => {
285262
onSocketMessage.hot();
286263
onSocketMessage.close();
287264

288-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
265+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
289266
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
290267
});
291268

@@ -294,11 +271,7 @@ describe("index", () => {
294271
onSocketMessage.liveReload();
295272
onSocketMessage.close();
296273

297-
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
274+
expect(log.log.info.mock.calls[1][0]).toMatchSnapshot();
298275
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
299276
});
300-
301-
test("should update log level if options is passed", () => {
302-
expect(log.setLogLevel.mock.calls[0][0]).toMatchSnapshot();
303-
});
304277
});

‎test/client/utils/createSocketURL.test.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"use strict";
66

77
describe("'createSocketURL' function ", () => {
8+
global.__webpack_hash__ = "hash";
9+
810
const samples = [
9-
// __resourceQuery, location and socket URL
11+
// // __resourceQuery, location and socket URL
1012
[
1113
"?hostname=example.com&pathname=/ws",
1214
"http://example.com",
@@ -103,16 +105,20 @@ describe("'createSocketURL' function ", () => {
103105
];
104106

105107
samples.forEach(([__resourceQuery, location, expected]) => {
106-
jest.doMock(
107-
"../../../client-src/utils/getCurrentScriptSource",
108-
() => () => new URL("./entry.js", location).toString(),
109-
);
108+
test(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
109+
global.__resourceQuery = __resourceQuery;
110110

111-
const createSocketURL =
112-
require("../../../client-src/utils/createSocketURL").default;
113-
const parseURL = require("../../../client-src/utils/parseURL").default;
111+
if (__resourceQuery === null) {
112+
Object.defineProperty(document, "currentScript", {
113+
value: document.createElement("script"),
114+
configurable: true,
115+
});
116+
}
117+
118+
const client = require("../../../client-src/index");
119+
const createSocketURL = client.createSocketURL;
120+
const parseURL = client.parseURL;
114121

115-
test(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
116122
const selfLocation = new URL(location);
117123

118124
delete window.location;
@@ -121,6 +127,13 @@ describe("'createSocketURL' function ", () => {
121127

122128
const parsedURL = parseURL(__resourceQuery);
123129

130+
if (__resourceQuery === null) {
131+
Object.defineProperty(document, "currentScript", {
132+
value: null,
133+
configurable: true,
134+
});
135+
}
136+
124137
expect(createSocketURL(parsedURL)).toBe(expected);
125138
});
126139

‎test/client/utils/getCurrentScriptSource.test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44

55
"use strict";
66

7-
const getCurrentScriptSource =
8-
require("../../../client-src/utils/getCurrentScriptSource").default;
9-
107
describe("'getCurrentScriptSource' function", () => {
8+
let getCurrentScriptSource;
9+
10+
beforeEach(() => {
11+
global.__webpack_hash__ = "mock-hash";
12+
global.__resourceQuery = "?protocol=ws&hostname=0.0.0.0";
13+
14+
getCurrentScriptSource =
15+
require("../../../client-src/index").getCurrentScriptSource;
16+
});
17+
1118
afterEach(() => {
1219
Object.defineProperty(document, "currentScript", {
1320
// eslint-disable-next-line no-undefined

‎test/client/utils/reloadApp.test.js

-115
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.