Skip to content

Commit

Permalink
feat: allow to configure more client options via resource URL
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Mar 3, 2022
1 parent 85bbeea commit 7be8503
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 29 deletions.
34 changes: 34 additions & 0 deletions client-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ if (parsedResourceQuery["live-reload"] === "true") {
log.info("Live Reloading enabled.");
}

if (parsedResourceQuery.progress === "true") {
options.liveReload = true;

log.info("Progress reporting enabled.");
}

if (parsedResourceQuery.overlay) {
try {
options.overlay = JSON.parse(parsedResourceQuery.overlay);
} catch (e) {
log.error("Error parsing overlay options from resource query:", e);
}

// Fill in default "true" params for partially-specified objects.
if (typeof options.overlay === "object") {
options.overlay = {
errors: true,
warnings: true,
...options.overlay,
};
}

if (
options.overlay === true ||
(options.overlay.errors && options.overlay.warnings)
) {
log.info("Overlay is enabled for both errors and warnings.");
} else if (options.overlay.errors) {
log.info("Overlay is enabled for errors only.");
} else if (options.overlay.warnings) {
log.info("Overlay is enabled for warnings only.");
}
}

if (parsedResourceQuery.logging) {
options.logging = parsedResourceQuery.logging;
}
Expand Down
13 changes: 13 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,19 @@ class Server {
searchParams.set("logging", client.logging);
}

if (typeof client.progress !== "undefined") {
searchParams.set("progress", String(client.progress));
}

if (typeof client.overlay !== "undefined") {
searchParams.set(
"overlay",
typeof client.overlay === "boolean"
? String(client.overlay)
: JSON.stringify(client.overlay)
);
}

