Skip to content

Commit

Permalink
feat: added types
Browse files Browse the repository at this point in the history
  • Loading branch information
taranek committed Feb 21, 2022
1 parent 6340c49 commit 8ec1375
Show file tree
Hide file tree
Showing 31 changed files with 858 additions and 372 deletions.
5 changes: 1 addition & 4 deletions .eslintignore
Expand Up @@ -2,10 +2,7 @@ coverage
.nyc_output
node_modules
dist
packages/configtest/lib
packages/generators/lib
packages/info/lib
packages/serve/lib
packages/*/lib
test/**/dist/
test/**/bin/
test/**/binary/
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -55,7 +55,6 @@ packages/**/*.map
# build files
packages/**/lib
packages/**/yarn.lock
!packages/webpack-cli/lib

# test output files
test/js/*
Expand Down
5 changes: 1 addition & 4 deletions .prettierignore
Expand Up @@ -2,10 +2,7 @@ coverage
.nyc_output
node_modules
dist
packages/configtest/lib
packages/generators/lib
packages/info/lib
packages/serve/lib
packages/*/lib
test/**/dist/
test/**/bin/
test/**/binary/
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"./packages/*"
],
"scripts": {
"clean": "del-cli \"*.tsbuildinfo\" \"packages/**/*.tsbuildinfo\" \"packages/!(webpack-cli)/lib/!(*.tpl)\" \"**/.yo-rc.json\"",
"clean": "del-cli \"*.tsbuildinfo\" \"packages/**/*.tsbuildinfo\" \"packages/*/lib/!(*.tpl)\" \"**/.yo-rc.json\"",
"prebuild": "yarn clean",
"prebuild:ci": "yarn clean && node ./scripts/setupBuild.js",
"build": "tsc --build",
Expand Down Expand Up @@ -54,6 +54,7 @@
"@commitlint/config-conventional": "^16.0.0",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.12",
"@types/rechoir": "^0.6.1",
"@typescript-eslint/eslint-plugin": "^5.10.1",
"@typescript-eslint/parser": "^5.10.1",
"@webpack-cli/migrate": "^1.1.2",
Expand Down
18 changes: 10 additions & 8 deletions packages/configtest/src/index.ts
@@ -1,8 +1,9 @@
import { IWebpackCLI } from "webpack-cli";

const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack";

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

if (Array.isArray(config.options)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config.options.forEach((options: any) => {
config.options.forEach((options) => {
if (config.path.get(options)) {
configPaths.add(config.path.get(options));
configPaths.add(config.path.get(options) as string);
}
});
} else {
if (config.path.get(config.options)) {
configPaths.add(config.path.get(config.options));
configPaths.add(config.path.get(config.options) as string);
}
}

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

try {
const error: Error[] = cli.webpack.validate(config.options);
// @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);
}
} catch (error) {
if (cli.isValidationError(error)) {
if (cli.isValidationError(error as Error)) {
cli.logger.error((error as Error).message);
} else {
cli.logger.error(error);
Expand Down
7 changes: 6 additions & 1 deletion packages/configtest/tsconfig.json
Expand Up @@ -4,5 +4,10 @@
"outDir": "./lib",
"rootDir": "./src"
},
"include": ["./src"]
"include": ["./src"],
"references": [
{
"path": "../webpack-cli"
}
]
}
4 changes: 2 additions & 2 deletions packages/generators/src/index.ts
Expand Up @@ -4,10 +4,10 @@ import pluginGenerator from "./plugin-generator";
import addonGenerator from "./addon-generator";
import initGenerator from "./init-generator";
import type { InitOptions, LoaderOptions, PluginOptions } from "./types";
import { IWebpackCLI } from "webpack-cli";

class GeneratorsCommand {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
async apply(cli: any): Promise<void> {
async apply(cli: IWebpackCLI): Promise<void> {
await cli.makeCommand(
{
name: "init [generation-path]",
Expand Down
7 changes: 3 additions & 4 deletions packages/generators/src/types/index.ts
@@ -1,5 +1,6 @@
import Generator from "yeoman-generator";
import path from "path";
import { IWebpackCLI } from "webpack-cli";

export type InitOptions = { template: string; force?: boolean };
export type LoaderOptions = { template: string };
Expand All @@ -16,17 +17,15 @@ export type BaseCustomGeneratorOptions = {
};
export type CustomGeneratorOptions<T extends BaseCustomGeneratorOptions> =
Generator.GeneratorOptions & {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cli: any;
cli: IWebpackCLI;
options: T;
};

export class CustomGenerator<
T extends BaseCustomGeneratorOptions = BaseCustomGeneratorOptions,
Z extends CustomGeneratorOptions<T> = CustomGeneratorOptions<T>,
> extends Generator<Z> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public cli: any;
public cli: IWebpackCLI;
public template: string;
public dependencies: string[];
public force: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/utils/helpers.ts
Expand Up @@ -37,7 +37,7 @@ export async function getInstaller(this: CustomGenerator): Promise<string> {
"packager",
"Pick a package manager:",
installers,
defaultPackager,
defaultPackager as string,
this.force,
);
return packager;
Expand Down
7 changes: 6 additions & 1 deletion packages/generators/tsconfig.json
Expand Up @@ -5,5 +5,10 @@
"outDir": "lib",
"rootDir": "src"
},
"include": ["src"]
"include": ["src"],
"references": [
{
"path": "../webpack-cli"
}
]
}
4 changes: 2 additions & 2 deletions packages/info/src/index.ts
@@ -1,4 +1,5 @@
import envinfo from "envinfo";
import { IWebpackCLI } from "webpack-cli";

interface Information {
Binaries?: string[];
Expand All @@ -10,8 +11,7 @@ interface Information {
}

class InfoCommand {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
async apply(cli: any): Promise<void> {
async apply(cli: IWebpackCLI): Promise<void> {
await cli.makeCommand(
{
name: "info",
Expand Down
7 changes: 6 additions & 1 deletion packages/info/tsconfig.json
Expand Up @@ -4,5 +4,10 @@
"outDir": "./lib",
"rootDir": "./src"
},
"include": ["./src"]
"include": ["./src"],
"references": [
{
"path": "../webpack-cli"
}
]
}
37 changes: 20 additions & 17 deletions packages/serve/src/index.ts
@@ -1,15 +1,14 @@
// eslint-disable-next-line node/no-extraneous-import
import type { Compiler, cli } from "webpack";
import { devServerOptionsType } from "./types";
import { IWebpackCLI, WebpackDevServerOptions } from "webpack-cli";

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

type Problem = NonNullable<ReturnType<typeof cli["processArguments"]>>[0];

type PublicPath = WebpackDevServerOptions["output"]["publicPath"];
class ServeCommand {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
async apply(cli: any): Promise<void> {
async apply(cli: IWebpackCLI): Promise<void> {
const loadDevServerOptions = () => {
// TODO simplify this after drop webpack v4 and webpack-dev-server v3
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -188,8 +187,7 @@ class ServeCommand {
process.exit(2);
}

const compilers =
typeof compiler.compilers !== "undefined" ? compiler.compilers : [compiler];
const compilers = cli.isMultipleCompiler(compiler) ? compiler.compilers : [compiler];
const possibleCompilers = compilers.filter(
(compiler: Compiler) => compiler.options.devServer,
);
Expand All @@ -199,7 +197,7 @@ class ServeCommand {
const usedPorts: number[] = [];

for (const compilerForDevServer of compilersForDevServer) {
let devServerOptions: devServerOptionsType;
let devServerOptions: WebpackDevServerOptions;

if (isNewDevServerCLIAPI) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -260,18 +258,23 @@ class ServeCommand {
process.exit(2);
}

devServerOptions = result;
devServerOptions = result as WebpackDevServerOptions;
} else {
// TODO remove in the next major release
const mergeOptions = (
devServerOptions: devServerOptionsType,
devServerCliOptions: devServerOptionsType,
): devServerOptionsType => {
devServerOptions: Partial<WebpackDevServerOptions>,
devServerCliOptions: Partial<WebpackDevServerOptions>,
): WebpackDevServerOptions => {
// CLI options should take precedence over devServer options,
// and CLI options should have no default values included
const options = { ...devServerOptions, ...devServerCliOptions };

if (devServerOptions.client && devServerCliOptions.client) {
if (
devServerOptions.client &&
devServerCliOptions.client &&
typeof devServerOptions.client === "object" &&
typeof devServerCliOptions.client === "object"
) {
// the user could set some client options in their devServer config,
// then also specify client options on the CLI
options.client = {
Expand All @@ -280,7 +283,7 @@ class ServeCommand {
};
}

return options;
return options as WebpackDevServerOptions;
};

devServerOptions = mergeOptions(
Expand All @@ -291,8 +294,8 @@ class ServeCommand {

// TODO remove in the next major release
if (!isDevServer4) {
const getPublicPathOption = (): string => {
const normalizePublicPath = (publicPath: string): string =>
const getPublicPathOption = (): PublicPath => {
const normalizePublicPath = (publicPath: PublicPath): PublicPath =>
typeof publicPath === "undefined" || publicPath === "auto" ? "/" : publicPath;

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

return normalizePublicPath(compilerForDevServer.options.output.publicPath);
};
const getStatsOption = (): string | boolean => {
const getStatsOption = (): WebpackDevServerOptions["stats"] => {
if (options.stats) {
return options.stats;
}
Expand Down Expand Up @@ -361,7 +364,7 @@ class ServeCommand {

servers.push(server);
} catch (error) {
if (cli.isValidationError(error)) {
if (cli.isValidationError(error as Error)) {
cli.logger.error((error as Error).message);
} else {
cli.logger.error(error);
Expand Down

0 comments on commit 8ec1375

Please sign in to comment.