|
6 | 6 | type MultiStatsOptions, |
7 | 7 | type StatsOptions, |
8 | 8 | type WebpackError, |
9 | | - cli, |
10 | 9 | default as webpack, |
11 | 10 | } from "webpack"; |
12 | 11 | import type webpackMerge from "webpack-merge"; |
@@ -104,9 +103,12 @@ class WebpackCLI implements IWebpackCLI { |
104 | 103 | this.program = program; |
105 | 104 | this.program.name("webpack"); |
106 | 105 | this.program.configureOutput({ |
107 | | - writeErr: this.logger.error, |
108 | | - outputError: (str, write) => |
109 | | - write(`Error: ${this.capitalizeFirstLetter(str.replace(/^error:/, "").trim())}`), |
| 106 | + writeErr: (str) => { |
| 107 | + this.logger.error(str); |
| 108 | + }, |
| 109 | + outputError: (str, write) => { |
| 110 | + write(`Error: ${this.capitalizeFirstLetter(str.replace(/^error:/, "").trim())}`); |
| 111 | + }, |
110 | 112 | }); |
111 | 113 | } |
112 | 114 |
|
@@ -135,11 +137,17 @@ class WebpackCLI implements IWebpackCLI { |
135 | 137 | } |
136 | 138 |
|
137 | 139 | createColors(useColor?: boolean): WebpackCLIColors { |
138 | | - if (typeof cli.createColors === "function") { |
139 | | - const { createColors, isColorSupported } = cli; |
140 | | - const shouldUseColor = useColor || isColorSupported(); |
| 140 | + try { |
| 141 | + const { cli } = require("webpack"); |
141 | 142 |
|
142 | | - return { ...createColors({ useColor: shouldUseColor }), isColorSupported: shouldUseColor }; |
| 143 | + if (typeof cli.createColors === "function") { |
| 144 | + const { createColors, isColorSupported } = cli; |
| 145 | + const shouldUseColor = useColor || isColorSupported(); |
| 146 | + |
| 147 | + return { ...createColors({ useColor: shouldUseColor }), isColorSupported: shouldUseColor }; |
| 148 | + } |
| 149 | + } catch { |
| 150 | + // Nothing |
143 | 151 | } |
144 | 152 |
|
145 | 153 | // TODO remove `colorette` and set [email protected] as the minimum supported version in the next major release |
@@ -1332,16 +1340,14 @@ class WebpackCLI implements IWebpackCLI { |
1332 | 1340 | }); |
1333 | 1341 |
|
1334 | 1342 | this.program.option("--color", "Enable colors on console."); |
1335 | | - this.program.on("option:color", function color() { |
1336 | | - // @ts-expect-error shadowing 'this' is intended |
| 1343 | + this.program.on("option:color", function color(this: WebpackCLICommand) { |
1337 | 1344 | const { color } = this.opts(); |
1338 | 1345 |
|
1339 | 1346 | cli.isColorSupportChanged = color; |
1340 | 1347 | cli.colors = cli.createColors(color); |
1341 | 1348 | }); |
1342 | 1349 | this.program.option("--no-color", "Disable colors on console."); |
1343 | | - this.program.on("option:no-color", function noColor() { |
1344 | | - // @ts-expect-error shadowing 'this' is intended |
| 1350 | + this.program.on("option:no-color", function noColor(this: WebpackCLICommand) { |
1345 | 1351 | const { color } = this.opts(); |
1346 | 1352 |
|
1347 | 1353 | cli.isColorSupportChanged = color; |
@@ -1839,8 +1845,7 @@ class WebpackCLI implements IWebpackCLI { |
1839 | 1845 | false, |
1840 | 1846 | moduleType, |
1841 | 1847 | ); |
1842 | | - // @ts-expect-error error type assertion |
1843 | | - } catch (error: Error) { |
| 1848 | + } catch (error) { |
1844 | 1849 | this.logger.error(`Failed to load '${configPath}' config`); |
1845 | 1850 |
|
1846 | 1851 | if (this.isValidationError(error)) { |
@@ -2337,8 +2342,10 @@ class WebpackCLI implements IWebpackCLI { |
2337 | 2342 | return config; |
2338 | 2343 | } |
2339 | 2344 |
|
2340 | | - isValidationError(error: Error): error is WebpackError { |
2341 | | - return error instanceof this.webpack.ValidationError || error.name === "ValidationError"; |
| 2345 | + isValidationError(error: unknown): error is WebpackError { |
| 2346 | + return ( |
| 2347 | + error instanceof this.webpack.ValidationError || (error as Error).name === "ValidationError" |
| 2348 | + ); |
2342 | 2349 | } |
2343 | 2350 |
|
2344 | 2351 | async createCompiler( |
@@ -2370,8 +2377,7 @@ class WebpackCLI implements IWebpackCLI { |
2370 | 2377 | } |
2371 | 2378 | : callback, |
2372 | 2379 | )!; |
2373 | | - // @ts-expect-error error type assertion |
2374 | | - } catch (error: Error) { |
| 2380 | + } catch (error) { |
2375 | 2381 | if (this.isValidationError(error)) { |
2376 | 2382 | this.logger.error(error.message); |
2377 | 2383 | } else { |
|
0 commit comments