Skip to content

Commit 8ec1375

Browse files
authored
feat: added types
1 parent 6340c49 commit 8ec1375

31 files changed

+858
-372
lines changed

.eslintignore

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ coverage
22
.nyc_output
33
node_modules
44
dist
5-
packages/configtest/lib
6-
packages/generators/lib
7-
packages/info/lib
8-
packages/serve/lib
5+
packages/*/lib
96
test/**/dist/
107
test/**/bin/
118
test/**/binary/

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ packages/**/*.map
5555
# build files
5656
packages/**/lib
5757
packages/**/yarn.lock
58-
!packages/webpack-cli/lib
5958

6059
# test output files
6160
test/js/*

.prettierignore

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ coverage
22
.nyc_output
33
node_modules
44
dist
5-
packages/configtest/lib
6-
packages/generators/lib
7-
packages/info/lib
8-
packages/serve/lib
5+
packages/*/lib
96
test/**/dist/
107
test/**/bin/
118
test/**/binary/

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"./packages/*"
2323
],
2424
"scripts": {
25-
"clean": "del-cli \"*.tsbuildinfo\" \"packages/**/*.tsbuildinfo\" \"packages/!(webpack-cli)/lib/!(*.tpl)\" \"**/.yo-rc.json\"",
25+
"clean": "del-cli \"*.tsbuildinfo\" \"packages/**/*.tsbuildinfo\" \"packages/*/lib/!(*.tpl)\" \"**/.yo-rc.json\"",
2626
"prebuild": "yarn clean",
2727
"prebuild:ci": "yarn clean && node ./scripts/setupBuild.js",
2828
"build": "tsc --build",
@@ -54,6 +54,7 @@
5454
"@commitlint/config-conventional": "^16.0.0",
5555
"@types/jest": "^27.4.0",
5656
"@types/node": "^17.0.12",
57+
"@types/rechoir": "^0.6.1",
5758
"@typescript-eslint/eslint-plugin": "^5.10.1",
5859
"@typescript-eslint/parser": "^5.10.1",
5960
"@webpack-cli/migrate": "^1.1.2",

packages/configtest/src/index.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { IWebpackCLI } from "webpack-cli";
2+
13
const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";
24

35
class ConfigTestCommand {
4-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
5-
async apply(cli: any): Promise<void> {
6+
async apply(cli: IWebpackCLI): Promise<void> {
67
await cli.makeCommand(
78
{
89
name: "configtest [config-path]",
@@ -19,15 +20,14 @@ class ConfigTestCommand {
1920
const configPaths = new Set<string>();
2021

2122
if (Array.isArray(config.options)) {
22-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23-
config.options.forEach((options: any) => {
23+
config.options.forEach((options) => {
2424
if (config.path.get(options)) {
25-
configPaths.add(config.path.get(options));
25+
configPaths.add(config.path.get(options) as string);
2626
}
2727
});
2828
} else {
2929
if (config.path.get(config.options)) {
30-
configPaths.add(config.path.get(config.options));
30+
configPaths.add(config.path.get(config.options) as string);
3131
}
3232
}
3333

@@ -39,14 +39,16 @@ class ConfigTestCommand {
3939
cli.logger.info(`Validate '${Array.from(configPaths).join(" ,")}'.`);
4040

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

4445
// TODO remove this after drop webpack@4
4546
if (error && error.length > 0) {
47+
// @ts-expect-error schema argument is missing
4648
throw new cli.webpack.WebpackOptionsValidationError(error);
4749
}
4850
} catch (error) {
49-
if (cli.isValidationError(error)) {
51+
if (cli.isValidationError(error as Error)) {
5052
cli.logger.error((error as Error).message);
5153
} else {
5254
cli.logger.error(error);

packages/configtest/tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44
"outDir": "./lib",
55
"rootDir": "./src"
66
},
7-
"include": ["./src"]
7+
"include": ["./src"],
8+
"references": [
9+
{
10+
"path": "../webpack-cli"
11+
}
12+
]
813
}

packages/generators/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import pluginGenerator from "./plugin-generator";
44
import addonGenerator from "./addon-generator";
55
import initGenerator from "./init-generator";
66
import type { InitOptions, LoaderOptions, PluginOptions } from "./types";
7+
import { IWebpackCLI } from "webpack-cli";
78

89
class GeneratorsCommand {
9-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
10-
async apply(cli: any): Promise<void> {
10+
async apply(cli: IWebpackCLI): Promise<void> {
1111
await cli.makeCommand(
1212
{
1313
name: "init [generation-path]",

packages/generators/src/types/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Generator from "yeoman-generator";
22
import path from "path";
3+
import { IWebpackCLI } from "webpack-cli";
34

45
export type InitOptions = { template: string; force?: boolean };
56
export type LoaderOptions = { template: string };
@@ -16,17 +17,15 @@ export type BaseCustomGeneratorOptions = {
1617
};
1718
export type CustomGeneratorOptions<T extends BaseCustomGeneratorOptions> =
1819
Generator.GeneratorOptions & {
19-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
cli: any;
20+
cli: IWebpackCLI;
2121
options: T;
2222
};
2323

2424
export class CustomGenerator<
2525
T extends BaseCustomGeneratorOptions = BaseCustomGeneratorOptions,
2626
Z extends CustomGeneratorOptions<T> = CustomGeneratorOptions<T>,
2727
> extends Generator<Z> {
28-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29-
public cli: any;
28+
public cli: IWebpackCLI;
3029
public template: string;
3130
public dependencies: string[];
3231
public force: boolean;

packages/generators/src/utils/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function getInstaller(this: CustomGenerator): Promise<string> {
3737
"packager",
3838
"Pick a package manager:",
3939
installers,
40-
defaultPackager,
40+
defaultPackager as string,
4141
this.force,
4242
);
4343
return packager;

packages/generators/tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55
"outDir": "lib",
66
"rootDir": "src"
77
},
8-
"include": ["src"]
8+
"include": ["src"],
9+
"references": [
10+
{
11+
"path": "../webpack-cli"
12+
}
13+
]
914
}

packages/info/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import envinfo from "envinfo";
2+
import { IWebpackCLI } from "webpack-cli";
23

34
interface Information {
45
Binaries?: string[];
@@ -10,8 +11,7 @@ interface Information {
1011
}
1112

1213
class InfoCommand {
13-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
14-
async apply(cli: any): Promise<void> {
14+
async apply(cli: IWebpackCLI): Promise<void> {
1515
await cli.makeCommand(
1616
{
1717
name: "info",

packages/info/tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44
"outDir": "./lib",
55
"rootDir": "./src"
66
},
7-
"include": ["./src"]
7+
"include": ["./src"],
8+
"references": [
9+
{
10+
"path": "../webpack-cli"
11+
}
12+
]
813
}

packages/serve/src/index.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// eslint-disable-next-line node/no-extraneous-import
22
import type { Compiler, cli } from "webpack";
3-
import { devServerOptionsType } from "./types";
3+
import { IWebpackCLI, WebpackDevServerOptions } from "webpack-cli";
44

55
const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";
66
const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server";
77

88
type Problem = NonNullable<ReturnType<typeof cli["processArguments"]>>[0];
9-
9+
type PublicPath = WebpackDevServerOptions["output"]["publicPath"];
1010
class ServeCommand {
11-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
12-
async apply(cli: any): Promise<void> {
11+
async apply(cli: IWebpackCLI): Promise<void> {
1312
const loadDevServerOptions = () => {
1413
// TODO simplify this after drop webpack v4 and webpack-dev-server v3
1514
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -188,8 +187,7 @@ class ServeCommand {
188187
process.exit(2);
189188
}
190189

191-
const compilers =
192-
typeof compiler.compilers !== "undefined" ? compiler.compilers : [compiler];
190+
const compilers = cli.isMultipleCompiler(compiler) ? compiler.compilers : [compiler];
193191
const possibleCompilers = compilers.filter(
194192
(compiler: Compiler) => compiler.options.devServer,
195193
);
@@ -199,7 +197,7 @@ class ServeCommand {
199197
const usedPorts: number[] = [];
200198

201199
for (const compilerForDevServer of compilersForDevServer) {
202-
let devServerOptions: devServerOptionsType;
200+
let devServerOptions: WebpackDevServerOptions;
203201

204202
if (isNewDevServerCLIAPI) {
205203
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -260,18 +258,23 @@ class ServeCommand {
260258
process.exit(2);
261259
}
262260

263-
devServerOptions = result;
261+
devServerOptions = result as WebpackDevServerOptions;
264262
} else {
265263
// TODO remove in the next major release
266264
const mergeOptions = (
267-
devServerOptions: devServerOptionsType,
268-
devServerCliOptions: devServerOptionsType,
269-
): devServerOptionsType => {
265+
devServerOptions: Partial<WebpackDevServerOptions>,
266+
devServerCliOptions: Partial<WebpackDevServerOptions>,
267+
): WebpackDevServerOptions => {
270268
// CLI options should take precedence over devServer options,
271269
// and CLI options should have no default values included
272270
const options = { ...devServerOptions, ...devServerCliOptions };
273271

274-
if (devServerOptions.client && devServerCliOptions.client) {
272+
if (
273+
devServerOptions.client &&
274+
devServerCliOptions.client &&
275+
typeof devServerOptions.client === "object" &&
276+
typeof devServerCliOptions.client === "object"
277+
) {
275278
// the user could set some client options in their devServer config,
276279
// then also specify client options on the CLI
277280
options.client = {
@@ -280,7 +283,7 @@ class ServeCommand {
280283
};
281284
}
282285

283-
return options;
286+
return options as WebpackDevServerOptions;
284287
};
285288

286289
devServerOptions = mergeOptions(
@@ -291,8 +294,8 @@ class ServeCommand {
291294

292295
// TODO remove in the next major release
293296
if (!isDevServer4) {
294-
const getPublicPathOption = (): string => {
295-
const normalizePublicPath = (publicPath: string): string =>
297+
const getPublicPathOption = (): PublicPath => {
298+
const normalizePublicPath = (publicPath: PublicPath): PublicPath =>
296299
typeof publicPath === "undefined" || publicPath === "auto" ? "/" : publicPath;
297300

298301
if (options.outputPublicPath) {
@@ -307,7 +310,7 @@ class ServeCommand {
307310

308311
return normalizePublicPath(compilerForDevServer.options.output.publicPath);
309312
};
310-
const getStatsOption = (): string | boolean => {
313+
const getStatsOption = (): WebpackDevServerOptions["stats"] => {
311314
if (options.stats) {
312315
return options.stats;
313316
}
@@ -361,7 +364,7 @@ class ServeCommand {
361364

362365
servers.push(server);
363366
} catch (error) {
364-
if (cli.isValidationError(error)) {
367+
if (cli.isValidationError(error as Error)) {
365368
cli.logger.error((error as Error).message);
366369
} else {
367370
cli.logger.error(error);

0 commit comments

Comments
 (0)