Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new CLI options API for serve #2754

Merged
merged 5 commits into from
Jun 5, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import startDevServer from "./startDevServer";
class ServeCommand {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
async apply(cli: any): Promise<void> {
const { logger } = cli;
const { logger, webpack } = cli;

const loadDevServerOptions = () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require
const options = require("webpack-dev-server/bin/cli-flags");
const devServer = require("webpack-dev-server");

// TODO only keep `getArguments` after drop webpack v4

const options =
typeof devServer.getArguments === "function"
? devServer.getArguments(webpack)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

: // eslint-disable-next-line node/no-extraneous-require
require("webpack-dev-server/bin/cli-flags");

// Old options format
// { devServer: [{...}, {}...] }
Expand Down Expand Up @@ -64,7 +72,7 @@ class ServeCommand {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const webpackOptions: Record<string, any> = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const devServerOptions: Record<string, any> = {};
let devServerOptions: Record<string, any> = {};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const processors: Array<(opts: Record<string, any>) => void> = [];
Expand Down Expand Up @@ -139,6 +147,30 @@ class ServeCommand {
process.stdin.resume();
}

// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require
const devServer = require("webpack-dev-server");

if (typeof devServer.processArguments === "function") {
const args = devServerFlags.reduce((accumulator, flag) => {
accumulator[flag.name] = flag;

return accumulator;
}, {});
const values = Object.keys(devServerOptions).reduce((accumulator, name) => {
const kebabName = cli.utils.toKebabCase(name);

if (args[kebabName]) {
accumulator[kebabName] = options[name];
}

return accumulator;
}, {});
const result = { ...compiler.options.devServer };

devServer.processArguments(cli.webpack, args, result, values);
devServerOptions = result;
}

try {
servers = await startDevServer(compiler, devServerOptions, options, logger);
} catch (error) {
Expand Down