Skip to content

Commit 06d25a8

Browse files
committed
feat: port webpack-dev-server
1 parent 9c1dcd5 commit 06d25a8

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/server.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ import * as ipaddr from 'ipaddr.js';
1717
import { validate } from 'schema-utils';
1818
import schema from './options.json';
1919

20+
// Type definition matching open package's Options type
21+
// (Cannot import directly from ES module in CommonJS context)
22+
type OpenOptions = {
23+
readonly wait?: boolean;
24+
readonly background?: boolean;
25+
readonly newInstance?: boolean;
26+
readonly app?: OpenApp | readonly OpenApp[];
27+
readonly allowNonzeroExitCode?: boolean;
28+
};
29+
2030
import type {
2131
Server as HTTPServer,
2232
IncomingMessage,
@@ -214,7 +224,7 @@ export interface Open {
214224

215225
export interface NormalizedOpen {
216226
target: string;
217-
options: EXPECTED_ANY;
227+
options: OpenOptions;
218228
}
219229

220230
export interface WebSocketURL {
@@ -289,7 +299,7 @@ export interface Configuration<
289299
| string
290300
| WebSocketServerConfiguration;
291301
proxy?: ProxyConfigArray;
292-
open?: EXPECTED_ANY;
302+
open?: boolean | string | Open | Array<string | Open>;
293303
setupExitSignals?: boolean;
294304
client?: boolean | ClientConfiguration;
295305
headers?:
@@ -1104,9 +1114,7 @@ class Server<
11041114
options: {},
11051115
};
11061116
} else {
1107-
const serverOptions =
1108-
/** @type {ServerConfiguration<A, S>} */
1109-
options.server || {};
1117+
const serverOptions = options.server || ({} as ServerConfiguration<A, S>);
11101118

11111119
options.server = {
11121120
type: serverOptions.type || 'http',
@@ -1146,7 +1154,6 @@ class Server<
11461154
continue;
11471155
}
11481156

1149-
/** @type {any} */
11501157
const value = serverOptions[property];
11511158
const readFile = (
11521159
item: string | Buffer | undefined,
@@ -1344,21 +1351,19 @@ class Server<
13441351
options.open = [];
13451352
} else if (typeof options.open === 'boolean') {
13461353
options.open = options.open
1347-
? [
1354+
? ([
13481355
{
13491356
target: '<url>',
1350-
options: defaultOpenOptions as EXPECTED_ANY,
1357+
options: defaultOpenOptions as OpenOptions,
13511358
},
1352-
]
1359+
] as NormalizedOpen[])
13531360
: [];
13541361
} else if (typeof options.open === 'string') {
1355-
/** @type {NormalizedOpen[]} */
1356-
options.open = [{ target: options.open, options: defaultOpenOptions }];
1362+
options.open = [
1363+
{ target: options.open, options: defaultOpenOptions },
1364+
] as NormalizedOpen[];
13571365
} else if (Array.isArray(options.open)) {
1358-
/**
1359-
* @type {NormalizedOpen[]}
1360-
*/
1361-
const result = [];
1366+
const result: NormalizedOpen[] = [];
13621367

13631368
for (const item of options.open) {
13641369
if (typeof item === 'string') {
@@ -1370,11 +1375,11 @@ class Server<
13701375
result.push(...getOpenItemsFromObject(item));
13711376
}
13721377

1373-
/** @type {NormalizedOpen[]} */
1374-
options.open = result;
1378+
options.open = result as NormalizedOpen[];
13751379
} else {
1376-
/** @type {NormalizedOpen[]} */
1377-
options.open = [...getOpenItemsFromObject(options.open)];
1380+
options.open = [
1381+
...getOpenItemsFromObject(options.open),
1382+
] as NormalizedOpen[];
13781383
}
13791384

13801385
if (typeof options.port === 'string' && options.port !== 'auto') {
@@ -2204,9 +2209,6 @@ class Server<
22042209
* ]
22052210
*/
22062211
for (const proxyConfigOrCallback of this.options.proxy) {
2207-
/**
2208-
* @type {RequestHandler}
2209-
*/
22102212
let proxyMiddleware: RequestHandler | undefined;
22112213

22122214
let proxyConfig =
@@ -2621,7 +2623,9 @@ class Server<
26212623
: new URL(item.target, defaultOpenTarget).toString();
26222624
}
26232625

2624-
return open(openTarget, item.options).catch(() => {
2626+
// Type assertion needed: OpenOptions is compatible at runtime but TypeScript can't verify
2627+
// the type match between our type definition and the ES module's type in CommonJS context
2628+
return open(openTarget, item.options as EXPECTED_ANY).catch(() => {
26252629
const app = item.options.app as OpenApp | undefined;
26262630
this.logger.warn(
26272631
`Unable to open "${openTarget}" page${

0 commit comments

Comments
 (0)