if (typeof client.reconnect !== "undefined") {
searchParams.set(
"reconnect",
Expand Down
54 changes: 54 additions & 0 deletions test/client/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,60 @@ describe("index", () => {
expect(overlay.show).toBeCalled();
});

test("should parse overlay options from resource query", () => {
jest.isolateModules(() => {
// Pass JSON config with warnings disabled
global.__resourceQuery = `?overlay=${encodeURIComponent(
`{"warnings": false}`
)}`;
overlay.show.mockReset();
socket.mockReset();
jest.unmock("../../client-src/utils/parseURL.js");
require("../../client-src");
onSocketMessage = socket.mock.calls[0][1];

onSocketMessage.warnings(["warn1"]);
expect(overlay.show).not.toBeCalled();

onSocketMessage.errors(["error1"]);
expect(overlay.show).toBeCalledTimes(1);
});

jest.isolateModules(() => {
// Pass JSON config with errors disabled
global.__resourceQuery = `?overlay=${encodeURIComponent(
`{"errors": false}`
)}`;
overlay.show.mockReset();
socket.mockReset();
jest.unmock("../../client-src/utils/parseURL.js");
require("../../client-src");
onSocketMessage = socket.mock.calls[0][1];

onSocketMessage.errors(["error1"]);
expect(overlay.show).not.toBeCalled();

onSocketMessage.warnings(["warn1"]);
expect(overlay.show).toBeCalledTimes(1);
});

jest.isolateModules(() => {
// Use simple boolean
global.__resourceQuery = "?overlay=true";
jest.unmock("../../client-src/utils/parseURL.js");
socket.mockReset();
overlay.show.mockReset();
require("../../client-src");
onSocketMessage = socket.mock.calls[0][1];

onSocketMessage.warnings(["warn2"]);
expect(overlay.show).toBeCalledTimes(1);

onSocketMessage.errors(["error2"]);
expect(overlay.show).toBeCalledTimes(2);
});
});

test("should run onSocketMessage.error", () => {
onSocketMessage.error("error!!");

Expand Down
72 changes: 43 additions & 29 deletions types/lib/Server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,12 @@ declare class Server {
| {
type: string;
multiple: boolean;
/**
* prependEntry Method for webpack 4
* @param {any} originalEntry
* @param {any} newAdditionalEntries
* @returns {any}
*/
description: string;
path: string;
}
Expand Down Expand Up @@ -826,12 +832,13 @@ declare class Server {
configs: {
type: string;
multiple: boolean;
/** @type {any} */
description: string;
negatedDescription: string;
path: string;
}[];
description: string;
/** @type {MultiCompiler} */ simpleType: string;
simpleType: string;
multiple: boolean;
};
open: {
Expand Down Expand Up @@ -865,10 +872,6 @@ declare class Server {
simpleType: string;
multiple: boolean;
};
/**
* @private
* @returns {Promise<void>}
*/
"open-app-name": {
configs: {
type: string;
Expand Down Expand Up @@ -928,7 +931,7 @@ declare class Server {
configs: (
| {
type: string;
/** @type {NormalizedStatic} */ multiple: boolean;
multiple: boolean;
description: string;
path: string;
}
Expand All @@ -937,7 +940,7 @@ declare class Server {
values: string[];
multiple: boolean;
description: string;
path: string;
/** @type {NormalizedStatic} */ path: string;
}
)[];
description: string;
Expand Down Expand Up @@ -1109,9 +1112,8 @@ declare class Server {
}[];
description: string;
multiple: boolean;
simpleType: string /** @type {Array<keyof ServerOptions>} */;
simpleType: string;
};
/** @type {Array<keyof ServerOptions>} */
static: {
configs: (
| {
Expand All @@ -1125,14 +1127,13 @@ declare class Server {
multiple: boolean;
description: string;
negatedDescription: string;
path: string;
path: string /** @type {ServerOptions} */;
}
)[];
description: string;
/** @type {any} */ simpleType: string;
multiple: boolean /** @type {ServerOptions} */;
simpleType: string;
multiple: boolean;
};
/** @type {ServerOptions} */
"static-directory": {
configs: {
type: string;
Expand All @@ -1159,7 +1160,7 @@ declare class Server {
configs: {
type: string;
multiple: boolean;
/** @type {ServerOptions} */ description: string;
description: string;
path: string;
}[];
description: string;
Expand Down Expand Up @@ -1244,7 +1245,6 @@ declare class Server {
description: string;
multiple: boolean;
path: string;
/** @type {ServerOptions & { cacert?: ServerOptions["ca"] }} */
type: string;
}
)[];
Expand All @@ -1256,6 +1256,7 @@ declare class Server {
configs: (
| {
description: string;
/** @type {ServerOptions & { cacert?: ServerOptions["ca"] }} */
multiple: boolean;
path: string;
type: string;
Expand Down Expand Up @@ -2110,12 +2111,6 @@ declare class Server {
}
| {
type: string;
/**
* prependEntry Method for webpack 4
* @param {any} originalEntry
* @param {any} newAdditionalEntries
* @returns {any}
*/
minLength: number;
items?: undefined;
minItems?: undefined;
Expand All @@ -2140,7 +2135,6 @@ declare class Server {
cli: {
description: string;
};
/** @type {Object<string,string>} */
additionalProperties?: undefined;
properties?: undefined;
}
Expand Down Expand Up @@ -2191,12 +2185,13 @@ declare class Server {
items: {
anyOf: (
| {
/** @type {any} */
type: string;
instanceof?: undefined;
}
| {
instanceof: string;
type?: undefined;
/** @type {any} */ type?: undefined;
}
)[];
};
Expand All @@ -2209,6 +2204,10 @@ declare class Server {
anyOf: {
$ref: string;
}[];
/**
* @private
* @returns {Compiler["options"]}
*/
link: string;
description: string;
};
Expand Down Expand Up @@ -2248,7 +2247,7 @@ declare class Server {
properties: {
passphrase: {
type: string;
/** @type {MultiCompiler} */ description: string;
description: string;
};
requestCert: {
type: string;
Expand All @@ -2268,6 +2267,10 @@ declare class Server {
instanceof?: undefined;
}
| {
/**
* @private
* @returns {Promise<void>}
*/
instanceof: string;
type?: undefined;
}
Expand Down Expand Up @@ -2375,7 +2378,7 @@ declare class Server {
}
| {
instanceof: string;
/** @type {NormalizedStatic} */ type?: undefined;
type?: undefined;
items?: undefined;
}
)[];
Expand Down Expand Up @@ -2641,7 +2644,8 @@ declare class Server {
$ref: string;
}[];
description: string;
link: string /** @type {Array<keyof ServerOptions>} */;
/** @type {ServerOptions} */
link: string;
};
WebSocketServerType: {
enum: string[];
Expand All @@ -2650,8 +2654,9 @@ declare class Server {
anyOf: (
| {
enum: boolean[];
/** @type {ServerOptions} */
cli: {
negatedDescription: string;
negatedDescription: string /** @type {ServerOptions} */;
};
$ref?: undefined;
}
Expand Down Expand Up @@ -2682,15 +2687,24 @@ declare class Server {
cli: {
exclude: boolean;
};
};
} /** @type {any} */;
};
additionalProperties: boolean;
/** @type {any} */ additionalProperties: boolean;
};
/** @type {ServerOptions} */
WebSocketServerString: {
type: string;
minLength: number;
};
/**
* @param {string | Buffer | undefined} item
* @returns {string | Buffer | undefined}
*/
};
/**
* @param {string | Buffer | undefined} item
* @returns {string | Buffer | undefined}
*/
additionalProperties: boolean;
properties: {
allowedHosts: {
Expand Down

0 comments on commit 7be8503

Please sign in to comment.