Skip to content

Commit 6f7a174

Browse files
refactor: types (#4583)
1 parent 6a0e1d4 commit 6f7a174

File tree

4 files changed

+61
-60
lines changed

4 files changed

+61
-60
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"ts-node": "^10.9.1",
9898
"typescript": "^5.0.4",
9999
"typescript-eslint": "^8.35.0",
100-
"webpack": "^5.101.0",
100+
"webpack": "^5.103.0",
101101
"webpack-bundle-analyzer": "^4.5.0",
102102
"webpack-dev-server": "^5.1.0"
103103
},

packages/webpack-cli/src/types.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
type EntryOptions,
1010
type FileCacheOptions,
1111
type MultiCompiler,
12-
type MultiCompilerOptions,
12+
type MultiConfiguration,
1313
type MultiStats,
1414
type Stats,
1515
type WebpackError,
@@ -26,7 +26,10 @@ import {
2626
* Webpack CLI
2727
*/
2828

29-
type WebpackCallback = (err: Error | undefined, stats: Stats | MultiStats | undefined) => void;
29+
declare interface WebpackCallback {
30+
(err: null | Error, result?: Stats): void;
31+
(err: null | Error, result?: MultiStats): void;
32+
}
3033

3134
// TODO remove me in the next major release, we don't need extra interface
3235
interface IWebpackCLI {
@@ -95,7 +98,7 @@ interface WebpackCLICommandOption extends CommanderOption {
9598
}
9699

97100
interface WebpackCLIConfig {
98-
options: WebpackConfiguration | (WebpackConfiguration[] & MultiCompilerOptions);
101+
options: Configuration | MultiConfiguration;
99102
path: WeakMap<object, string[]>;
100103
}
101104

@@ -159,7 +162,7 @@ type WebpackCLIExternalCommandInfo = Pick<WebpackCLIOptions, "name" | "alias" |
159162
*/
160163

161164
type WebpackDevServerOptions = DevServerConfig &
162-
WebpackConfiguration &
165+
Configuration &
163166
ClientConfiguration &
164167
AssetEmittedInfo &
165168
WebpackOptionsNormalized &
@@ -183,11 +186,8 @@ type WebpackDevServerOptions = DevServerConfig &
183186
/**
184187
* Webpack
185188
*/
186-
type WebpackConfiguration = Configuration;
187-
type LoadableWebpackConfiguration = PotentialPromise<
188-
WebpackConfiguration | CallableWebpackConfiguration
189-
>;
190-
type CallableWebpackConfiguration = (env: Env | undefined, argv: Argv) => WebpackConfiguration;
189+
type LoadableWebpackConfiguration = PotentialPromise<Configuration | CallableWebpackConfiguration>;
190+
type CallableWebpackConfiguration = (env: Env | undefined, argv: Argv) => Configuration;
191191
type WebpackCompiler = Compiler | MultiCompiler;
192192

193193
interface EnumValueObject {
@@ -205,7 +205,7 @@ interface ArgumentConfig {
205205
values?: EnumValue[];
206206
}
207207

208-
type FileSystemCacheOptions = WebpackConfiguration & {
208+
type FileSystemCacheOptions = Configuration & {
209209
cache: FileCacheOptions & { defaultConfig: string[] };
210210
};
211211

@@ -343,7 +343,6 @@ export {
343343
type WebpackCLIOptions,
344344
type WebpackCallback,
345345
type WebpackCompiler,
346-
type WebpackConfiguration,
347346
type WebpackDevServerOptions,
348347
type WebpackRunOptions,
349348
};

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

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { type stringifyChunked } from "@discoveryjs/json-ext";
22
import { type Help, type ParseOptions } from "commander";
33
import {
44
type Compiler,
5+
type Configuration,
56
type MultiCompiler,
7+
type MultiConfiguration,
68
type MultiStatsOptions,
79
type StatsOptions,
810
type WebpackError,
@@ -50,7 +52,6 @@ import {
5052
type WebpackCLIOptions,
5153
type WebpackCallback,
5254
type WebpackCompiler,
53-
type WebpackConfiguration,
5455
type WebpackDevServerOptions,
5556
type WebpackRunOptions,
5657
} from "./types.js";
@@ -112,6 +113,12 @@ class WebpackCLI implements IWebpackCLI {
112113
});
113114
}
114115

116+
isMultipleConfiguration(
117+
config: Configuration | MultiConfiguration,
118+
): config is MultiConfiguration {
119+
return Array.isArray(config);
120+
}
121+
115122
isMultipleCompiler(compiler: WebpackCompiler): compiler is MultiCompiler {
116123
return (compiler as MultiCompiler).compilers as unknown as boolean;
117124
}
@@ -1791,7 +1798,7 @@ class WebpackCLI implements IWebpackCLI {
17911798
const loadConfigByPath = async (
17921799
configPath: string,
17931800
argv: Argv = {},
1794-
): Promise<{ options: WebpackConfiguration | WebpackConfiguration[]; path: string }> => {
1801+
): Promise<{ options: Configuration | Configuration[]; path: string }> => {
17951802
const ext = path.extname(configPath).toLowerCase();
17961803
let interpreted = Object.keys(interpret.jsVariants).find((variant) => variant === ext);
17971804
// Fallback `.cts` to `.ts`
@@ -1824,7 +1831,7 @@ class WebpackCLI implements IWebpackCLI {
18241831

18251832
let options: LoadableWebpackConfiguration | LoadableWebpackConfiguration[];
18261833

1827-
type LoadConfigOption = PotentialPromise<WebpackConfiguration>;
1834+
type LoadConfigOption = PotentialPromise<Configuration>;
18281835

18291836
let moduleType: "unknown" | "commonjs" | "esm" = "unknown";
18301837

@@ -1868,8 +1875,8 @@ class WebpackCLI implements IWebpackCLI {
18681875
await Promise.all(
18691876
optionsArray.map(async (_, i) => {
18701877
if (
1871-
this.isPromise<WebpackConfiguration | CallableWebpackConfiguration>(
1872-
optionsArray[i] as Promise<WebpackConfiguration | CallableWebpackConfiguration>,
1878+
this.isPromise<Configuration | CallableWebpackConfiguration>(
1879+
optionsArray[i] as Promise<Configuration | CallableWebpackConfiguration>,
18731880
)
18741881
) {
18751882
optionsArray[i] = await optionsArray[i];
@@ -1908,7 +1915,7 @@ class WebpackCLI implements IWebpackCLI {
19081915
}
19091916

19101917
return {
1911-
options: options as WebpackConfiguration | WebpackConfiguration[],
1918+
options: options as Configuration | Configuration[],
19121919
path: configPath,
19131920
};
19141921
};
@@ -1933,11 +1940,11 @@ class WebpackCLI implements IWebpackCLI {
19331940
for (const loadedConfig of loadedConfigs) {
19341941
if (Array.isArray(loadedConfig.options)) {
19351942
for (const item of loadedConfig.options) {
1936-
(config.options as WebpackConfiguration[]).push(item);
1943+
(config.options as Configuration[]).push(item);
19371944
config.path.set(options, [loadedConfig.path]);
19381945
}
19391946
} else {
1940-
(config.options as WebpackConfiguration[]).push(loadedConfig.options);
1947+
(config.options as Configuration[]).push(loadedConfig.options);
19411948
config.path.set(loadedConfig.options, [loadedConfig.path]);
19421949
}
19431950
}
@@ -1976,7 +1983,7 @@ class WebpackCLI implements IWebpackCLI {
19761983

19771984
config.options = loadedConfig.options;
19781985

1979-
if (Array.isArray(config.options)) {
1986+
if (this.isMultipleConfiguration(config.options)) {
19801987
for (const item of config.options) {
19811988
config.path.set(item, [loadedConfig.path]);
19821989
}
@@ -1992,7 +1999,7 @@ class WebpackCLI implements IWebpackCLI {
19921999
config.options = options.configName.map((configName) => {
19932000
let found;
19942001

1995-
if (Array.isArray(config.options)) {
2002+
if (this.isMultipleConfiguration(config.options)) {
19962003
found = config.options.find((options) => options.name === configName);
19972004
} else {
19982005
found = config.options.name === configName ? config.options : undefined;
@@ -2003,7 +2010,7 @@ class WebpackCLI implements IWebpackCLI {
20032010
}
20042011

20052012
return found;
2006-
}) as WebpackConfiguration[];
2013+
}) as Configuration[];
20072014

20082015
if (notFoundConfigNames.length > 0) {
20092016
this.logger.error(
@@ -2016,10 +2023,10 @@ class WebpackCLI implements IWebpackCLI {
20162023
}
20172024

20182025
const resolveExtends = async (
2019-
config: WebpackConfiguration,
2026+
config: Configuration,
20202027
configPaths: WebpackCLIConfig["path"],
20212028
extendsPaths: string[],
2022-
): Promise<WebpackConfiguration> => {
2029+
): Promise<Configuration> => {
20232030
delete config.extends;
20242031

20252032
const loadedConfigs = await Promise.all(
@@ -2044,10 +2051,7 @@ class WebpackCLI implements IWebpackCLI {
20442051
}
20452052
}
20462053

2047-
config = merge(
2048-
...(loadedOptions as [WebpackConfiguration, ...WebpackConfiguration[]]),
2049-
config,
2050-
);
2054+
config = merge(...(loadedOptions as [Configuration, ...Configuration[]]), config);
20512055

20522056
if (prevPaths) {
20532057
configPaths.set(config, [...prevPaths, ...loadedPaths]);
@@ -2067,7 +2071,7 @@ class WebpackCLI implements IWebpackCLI {
20672071
if (options.extends && options.extends.length > 0) {
20682072
const extendsPaths = options.extends;
20692073

2070-
if (Array.isArray(config.options)) {
2074+
if (this.isMultipleConfiguration(config.options)) {
20712075
config.options = await Promise.all(
20722076
config.options.map((options) => resolveExtends(options, config.path, extendsPaths)),
20732077
);
@@ -2077,7 +2081,10 @@ class WebpackCLI implements IWebpackCLI {
20772081
}
20782082
}
20792083
// if no extends option is passed, check if the config file has extends
2080-
else if (Array.isArray(config.options) && config.options.some((options) => options.extends)) {
2084+
else if (
2085+
this.isMultipleConfiguration(config.options) &&
2086+
config.options.some((options) => options.extends)
2087+
) {
20812088
config.options = await Promise.all(
20822089
config.options.map((options) => {
20832090
if (options.extends) {
@@ -2090,7 +2097,7 @@ class WebpackCLI implements IWebpackCLI {
20902097
return options;
20912098
}),
20922099
);
2093-
} else if (!Array.isArray(config.options) && config.options.extends) {
2100+
} else if (!this.isMultipleConfiguration(config.options) && config.options.extends) {
20942101
config.options = await resolveExtends(
20952102
config.options,
20962103
config.path,
@@ -2106,7 +2113,7 @@ class WebpackCLI implements IWebpackCLI {
21062113
// we can only merge when there are multiple configurations
21072114
// either by passing multiple configs by flags or passing a
21082115
// single config exporting an array
2109-
if (!Array.isArray(config.options) || config.options.length <= 1) {
2116+
if (!this.isMultipleConfiguration(config.options) || config.options.length <= 1) {
21102117
this.logger.error("At least two configurations are required for merge.");
21112118
process.exit(2);
21122119
}
@@ -2159,7 +2166,7 @@ class WebpackCLI implements IWebpackCLI {
21592166
"./plugins/cli-plugin",
21602167
);
21612168

2162-
const internalBuildConfig = (item: WebpackConfiguration) => {
2169+
const internalBuildConfig = (item: Configuration) => {
21632170
const originalWatchValue = item.watch;
21642171

21652172
// Apply options
@@ -2246,9 +2253,7 @@ class WebpackCLI implements IWebpackCLI {
22462253
}
22472254
}
22482255

2249-
const isFileSystemCacheOptions = (
2250-
config: WebpackConfiguration,
2251-
): config is FileSystemCacheOptions =>
2256+
const isFileSystemCacheOptions = (config: Configuration): config is FileSystemCacheOptions =>
22522257
Boolean(config.cache) && (config as FileSystemCacheOptions).cache.type === "filesystem";
22532258

22542259
// Setup default cache options
@@ -2325,13 +2330,13 @@ class WebpackCLI implements IWebpackCLI {
23252330
helpfulOutput: !options.json,
23262331
progress: options.progress,
23272332
analyze: options.analyze,
2328-
isMultiCompiler: Array.isArray(config.options),
2333+
isMultiCompiler: this.isMultipleConfiguration(config.options),
23292334
}),
23302335
);
23312336
}
23322337
};
23332338

2334-
if (Array.isArray(config.options)) {
2339+
if (this.isMultipleConfiguration(config.options)) {
23352340
for (const item of config.options) {
23362341
internalBuildConfig(item);
23372342
}
@@ -2364,19 +2369,16 @@ class WebpackCLI implements IWebpackCLI {
23642369
let compiler: WebpackCompiler;
23652370

23662371
try {
2367-
compiler = this.webpack(
2368-
config.options,
2369-
callback
2370-
? (error, stats) => {
2371-
if (error && this.isValidationError(error)) {
2372-
this.logger.error(error.message);
2373-
process.exit(2);
2374-
}
2375-
2376-
callback(error as Error | undefined, stats);
2372+
compiler = callback
2373+
? this.webpack(config.options, (error, stats) => {
2374+
if (error && this.isValidationError(error)) {
2375+
this.logger.error(error.message);
2376+
process.exit(2);
23772377
}
2378-
: callback,
2379-
)!;
2378+
2379+
callback(error as Error | null, stats);
2380+
})!
2381+
: this.webpack(config.options);
23802382
} catch (error) {
23812383
if (this.isValidationError(error)) {
23822384
this.logger.error(error.message);

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7956,10 +7956,10 @@ load-json-file@^4.0.0:
79567956
pify "^3.0.0"
79577957
strip-bom "^3.0.0"
79587958

7959-
loader-runner@^4.2.0:
7960-
version "4.3.0"
7961-
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-4.3.0.tgz#c1b4a163b99f614830353b16755e7149ac2314e1"
7962-
integrity sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==
7959+
loader-runner@^4.3.1:
7960+
version "4.3.1"
7961+
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-4.3.1.tgz#6c76ed29b0ccce9af379208299f07f876de737e3"
7962+
integrity sha512-IWqP2SCPhyVFTBtRcgMHdzlf9ul25NwaFx4wCEH/KjAXuuHY4yNjvPXsBokp8jCB936PyWRaPKUNh8NvylLp2Q==
79637963

79647964
locate-path@^2.0.0:
79657965
version "2.0.0"
@@ -11759,10 +11759,10 @@ webpack-sources@^3.3.3:
1175911759
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.3.3.tgz#d4bf7f9909675d7a070ff14d0ef2a4f3c982c723"
1176011760
integrity sha512-yd1RBzSGanHkitROoPFd6qsrxt+oFhg/129YzheDGqeustzX0vTZJZsSsQjVQC4yzBQ56K55XU8gaNCtIzOnTg==
1176111761

11762-
webpack@^5.101.0:
11763-
version "5.102.1"
11764-
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.102.1.tgz#1003a3024741a96ba99c37431938bf61aad3d988"
11765-
integrity sha512-7h/weGm9d/ywQ6qzJ+Xy+r9n/3qgp/thalBbpOi5i223dPXKi04IBtqPN9nTd+jBc7QKfvDbaBnFipYp4sJAUQ==
11762+
webpack@^5.103.0:
11763+
version "5.103.0"
11764+
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.103.0.tgz#17a7c5a5020d5a3a37c118d002eade5ee2c6f3da"
11765+
integrity sha512-HU1JOuV1OavsZ+mfigY0j8d1TgQgbZ6M+J75zDkpEAwYeXjWSqrGJtgnPblJjd/mAyTNQ7ygw0MiKOn6etz8yw==
1176611766
dependencies:
1176711767
"@types/eslint-scope" "^3.7.7"
1176811768
"@types/estree" "^1.0.8"
@@ -11781,7 +11781,7 @@ webpack@^5.101.0:
1178111781
glob-to-regexp "^0.4.1"
1178211782
graceful-fs "^4.2.11"
1178311783
json-parse-even-better-errors "^2.3.1"
11784-
loader-runner "^4.2.0"
11784+
loader-runner "^4.3.1"
1178511785
mime-types "^2.1.27"
1178611786
neo-async "^2.6.2"
1178711787
schema-utils "^4.3.3"

0 commit comments

Comments
 (0)