Skip to content

Commit

Permalink
refactor!: remove workarounds for the webpack v4 support (#3346)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored and alexander-akait committed Nov 15, 2022
1 parent deb8843 commit 4cbb354
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 82 deletions.
9 changes: 1 addition & 8 deletions packages/configtest/src/index.ts
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
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
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

0 comments on commit 4cbb354

Please sign in to comment.