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

refactor!: remove workarounds for the webpack v4 support #3346

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 1 addition & 8 deletions packages/configtest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ class ConfigTestCommand {
cli.logger.info(`Validate '${Array.from(configPaths).join(" ,")}'.`);

try {
// @ts-expect-error cli.webpack.validate returns void
const error: Error[] | undefined = cli.webpack.validate(config.options);

// TODO remove this after drop webpack@4
if (error && error.length > 0) {
// @ts-expect-error schema argument is missing
throw new cli.webpack.WebpackOptionsValidationError(error);
}
cli.webpack.validate(config.options);
} catch (error) {
if (cli.isValidationError(error as Error)) {
cli.logger.error((error as Error).message);
Expand Down
11 changes: 0 additions & 11 deletions packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,6 @@ interface BasicPackageJsonContent {
license: string;
}

/**
* Webpack V4
*/

type WebpackV4LegacyStats = Required<WebpackCLIStats>;
interface WebpackV4Compiler extends Compiler {
compiler: Compiler;
}

/**
* Plugins and util types
*/
Expand Down Expand Up @@ -320,10 +311,8 @@ export {
WebpackCLICommandOptions,
WebpackCLIMainOption,
WebpackCLILogger,
WebpackV4LegacyStats,
WebpackDevServerOptions,
WebpackRunOptions,
WebpackV4Compiler,
WebpackCompiler,
WebpackConfiguration,
Argv,
Expand Down
71 changes: 8 additions & 63 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import {
WebpackCLICommandOptions,
WebpackCLIMainOption,
WebpackCLILogger,
WebpackV4LegacyStats,
WebpackDevServerOptions,
WebpackRunOptions,
WebpackV4Compiler,
WebpackCompiler,
WebpackConfiguration,
Argv,
Expand All @@ -34,7 +32,6 @@ import {
Instantiable,
JsonExt,
ModuleName,
MultipleCompilerStatsOptions,
PackageInstallOptions,
PackageManager,
Path,
Expand Down Expand Up @@ -2226,40 +2223,12 @@ class WebpackCLI implements IWebpackCLI {
}

// Setup stats
// TODO remove after drop webpack@4
const statsForWebpack4 =
this.webpack.Stats &&
(this.webpack.Stats as unknown as Partial<WebpackV4LegacyStats>).presetToOptions;

if (statsForWebpack4) {
if (typeof item.stats === "undefined") {
item.stats = {};
} else if (typeof item.stats === "boolean") {
item.stats = (this.webpack.Stats as unknown as WebpackV4LegacyStats).presetToOptions(
item.stats,
);
} else if (
typeof item.stats === "string" &&
(item.stats === "none" ||
item.stats === "verbose" ||
item.stats === "detailed" ||
item.stats === "normal" ||
item.stats === "minimal" ||
item.stats === "errors-only" ||
item.stats === "errors-warnings")
) {
item.stats = (this.webpack.Stats as unknown as WebpackV4LegacyStats).presetToOptions(
item.stats,
);
}
} else {
if (typeof item.stats === "undefined") {
item.stats = { preset: "normal" };
} else if (typeof item.stats === "boolean") {
item.stats = item.stats ? { preset: "normal" } : { preset: "none" };
} else if (typeof item.stats === "string") {
item.stats = { preset: item.stats };
}
if (typeof item.stats === "undefined") {
item.stats = { preset: "normal" };
} else if (typeof item.stats === "boolean") {
item.stats = item.stats ? { preset: "normal" } : { preset: "none" };
} else if (typeof item.stats === "string") {
item.stats = { preset: item.stats };
}

let colors;
Expand All @@ -2277,10 +2246,7 @@ class WebpackCLI implements IWebpackCLI {
colors = Boolean(this.colors.isColorSupported);
}

// TODO remove after drop webpack v4
if (typeof item.stats === "object" && item.stats !== null) {
item.stats.colors = colors;
}
item.stats.colors = colors;

// Apply CLI plugin
if (!item.plugins) {
Expand All @@ -2305,12 +2271,7 @@ class WebpackCLI implements IWebpackCLI {
}

isValidationError(error: Error): error is WebpackError {
// https://github.com/webpack/webpack/blob/master/lib/index.js#L267
// https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
const ValidationError =
this.webpack.ValidationError || this.webpack.WebpackOptionsValidationError;

return error instanceof ValidationError || error.name === "ValidationError";
return error instanceof this.webpack.ValidationError || error.name === "ValidationError";
}

async createCompiler(
Expand Down Expand Up @@ -2350,11 +2311,6 @@ class WebpackCLI implements IWebpackCLI {
process.exit(2);
}

// TODO webpack@4 return Watching and MultiWatching instead Compiler and MultiCompiler, remove this after drop webpack@4
if (compiler && (compiler as WebpackV4Compiler).compiler) {
compiler = (compiler as WebpackV4Compiler).compiler;
}

return compiler;
}

Expand Down Expand Up @@ -2406,17 +2362,6 @@ class WebpackCLI implements IWebpackCLI {
? compiler.options.stats
: undefined;

// TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
const statsForWebpack4 =
this.webpack.Stats &&
(this.webpack.Stats as unknown as Partial<WebpackV4LegacyStats>).presetToOptions;

if (this.isMultipleCompiler(compiler) && statsForWebpack4) {
(statsOptions as StatsOptions).colors = (
statsOptions as MultipleCompilerStatsOptions
).children.some((child) => child.colors);
}

if (options.json && createJsonStringifyStream) {
const handleWriteError = (error: WebpackError) => {
this.logger.error(error);
Expand Down