Skip to content

Commit 617c3b6

Browse files
committed
feat: allow to configure more client options via resource URL
1 parent 12fe530 commit 617c3b6

File tree

4 files changed

+117
-11
lines changed

4 files changed

+117
-11
lines changed

client-src/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ if (parsedResourceQuery["live-reload"] === "true") {
5858
log.info("Live Reloading enabled.");
5959
}
6060

61+
if (parsedResourceQuery.progress === "true") {
62+
options.liveReload = true;
63+
64+
log.info("Progress reporting enabled.");
65+
}
66+
67+
if (parsedResourceQuery.overlay) {
68+
try {
69+
options.overlay = JSON.parse(parsedResourceQuery.overlay);
70+
} catch (e) {
71+
log.error("Error parsing overlay options from resource query:", e);
72+
}
73+
74+
// Fill in default "true" params for partially-specified objects.
75+
if (typeof options.overlay === "object") {
76+
options.overlay = {
77+
errors: true,
78+
warnings: true,
79+
...options.overlay,
80+
};
81+
}
82+
83+
if (
84+
options.overlay === true ||
85+
(options.overlay.errors && options.overlay.warnings)
86+
) {
87+
log.info("Overlay is enabled for both errors and warnings.");
88+
} else if (options.overlay.errors) {
89+
log.info("Overlay is enabled for errors only.");
90+
} else if (options.overlay.warnings) {
91+
log.info("Overlay is enabled for warnings only.");
92+
}
93+
}
94+
6195
if (parsedResourceQuery.logging) {
6296
options.logging = parsedResourceQuery.logging;
6397
}

lib/Server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,19 @@ class Server {
598598
searchParams.set("logging", client.logging);
599599
}
600600

