Skip to content

Commit

Permalink
tests: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 6, 2021
1 parent 847302b commit f17ec45
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 286 deletions.
94 changes: 0 additions & 94 deletions packages/serve/__tests__/__snapshots__/startDevServer.test.ts.snap

This file was deleted.

173 changes: 0 additions & 173 deletions packages/serve/__tests__/startDevServer.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ServeCommand {
}

try {
servers = await startDevServer(compiler, devServerOptions, logger);
servers = await startDevServer(compiler, devServerOptions, options, logger);
} catch (error) {
if (error.name === 'ValidationError') {
logger.error(error.message);
Expand Down
55 changes: 40 additions & 15 deletions packages/serve/src/startDevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { devServerOptionsType } from './types';
* Starts the devServer
*
* @param {Object} compiler - a webpack compiler
* @param {Object} cliOptions - devServer args
* @param {Object} devServerCliOptions - dev server CLI options
* @param {Object} cliOptions - CLI options
* @param {Object} logger - logger
*
* @returns {Object[]} array of resulting servers
*/
export default async function startDevServer(compiler, cliOptions, logger): Promise<object[]> {
export default async function startDevServer(compiler, devServerCliOptions, cliOptions, logger): Promise<object[]> {
let devServerVersion, Server, findPort;

try {
Expand All @@ -25,15 +26,15 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
process.exit(2);
}

const mergeOptions = (cliOptions: devServerOptionsType, devServerOptions: devServerOptionsType): devServerOptionsType => {
const mergeOptions = (devServerOptions: devServerOptionsType, devServerCliOptions: devServerOptionsType): devServerOptionsType => {
// CLI options should take precedence over devServer options,
// and CLI options should have no default values included
const options = { ...devServerOptions, ...cliOptions };
const options = { ...devServerOptions, ...devServerCliOptions };

if (devServerOptions.client && cliOptions.client) {
if (devServerOptions.client && devServerCliOptions.client) {
// the user could set some client options in their devServer config,
// then also specify client options on the CLI
options.client = { ...devServerOptions.client, ...cliOptions.client };
options.client = { ...devServerOptions.client, ...devServerCliOptions.client };
}

return options;
Expand All @@ -59,24 +60,48 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
const devServersOptions = [];

for (const compilerWithDevServerOption of compilersWithDevServerOption) {
const options = mergeOptions(cliOptions, compilerWithDevServerOption.options.devServer || {});
const options = mergeOptions(compilerWithDevServerOption.options.devServer || {}, devServerCliOptions);

if (isDevServer4) {
options.port = await findPort(options.port);
options.client = options.client || {};
options.client.port = options.client.port || options.port;
} else {
if (!options.publicPath) {
options.publicPath =
typeof compilerWithDevServerOption.options.output.publicPath === 'undefined' ||
compilerWithDevServerOption.options.output.publicPath === 'auto'
? '/'
: compilerWithDevServerOption.options.output.publicPath;
}
const getPublicPathOption = () => {
const normalizePublicPath = (publicPath) => (typeof publicPath === 'undefined' || publicPath === 'auto' ? '/' : publicPath);

if (cliOptions.outputPublicPath) {
return normalizePublicPath(compilerWithDevServerOption.options.output.publicPath);
}

// webpack-dev-server@3
if (options.publicPath) {
return normalizePublicPath(options.publicPath);
}

// webpack-dev-server@4
if (options.dev && options.dev.publicPath) {
return normalizePublicPath(options.dev.publicPath);
}

return normalizePublicPath(compilerWithDevServerOption.options.output.publicPath);
};
const getStatsOption = () => {
if (cliOptions.stats) {
return compilerWithDevServerOption.options.stats;
}

if (options.stats) {
return options.stats;
}

return compilerWithDevServerOption.options.stats;
};

options.host = options.host || 'localhost';
options.port = options.port || 8080;
options.stats = compilerWithDevServerOption.options.stats;
options.stats = getStatsOption();
options.publicPath = getPublicPathOption();
}

if (options.port) {
Expand Down
3 changes: 2 additions & 1 deletion packages/serve/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export type devServerOptionsType = {
bonjour?: boolean;
client?: devServerClientOptions;
compress?: boolean;
dev?: object;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
dev?: Record<string, any>;
firewall?: boolean | string[];
headers?: object;
historyApiFallback?: boolean | object;
Expand Down
Loading

0 comments on commit f17ec45

Please sign in to comment.