Skip to content

Commit 29e507b

Browse files
committed
chore(serve): improve dev server types
1 parent b17428b commit 29e507b

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

packages/serve/src/createConfig.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { devServerOptionsType } from './types';
2+
13
/**
24
*
35
* Creates a devServer config from CLI args
@@ -6,7 +8,7 @@
68
*
79
* @returns {Object} valid devServer options object
810
*/
9-
export default function createConfig(args): any {
11+
export default function createConfig(args): devServerOptionsType {
1012
const options = { ...args };
1113

1214
if (options.clientLogging) {

packages/serve/src/getDevServerOptions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logger from 'webpack-cli/lib/utils/logger';
2+
import { devServerOptionsType } from './types';
23

34
/**
45
*
@@ -9,7 +10,7 @@ import logger from 'webpack-cli/lib/utils/logger';
910
*
1011
* @returns {Object}
1112
*/
12-
export default function getDevServerOptions(compiler, args): any {
13+
export default function getDevServerOptions(compiler, args): devServerOptionsType[] {
1314
const defaultOpts = {};
1415
const devServerOptions = [];
1516
const compilers = compiler.compilers || [compiler];

packages/serve/src/mergeOptions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import { devServerOptionsType } from './types';
2+
13
/**
24
*
35
* Merges CLI options and devServer options from config file
46
*
5-
* @param {Object} cliOptions - devServer args
7+
* @param {Object} cliOptions - devServer CLI args
8+
* @param {Object} devServerOptions - devServer config options
69
*
710
* @returns {Object} merged options object
811
*/
9-
export default function mergeOptions(cliOptions, devServerOptions): any {
12+
export default function mergeOptions(cliOptions: devServerOptionsType, devServerOptions: devServerOptionsType): devServerOptionsType {
1013
// CLI options should take precedence over devServer options,
1114
// and CLI options should have no default values included
1215
const options = { ...devServerOptions, ...cliOptions };

packages/serve/src/startDevServer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ export default function startDevServer(compiler, args): void {
1616
const cliOptions = createConfig(args);
1717
const devServerOptions = getDevServerOptions(compiler, args);
1818

19-
const usedPorts = [];
19+
const usedPorts: number[] = [];
2020
devServerOptions.forEach((devServerOpts): void => {
2121
const options = mergeOptions(cliOptions, devServerOpts);
2222

2323
options.host = options.host || 'localhost';
2424
options.port = options.port || 8080;
2525

26-
if (usedPorts.find(options.port)) {
26+
const portNum = +options.port;
27+
28+
if (usedPorts.find((port) => portNum === port)) {
2729
throw new Error(
2830
'Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --name flag to specify your desired config.',
2931
);
3032
}
31-
usedPorts.push(options.port);
33+
usedPorts.push(portNum);
3234

3335
const server = new Server(compiler, options);
3436
server.listen(options.port, options.host, (err): void => {

packages/serve/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type devServerOptionsType = {
2+
host?: string;
3+
port?: string | number;
4+
client?: object;
5+
};

0 commit comments

Comments
 (0)