Skip to content

Commit 6a2faf4

Browse files
refactor(cli): revert changes on build, watch, serve and runwebpack
1 parent 37e915c commit 6a2faf4

1 file changed

Lines changed: 12 additions & 126 deletions

File tree

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 12 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import {
4141
renderCommandHelp,
4242
renderError,
4343
renderFooter,
44-
renderInfo,
4544
renderInfoOutput,
4645
renderOptionHelp,
4746
renderSuccess,
@@ -1655,27 +1654,14 @@ class WebpackCLI {
16551654
this.schemaToOptions(cmd.context.webpack, undefined, this.#CLIOptions),
16561655
action: async (entries, options, cmd) => {
16571656
const { webpack } = cmd.context;
1658-
const renderOpts = this.#renderOptions();
16591657

16601658
if (entries.length > 0) {
16611659
options.entry = [...entries, ...(options.entry || [])];
16621660
}
16631661

16641662
options.webpack = webpack;
16651663

1666-
let headerWasPrinted = false;
1667-
1668-
await this.runWebpack(options as Options, false, () => {
1669-
headerWasPrinted = true;
1670-
renderCommandHeader(
1671-
{ name: "build", description: "Compiling your application…" },
1672-
renderOpts,
1673-
);
1674-
});
1675-
1676-
if (headerWasPrinted && !options.json) {
1677-
renderCommandFooter(renderOpts);
1678-
}
1664+
await this.runWebpack(options as Options, false);
16791665
},
16801666
},
16811667
watch: {
@@ -1693,20 +1679,14 @@ class WebpackCLI {
16931679
this.schemaToOptions(cmd.context.webpack, undefined, this.#CLIOptions),
16941680
action: async (entries, options, cmd) => {
16951681
const { webpack } = cmd.context;
1696-
const renderOpts = this.#renderOptions();
16971682

16981683
if (entries.length > 0) {
16991684
options.entry = [...entries, ...(options.entry || [])];
17001685
}
17011686

17021687
options.webpack = webpack;
17031688

1704-
await this.runWebpack(options as Options, true, () =>
1705-
renderCommandHeader(
1706-
{ name: "watch", description: "Watching for file changes…" },
1707-
renderOpts,
1708-
),
1709-
);
1689+
await this.runWebpack(options as Options, true);
17101690
},
17111691
},
17121692
serve: {
@@ -1735,8 +1715,6 @@ class WebpackCLI {
17351715
},
17361716
action: async (entries: string[], options: CommanderArgs, cmd) => {
17371717
const { webpack, webpackOptions, devServerOptions } = cmd.context;
1738-
const renderOpts = this.#renderOptions();
1739-
let serveHeaderPrinted = false;
17401718
const webpackCLIOptions: Options = { webpack, isWatchingLikeCommand: true };
17411719
const devServerCLIOptions: CommanderArgs = {};
17421720

@@ -1821,18 +1799,6 @@ class WebpackCLI {
18211799
const portNumber = Number(devServerConfiguration.port);
18221800

18231801
if (usedPorts.includes(portNumber)) {
1824-
renderError(
1825-
`Port ${portNumber} is already in use by another devServer configuration.`,
1826-
renderOpts,
1827-
);
1828-
renderInfo(
1829-
"Each devServer entry must use a unique port, or use --config-name to run a single configuration.",
1830-
renderOpts,
1831-
);
1832-
renderInfo(
1833-
"Documentation: https://webpack.js.org/configuration/dev-server/#devserverport",
1834-
renderOpts,
1835-
);
18361802
throw new Error(
18371803
"Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --config-name flag to specify your desired config.",
18381804
);
@@ -1841,15 +1807,6 @@ class WebpackCLI {
18411807
usedPorts.push(portNumber);
18421808
}
18431809

1844-
// All validation passed for this compiler, safe to print the header.
1845-
if (!serveHeaderPrinted) {
1846-
renderCommandHeader(
1847-
{ name: "serve", description: "Starting the development server…" },
1848-
renderOpts,
1849-
);
1850-
serveHeaderPrinted = true;
1851-
}
1852-
18531810
try {
18541811
const server = new DevServer(devServerConfiguration, compiler);
18551812

@@ -1858,14 +1815,11 @@ class WebpackCLI {
18581815
servers.push(server as unknown as InstanceType<DevServerConstructor>);
18591816
} catch (error) {
18601817
if (this.isValidationError(error as Error)) {
1861-
renderError((error as Error).message, renderOpts);
1818+
this.logger.error((error as Error).message);
18621819
} else {
1863-
renderError(String(error), renderOpts);
1820+
this.logger.error(error);
18641821
}
1865-
renderInfo(
1866-
"Documentation: https://webpack.js.org/configuration/dev-server/",
1867-
renderOpts,
1868-
);
1822+
18691823
process.exit(2);
18701824
}
18711825
}
@@ -2903,11 +2857,7 @@ class WebpackCLI {
29032857
return Boolean(compiler.options.watchOptions?.stdin);
29042858
}
29052859

2906-
async runWebpack(
2907-
options: Options,
2908-
isWatchCommand: boolean,
2909-
headerFn?: () => void,
2910-
): Promise<void> {
2860+
async runWebpack(options: Options, isWatchCommand: boolean): Promise<void> {
29112861
let compiler: Compiler | MultiCompiler;
29122862
let stringifyChunked: typeof stringifyChunkedType;
29132863
let Readable: typeof ReadableType;
@@ -2917,27 +2867,13 @@ class WebpackCLI {
29172867
({ Readable } = await import("node:stream"));
29182868
}
29192869

2920-
// For non-watch builds, resolve only after the first compilation so
2921-
// the caller (build action) can safely call renderCommandFooter() knowing
2922-
// the stats have already been output.
2923-
let onFirstBuildComplete: (() => void) | undefined;
2924-
const firstBuildComplete =
2925-
!isWatchCommand && !options.watch
2926-
? new Promise<void>((resolve) => {
2927-
onFirstBuildComplete = resolve;
2928-
})
2929-
: null;
2930-
29312870
const callback: WebpackCallback = (error, stats): void => {
29322871
if (error) {
29332872
this.logger.error(error);
29342873
process.exit(2);
29352874
}
29362875

2937-
const hasCompilationErrors =
2938-
stats && (stats.hasErrors() || (options.failOnWarnings && stats.hasWarnings()));
2939-
2940-
if (hasCompilationErrors) {
2876+
if (stats && (stats.hasErrors() || (options.failOnWarnings && stats.hasWarnings()))) {
29412877
process.exitCode = 1;
29422878
}
29432879

@@ -2962,72 +2898,28 @@ class WebpackCLI {
29622898
.on("error", handleWriteError)
29632899
.pipe(process.stdout)
29642900
.on("error", handleWriteError)
2965-
.on("close", () => {
2966-
process.stdout.write("\n");
2967-
onFirstBuildComplete?.();
2968-
});
2901+
.on("close", () => process.stdout.write("\n"));
29692902
} else {
29702903
Readable.from(stringifyChunked(stats.toJson(statsOptions as StatsOptions)))
29712904
.on("error", handleWriteError)
29722905
.pipe(fs.createWriteStream(options.json))
29732906
.on("error", handleWriteError)
2907+
// Use stderr to logging
29742908
.on("close", () => {
2975-
// Use stderr to logging
29762909
process.stderr.write(
29772910
`[webpack-cli] ${this.colors.green(
29782911
`stats are successfully stored as json to ${options.json}`,
29792912
)}\n`,
29802913
);
2981-
onFirstBuildComplete?.();
29822914
});
29832915
}
29842916
} else {
29852917
const printedStats = stats.toString(statsOptions);
29862918

2987-
// Only emit header+chrome when there is something meaningful to frame.
2988-
// stats: none produces an empty string, matching the old behavior.
2989-
const hasOutput = printedStats.trim().length > 0;
2990-
2991-
if (!hasCompilationErrors && hasOutput) {
2992-
headerFn?.();
2993-
}
2994-
2995-
// ...summary computation unchanged...
2996-
let summary: { success: boolean; message: string } | null = null;
2997-
if (!this.isMultipleCompiler(compiler)) {
2998-
try {
2999-
const json = (stats as Stats).toJson({
3000-
all: false,
3001-
timings: true,
3002-
errorsCount: true,
3003-
warningsCount: true,
3004-
});
3005-
const time = typeof json.time === "number" ? ` in ${json.time}ms` : "";
3006-
const errCount = json.errorsCount ?? 0;
3007-
const warnCount = json.warningsCount ?? 0;
3008-
3009-
if (errCount > 0) {
3010-
summary = {
3011-
success: false,
3012-
message: `Compilation failed: ${errCount} error${errCount !== 1 ? "s" : ""}${
3013-
warnCount > 0 ? `, ${warnCount} warning${warnCount !== 1 ? "s" : ""}` : ""
3014-
}${time}`,
3015-
};
3016-
} else if (warnCount > 0) {
3017-
summary = {
3018-
success: true,
3019-
message: `Compiled with ${warnCount} warning${warnCount !== 1 ? "s" : ""}${time}`,
3020-
};
3021-
} else {
3022-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3023-
summary = { success: true, message: `Compiled successfully${time}` };
3024-
}
3025-
} catch {
3026-
// proceed without summary
3027-
}
2919+
// Avoid extra empty line when `stats: 'none'`
2920+
if (printedStats) {
2921+
this.logger.raw(printedStats);
30282922
}
3029-
3030-
onFirstBuildComplete?.();
30312923
}
30322924
};
30332925

@@ -3096,12 +2988,6 @@ class WebpackCLI {
30962988
process.stdin.resume();
30972989
}
30982990
}
3099-
3100-
// For non-watch builds, block until the first compilation callback fires
3101-
// so callers can rely on all output being flushed before continuing.
3102-
if (firstBuildComplete) {
3103-
await firstBuildComplete;
3104-
}
31052991
}
31062992
}
31072993

0 commit comments

Comments
 (0)