Skip to content

Commit

Permalink
fix(typing): fix packages/rspack-cli ts-expect-error (#4673)
Browse files Browse the repository at this point in the history
* fix(typing): fix src/rspack-cli ts-expect-error

part of #4640

* fix(typing): fix rspack-cli build typing error

part of #4640

* fix(typing): fix rspack-cli utils type error

part of #4640
  • Loading branch information
HerringtonDarkholme authored Nov 17, 2023
1 parent 3e4362d commit 5764637
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 41 deletions.
24 changes: 8 additions & 16 deletions packages/rspack-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ export class BuildCommand implements RspackCommand {
}),
async options => {
const logger = cli.getLogger();
// @ts-expect-error
let createJsonStringifyStream;
let createJsonStringifyStream: typeof import("@discoveryjs/json-ext").stringifyStream;
if (options.json) {
const jsonExt = await import("@discoveryjs/json-ext");
createJsonStringifyStream = jsonExt.default.stringifyStream;
}

// @ts-expect-error
const callback = (error, stats: Stats | MultiStats) => {
const errorHandler = (
error: Error | null,
stats: Stats | MultiStats | undefined
) => {
if (error) {
logger.error(error);
process.exit(2);
Expand All @@ -47,15 +48,11 @@ export class BuildCommand implements RspackCommand {
compiler.options ? compiler.options.stats : undefined
)
}
: // @ts-expect-error
compiler.options
? // @ts-expect-error
compiler.options.stats
: compiler.options
? compiler.options.stats
: undefined;
// @ts-expect-error
if (options.json && createJsonStringifyStream) {
// @ts-expect-error
const handleWriteError = error => {
const handleWriteError = (error: Error) => {
logger.error(error);
process.exit(2);
};
Expand Down Expand Up @@ -90,11 +87,6 @@ export class BuildCommand implements RspackCommand {

const rspackOptions = { ...options, argv: { ...options } };

// @ts-expect-error
const errorHandler = (err, Stats) => {
callback(err, Stats);
};

const compiler = await cli.createCompiler(
rspackOptions,
"build",
Expand Down
21 changes: 10 additions & 11 deletions packages/rspack-cli/src/rspack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
Compiler
} from "@rspack/core";
import { normalizeEnv } from "./utils/options";
import { loadRspackConfig } from "./utils/loadConfig";
import { LoadedRspackConfig, loadRspackConfig } from "./utils/loadConfig";
import findConfig from "./utils/findConfig";
import type { RspackPluginInstance, RspackPluginFunction } from "@rspack/core";
import * as rspackCore from "@rspack/core";
Expand All @@ -40,7 +40,7 @@ export class RspackCLI {
async createCompiler(
options: RspackBuildCLIOptions,
rspackCommand: Command,
callback?: (e: Error, res?: Stats | MultiStats) => void
callback?: (e: Error | null, res?: Stats | MultiStats) => void
) {
process.env.RSPACK_CONFIG_VALIDATE = "loose";
let nodeEnv = process?.env?.NODE_ENV;
Expand All @@ -58,7 +58,6 @@ export class RspackCLI {
? (config as MultiRspackOptions).some(i => i.watch)
: (config as RspackOptions).watch;

// @ts-expect-error
return rspack(config, isWatch ? callback : undefined);
}
createColors(useColor?: boolean): RspackCLIColors {
Expand Down Expand Up @@ -89,8 +88,7 @@ export class RspackCLI {
};
}
async run(argv: string[]) {
// @ts-expect-error
if (semver.lt(semver.clean(process.version), "14.0.0")) {
if (semver.lt(semver.clean(process.version)!, "14.0.0")) {
this.getLogger().warn(
`Minimum recommended Node.js version is 14.0.0, current version is ${process.version}`
);
Expand Down Expand Up @@ -246,13 +244,14 @@ export class RspackCLI {
async loadConfig(
options: RspackCLIOptions
): Promise<RspackOptions | MultiRspackOptions> {
let loadedConfig = await loadRspackConfig(options);
let loadedConfig = (await loadRspackConfig(
options
)) as NonNullable<LoadedRspackConfig>;
if (options.configName) {
const notFoundConfigNames: string[] = [];

// @ts-expect-error
loadedConfig = options.configName.map((configName: string) => {
let found: RspackOptions | MultiRspackOptions | undefined;
let found: RspackOptions | undefined;

if (Array.isArray(loadedConfig)) {
found = loadedConfig.find(options => options.name === configName);
Expand All @@ -267,7 +266,9 @@ export class RspackCLI {
notFoundConfigNames.push(configName);
}

return found;
// WARNING: if config is not found, the program will exit
// so assert here is okay to avoid runtime filtering
return found!;
});

if (notFoundConfigNames.length > 0) {
Expand All @@ -284,7 +285,6 @@ export class RspackCLI {
}

if (typeof loadedConfig === "function") {
// @ts-expect-error
loadedConfig = loadedConfig(options.argv?.env, options.argv);
// if return promise we should await its result
if (
Expand All @@ -293,7 +293,6 @@ export class RspackCLI {
loadedConfig = await loadedConfig;
}
}
// @ts-expect-error
return loadedConfig;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/utils/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type LoadedRspackConfig =
| MultiRspackOptions
| ((
env: Record<string, any>,
argv: Record<string, any>
argv?: Record<string, any>
) => RspackOptions | MultiRspackOptions);

export async function loadRspackConfig(
Expand Down
7 changes: 2 additions & 5 deletions packages/rspack-cli/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,13 @@ export const previewOptions = (yargs: yargs.Argv) => {
});
};

// @ts-expect-error
export function normalizeEnv(argv) {
// @ts-expect-error
function parseValue(previous, value) {
export function normalizeEnv(argv: yargs.Arguments) {
function parseValue(previous: Record<string, unknown>, value: string) {
const [allKeys, val] = value.split(/=(.+)/, 2);
const splitKeys = allKeys.split(/\.(?!$)/);

let prevRef = previous;

// @ts-expect-error
splitKeys.forEach((someKey, index) => {
// https://github.com/webpack/webpack-cli/issues/3284
if (someKey.endsWith("=")) {
Expand Down
13 changes: 5 additions & 8 deletions packages/rspack-cli/src/utils/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { URLSearchParams } from "url";
import {
experimental_registerGlobalTrace as registerGlobalTrace,
experimental_cleanupGlobalTrace as cleanupGlobalTrace,
RspackOptions
RspackOptions,
type Compiler
} from "@rspack/core";

type JSCPUProfileOptionsOutput = string;
Expand Down Expand Up @@ -167,16 +168,14 @@ function resolveLoggingOptions(value: string): LoggingOptions {
class RspackProfileJSCPUProfilePlugin {
constructor(private output: string) {}

// @ts-expect-error
apply(compiler) {
apply(compiler: Compiler) {
const session = new inspector.Session();
session.connect();
session.post("Profiler.enable");
session.post("Profiler.start");
compiler.hooks.done.tapAsync(
RspackProfileJSCPUProfilePlugin.name,
// @ts-expect-error
(stats, callback) => {
(_stats, callback) => {
if (compiler.watchMode) return callback();
session.post("Profiler.stop", (error, param) => {
if (error) {
Expand All @@ -194,11 +193,9 @@ class RspackProfileJSCPUProfilePlugin {
class RspackProfileLoggingPlugin {
constructor(private output: string) {}

// @ts-expect-error
apply(compiler) {
apply(compiler: Compiler) {
compiler.hooks.done.tapAsync(
RspackProfileLoggingPlugin.name,
// @ts-expect-error
(stats, callback) => {
if (compiler.watchMode) return callback();
const logging = stats.toJson({
Expand Down

0 comments on commit 5764637

Please sign in to comment.