601+
if (typeof client.progress !== "undefined") {
602+
searchParams.set("progress", String(client.progress));
603+
}
604+
605+
if (typeof client.overlay !== "undefined") {
606+
searchParams.set(
607+
"overlay",
608+
typeof client.overlay === "boolean"
609+
? String(client.overlay)
610+
: JSON.stringify(client.overlay)
611+
);
612+
}
613+
601614
if (typeof client.reconnect !== "undefined") {
602615
searchParams.set(
603616
"reconnect",

test/client/index.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,60 @@ describe("index", () => {
208208
expect(overlay.show).toBeCalled();
209209
});
210210

211+
test("should parse overlay options from resource query", () => {
212+
jest.isolateModules(() => {
213+
// Pass JSON config with warnings disabled
214+
global.__resourceQuery = `?overlay=${encodeURIComponent(
215+
`{"warnings": false}`
216+
)}`;
217+
overlay.show.mockReset();
218+
socket.mockReset();
219+
jest.unmock("../../client-src/utils/parseURL.js");
220+
require("../../client-src");
221+
onSocketMessage = socket.mock.calls[0][1];
222+
223+
onSocketMessage.warnings(["warn1"]);
224+
expect(overlay.show).not.toBeCalled();
225+
226+
onSocketMessage.errors(["error1"]);
227+
expect(overlay.show).toBeCalledTimes(1);
228+
});
229+
230+
jest.isolateModules(() => {
231+
// Pass JSON config with errors disabled
232+
global.__resourceQuery = `?overlay=${encodeURIComponent(
233+
`{"errors": false}`
234+
)}`;
235+
overlay.show.mockReset();
236+
socket.mockReset();
237+
jest.unmock("../../client-src/utils/parseURL.js");
238+
require("../../client-src");
239+
onSocketMessage = socket.mock.calls[0][1];
240+
241+
onSocketMessage.errors(["error1"]);
242+
expect(overlay.show).not.toBeCalled();
243+
244+
onSocketMessage.warnings(["warn1"]);
245+
expect(overlay.show).toBeCalledTimes(1);
246+
});
247+
248+
jest.isolateModules(() => {
249+
// Use simple boolean
250+
global.__resourceQuery = "?overlay=true";
251+
jest.unmock("../../client-src/utils/parseURL.js");
252+
socket.mockReset();
253+
overlay.show.mockReset();
254+
require("../../client-src");
255+
onSocketMessage = socket.mock.calls[0][1];
256+
257+
onSocketMessage.warnings(["warn2"]);
258+
expect(overlay.show).toBeCalledTimes(1);
259+
260+
onSocketMessage.errors(["error2"]);
261+
expect(overlay.show).toBeCalledTimes(2);
262+
});
263+
});
264+
211265
test("should run onSocketMessage.error", () => {
212266
onSocketMessage.error("error!!");
213267

types/lib/Server.d.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ declare class Server {
13621362
"static-directory": {
13631363
configs: {
13641364
type: string;
1365-
multiple: boolean;
1365+
/** @type {ServerOptions} */ multiple: boolean;
13661366
description: string;
13671367
path: string;
13681368
}[];
@@ -1389,7 +1389,7 @@ declare class Server {
13891389
path: string;
13901390
}[];
13911391
description: string;
1392-
simpleType: string;
1392+
/** @type {any} */ simpleType: string;
13931393
multiple: boolean;
13941394
};
13951395
"static-reset": {
@@ -2383,19 +2383,18 @@ declare class Server {
23832383
instanceof?: undefined;
23842384
}
23852385
| {
2386-
/** @type {any} */
23872386
instanceof: string;
23882387
type?: undefined;
23892388
}
23902389
)[];
23912390
};
23922391
}
23932392
)[];
2394-
/** @type {any} */
23952393
description: string;
23962394
link: string;
23972395
};
23982396
Server: {
2397+
/** @type {any} */
23992398
anyOf: {
24002399
$ref: string;
24012400
}[];
@@ -2413,6 +2412,10 @@ declare class Server {
24132412
};
24142413
ServerString: {
24152414
type: string;
2415+
/**
2416+
* @private
2417+
* @returns {Compiler["options"]}
2418+
*/
24162419
minLength: number;
24172420
cli: {
24182421
exclude: boolean;
@@ -2428,10 +2431,11 @@ declare class Server {
24282431
};
24292432
options: {
24302433
$ref: string;
2431-
} /** @type {MultiCompiler} */;
2434+
};
24322435
};
2433-
/** @type {MultiCompiler} */ additionalProperties: boolean;
2436+
additionalProperties: boolean /** @type {MultiCompiler} */;
24342437
};
2438+
/** @type {MultiCompiler} */
24352439
ServerOptions: {
24362440
type: string;
24372441
additionalProperties: boolean;
@@ -2837,7 +2841,6 @@ declare class Server {
28372841
enum?: undefined;
28382842
}
28392843
)[];
2840-
/** @type {ServerOptions} */
28412844
cli: {
28422845
description: string;
28432846
};
@@ -2847,7 +2850,6 @@ declare class Server {
28472850
};
28482851
WebSocketServerObject: {
28492852
type: string;
2850-
/** @type {ServerOptions} */
28512853
properties: {
28522854
type: {
28532855
anyOf: {
@@ -2857,6 +2859,7 @@ declare class Server {
28572859
options: {
28582860
type: string;
28592861
additionalProperties: boolean;
2862+
/** @type {ServerOptions} */
28602863
cli: {
28612864
exclude: boolean;
28622865
};
@@ -2872,7 +2875,7 @@ declare class Server {
28722875
additionalProperties: boolean;
28732876
properties: {
28742877
allowedHosts: {
2875-
$ref: string;
2878+
$ref: string /** @type {ServerOptions} */;
28762879
};
28772880
bonjour: {
28782881
$ref: string;
@@ -2898,9 +2901,11 @@ declare class Server {
28982901
hot: {
28992902
$ref: string;
29002903
};
2904+
/** @type {any} */
29012905
http2: {
2902-
$ref: string;
2906+
$ref: string /** @type {ServerOptions} */;
29032907
};
2908+
/** @type {ServerOptions} */
29042909
https: {
29052910
$ref: string;
29062911
};
@@ -2917,7 +2922,7 @@ declare class Server {
29172922
$ref: string;
29182923
};
29192924
onBeforeSetupMiddleware: {
2920-
$ref: string /** @type {any} */;
2925+
$ref: string;
29212926
};
29222927
onListening: {
29232928
$ref: string;

0 commit comments

Comments
 (0)