Skip to content

Commit

Permalink
chore(serve): improve dev server types
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed Aug 21, 2020
1 parent e3df166 commit a75e611
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/serve/src/createConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { devServerOptionsType } from './types';

/**
*
* Creates a devServer config from CLI args
Expand All @@ -6,7 +8,7 @@
*
* @returns {Object} valid devServer options object
*/
export default function createConfig(args): any {
export default function createConfig(args): devServerOptionsType {
const options = { ...args };

if (options.clientLogging) {
Expand Down
3 changes: 2 additions & 1 deletion packages/serve/src/getDevServerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logger from 'webpack-cli/lib/utils/logger';
import { devServerOptionsType } from './types';

/**
*
Expand All @@ -9,7 +10,7 @@ import logger from 'webpack-cli/lib/utils/logger';
*
* @returns {Object}
*/
export default function getDevServerOptions(compiler, args): any {
export default function getDevServerOptions(compiler, args): devServerOptionsType[] {
const defaultOpts = {};
const devServerOptions = [];
const compilers = compiler.compilers || [compiler];
Expand Down
7 changes: 5 additions & 2 deletions packages/serve/src/mergeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { devServerOptionsType } from './types';

/**
*
* Merges CLI options and devServer options from config file
*
* @param {Object} cliOptions - devServer args
* @param {Object} cliOptions - devServer CLI args
* @param {Object} devServerOptions - devServer config options
*
* @returns {Object} merged options object
*/
export default function mergeOptions(cliOptions, devServerOptions): any {
export default function mergeOptions(cliOptions: devServerOptionsType, devServerOptions: devServerOptionsType): devServerOptionsType {
// CLI options should take precedence over devServer options,
// and CLI options should have no default values included
const options = { ...devServerOptions, ...cliOptions };
Expand Down
8 changes: 5 additions & 3 deletions packages/serve/src/startDevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ export default function startDevServer(compiler, args): void {
const cliOptions = createConfig(args);
const devServerOptions = getDevServerOptions(compiler, args);

const usedPorts = [];
const usedPorts: number[] = [];
devServerOptions.forEach((devServerOpts): void => {
const options = mergeOptions(cliOptions, devServerOpts);

options.host = options.host || 'localhost';
options.port = options.port || 8080;

if (usedPorts.find(options.port)) {
const portNum = +options.port;

if (usedPorts.find((port) => portNum === port)) {
throw new Error(
'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.',
);
}
usedPorts.push(options.port);
usedPorts.push(portNum);

const server = new Server(compiler, options);
server.listen(options.port, options.host, (err): void => {
Expand Down
5 changes: 5 additions & 0 deletions packages/serve/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type devServerOptionsType = {
host?: string;
port?: string | number;
client?: object;
};

0 comments on commit a75e611

Please sign in to comment.