From 38f3dda9174ad647d081220088a6beff3c2283a3 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 30 Aug 2024 17:34:14 +0800 Subject: [PATCH] fix: align stats types with Rspack (#3324) --- packages/core/src/client/hmr.ts | 6 +-- packages/core/src/helpers/stats.ts | 8 ++-- packages/core/src/hooks.ts | 10 ++--- packages/core/src/plugins/fileSize.ts | 11 ++--- packages/core/src/provider/build.ts | 46 ++++++++++---------- packages/core/src/provider/createCompiler.ts | 13 +++--- packages/core/src/server/devServer.ts | 3 +- packages/core/src/server/environment.ts | 4 +- packages/core/src/server/socketServer.ts | 6 +-- packages/core/src/types/config/dev.ts | 3 +- packages/core/src/types/hooks.ts | 7 ++- packages/core/src/types/index.ts | 1 - packages/core/src/types/stats.ts | 20 --------- 13 files changed, 56 insertions(+), 82 deletions(-) delete mode 100644 packages/core/src/types/stats.ts diff --git a/packages/core/src/client/hmr.ts b/packages/core/src/client/hmr.ts index f07b1ccfe4..3b12cea1be 100644 --- a/packages/core/src/client/hmr.ts +++ b/packages/core/src/client/hmr.ts @@ -4,7 +4,7 @@ * * Tips: this package will be bundled and running in the browser, do not import any Node.js modules. */ -import type { ClientConfig, StatsError } from '../types'; +import type { ClientConfig, Rspack } from '../types'; import { formatStatsMessages } from './format'; const compilationName = RSBUILD_COMPILATION_NAME; @@ -85,7 +85,7 @@ function handleSuccess() { } // Compilation with warnings (e.g. ESLint). -function handleWarnings(warnings: StatsError[]) { +function handleWarnings(warnings: Rspack.StatsError[]) { clearOutdatedErrors(); const isHotUpdate = !isFirstCompilation; @@ -114,7 +114,7 @@ function handleWarnings(warnings: StatsError[]) { } // Compilation with errors (e.g. syntax error or missing modules). -function handleErrors(errors: StatsError[]) { +function handleErrors(errors: Rspack.StatsError[]) { clearOutdatedErrors(); isFirstCompilation = false; diff --git a/packages/core/src/helpers/stats.ts b/packages/core/src/helpers/stats.ts index 39d5b322a9..25979ab75d 100644 --- a/packages/core/src/helpers/stats.ts +++ b/packages/core/src/helpers/stats.ts @@ -2,7 +2,7 @@ import type { StatsCompilation, StatsValue } from '@rspack/core'; import color from 'picocolors'; import { formatStatsMessages } from '../client/format'; import { logger } from '../logger'; -import type { MultiStats, Rspack, Stats, StatsError } from '../types'; +import type { Rspack } from '../types'; import { isMultiCompiler } from './'; /** @@ -93,7 +93,7 @@ export const getAllStatsErrors = ( // stats error + childCompiler error // only append child errors when stats error does not exist, because some errors will exist in both stats and childCompiler if (statsData.errorsCount && statsData.errors?.length === 0) { - return statsData.children?.reduce( + return statsData.children?.reduce( (errors, curr) => errors.concat(curr.errors || []), [], ); @@ -106,7 +106,7 @@ export const getAllStatsWarnings = ( statsData: StatsCompilation, ): Rspack.StatsError[] | undefined => { if (statsData.warningsCount && statsData.warnings?.length === 0) { - return statsData.children?.reduce( + return statsData.children?.reduce( (warnings, curr) => warnings.concat(curr.warnings || []), [], ); @@ -130,7 +130,7 @@ export function getStatsOptions( } export function formatStats( - stats: Stats | MultiStats, + stats: Rspack.Stats | Rspack.MultiStats, options: StatsValue = {}, ): { message?: string; diff --git a/packages/core/src/hooks.ts b/packages/core/src/hooks.ts index a297debdab..a98c936363 100644 --- a/packages/core/src/hooks.ts +++ b/packages/core/src/hooks.ts @@ -13,7 +13,6 @@ import type { ModifyRspackConfigFn, ModifyWebpackChainFn, ModifyWebpackConfigFn, - MultiStats, OnAfterBuildFn, OnAfterCreateCompilerFn, OnAfterEnvironmentCompileFn, @@ -28,7 +27,6 @@ import type { OnDevCompileDoneFn, OnExitFn, Rspack, - Stats, } from './types'; export function createEnvironmentAsyncHook< @@ -364,7 +362,7 @@ export const registerBuildHook = ({ ], }); - const onDone = async (stats: Stats | MultiStats) => { + const onDone = async (stats: Rspack.Stats | Rspack.MultiStats) => { const p = context.hooks.onAfterBuild.call({ isFirstCompile, stats, @@ -375,7 +373,7 @@ export const registerBuildHook = ({ await p; }; - const onEnvironmentDone = async (buildIndex: number, stats: Stats) => { + const onEnvironmentDone = async (buildIndex: number, stats: Rspack.Stats) => { await context.hooks.onAfterEnvironmentCompile.callInEnvironment({ environment: environmentList[buildIndex].name, args: [ @@ -437,7 +435,7 @@ export const registerDevHook = ({ ], }); - const onDone = async (stats: Stats | MultiStats) => { + const onDone = async (stats: Rspack.Stats | Rspack.MultiStats) => { const p = context.hooks.onDevCompileDone.call({ isFirstCompile, stats, @@ -447,7 +445,7 @@ export const registerDevHook = ({ await p; }; - const onEnvironmentDone = async (buildIndex: number, stats: Stats) => { + const onEnvironmentDone = async (buildIndex: number, stats: Rspack.Stats) => { await context.hooks.onAfterEnvironmentCompile.callInEnvironment({ environment: environmentList[buildIndex].name, args: [ diff --git a/packages/core/src/plugins/fileSize.ts b/packages/core/src/plugins/fileSize.ts index ffddd824a9..dc853e2d1e 100644 --- a/packages/core/src/plugins/fileSize.ts +++ b/packages/core/src/plugins/fileSize.ts @@ -9,12 +9,7 @@ import zlib from 'node:zlib'; import color from 'picocolors'; import { CSS_REGEX, HTML_REGEX, JS_REGEX } from '../constants'; import { logger } from '../logger'; -import type { - PrintFileSizeOptions, - RsbuildPlugin, - Stats, - StatsAsset, -} from '../types'; +import type { PrintFileSizeOptions, RsbuildPlugin, Rspack } from '../types'; const gzip = promisify(zlib.gzip); @@ -82,7 +77,7 @@ const coloringAssetName = (assetName: string) => { async function printFileSizes( options: PrintFileSizeOptions, - stats: Stats, + stats: Rspack.Stats, rootPath: string, ) { const logs: string[] = []; @@ -91,7 +86,7 @@ async function printFileSizes( } const formatAsset = async ( - asset: StatsAsset, + asset: Rspack.StatsAsset, distPath: string, distFolder: string, ) => { diff --git a/packages/core/src/provider/build.ts b/packages/core/src/provider/build.ts index da4046d622..53b737b628 100644 --- a/packages/core/src/provider/build.ts +++ b/packages/core/src/provider/build.ts @@ -1,7 +1,7 @@ import { rspack } from '@rspack/core'; import { registerBuildHook } from '../hooks'; import { logger } from '../logger'; -import type { BuildOptions, MultiStats, Rspack, Stats } from '../types'; +import type { BuildOptions, Rspack } from '../types'; import { createCompiler } from './createCompiler'; import type { InitConfigsOptions } from './initConfigs'; @@ -46,26 +46,28 @@ export const build = async ( }; } - await new Promise<{ stats?: Stats | MultiStats }>((resolve, reject) => { - compiler.run((err, stats?: Stats | MultiStats) => { - if (err || stats?.hasErrors()) { - const buildError = err || new Error('Rspack build failed!'); - reject(buildError); - } - // If there is a compilation error, the close method should not be called. - // Otherwise the bundler may generate an invalid cache. - else { - // When using run or watch, call close and wait for it to finish before calling run or watch again. - // Concurrent compilations will corrupt the output files. - compiler.close((closeErr) => { - if (closeErr) { - logger.error(closeErr); - } + await new Promise<{ stats?: Rspack.Stats | Rspack.MultiStats }>( + (resolve, reject) => { + compiler.run((err, stats?: Rspack.Stats | Rspack.MultiStats) => { + if (err || stats?.hasErrors()) { + const buildError = err || new Error('Rspack build failed!'); + reject(buildError); + } + // If there is a compilation error, the close method should not be called. + // Otherwise the bundler may generate an invalid cache. + else { + // When using run or watch, call close and wait for it to finish before calling run or watch again. + // Concurrent compilations will corrupt the output files. + compiler.close((closeErr) => { + if (closeErr) { + logger.error(closeErr); + } - // Assert type of stats must align to compiler. - resolve({ stats }); - }); - } - }); - }); + // Assert type of stats must align to compiler. + resolve({ stats }); + }); + } + }); + }, + ); }; diff --git a/packages/core/src/provider/createCompiler.ts b/packages/core/src/provider/createCompiler.ts index c233186a22..f6e6ed03e8 100644 --- a/packages/core/src/provider/createCompiler.ts +++ b/packages/core/src/provider/createCompiler.ts @@ -10,7 +10,7 @@ import { } from '../helpers'; import { registerDevHook } from '../hooks'; import { logger } from '../logger'; -import type { DevConfig, MultiStats, Rspack, Stats } from '../types'; +import type { DevConfig, Rspack } from '../types'; import { type InitConfigsOptions, initConfigs } from './initConfigs'; export async function createCompiler(options: InitConfigsOptions): Promise<{ @@ -61,7 +61,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{ compiler.hooks.run.tap('rsbuild:run', logRspackVersion); } - const done = (stats: Stats | MultiStats) => { + const done = (stats: Rspack.Stats | Rspack.MultiStats) => { const statsJson = stats.toJson({ all: false, timings: true, @@ -98,9 +98,12 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{ isCompiling = false; }; - compiler.hooks.done.tap('rsbuild:done', (stats: Stats | MultiStats) => { - done(stats); - }); + compiler.hooks.done.tap( + 'rsbuild:done', + (stats: Rspack.Stats | Rspack.MultiStats) => { + done(stats); + }, + ); if (context.normalizedConfig?.mode === 'development') { registerDevHook({ diff --git a/packages/core/src/server/devServer.ts b/packages/core/src/server/devServer.ts index cf69c5f271..21ea0653b5 100644 --- a/packages/core/src/server/devServer.ts +++ b/packages/core/src/server/devServer.ts @@ -13,7 +13,6 @@ import type { NormalizedConfig, NormalizedDevConfig, Rspack, - Stats, } from '../types'; import { getTransformedHtml, loadBundle } from './environment'; import { @@ -120,7 +119,7 @@ export async function createDevServer< }; let outputFileSystem: Rspack.OutputFileSystem = fs; - let lastStats: Stats[]; + let lastStats: Rspack.Stats[]; // should register onDevCompileDone hook before startCompile const waitFirstCompileDone = runCompile diff --git a/packages/core/src/server/environment.ts b/packages/core/src/server/environment.ts index 86643b1aa1..bcae4ce0d7 100644 --- a/packages/core/src/server/environment.ts +++ b/packages/core/src/server/environment.ts @@ -1,5 +1,5 @@ import { join } from 'node:path'; -import type { EnvironmentContext, Stats } from '../types'; +import type { EnvironmentContext, Rspack } from '../types'; import { run } from './runner'; export type ServerUtils = { @@ -8,7 +8,7 @@ export type ServerUtils = { }; export const loadBundle = async ( - stats: Stats, + stats: Rspack.Stats, entryName: string, utils: ServerUtils, ): Promise => { diff --git a/packages/core/src/server/socketServer.ts b/packages/core/src/server/socketServer.ts index f55963efc7..a86da05572 100644 --- a/packages/core/src/server/socketServer.ts +++ b/packages/core/src/server/socketServer.ts @@ -4,7 +4,7 @@ import { parse } from 'node:querystring'; import type Ws from 'ws'; import { getAllStatsErrors, getAllStatsWarnings } from '../helpers'; import { logger } from '../logger'; -import type { DevConfig, Stats } from '../types'; +import type { DevConfig, Rspack } from '../types'; import { getCompilationName } from './helper'; interface ExtWebSocket extends Ws { @@ -31,7 +31,7 @@ export class SocketServer { private readonly options: DevConfig; - private stats: Record; + private stats: Record; private initialChunks: Record>; private timer: ReturnType | null = null; @@ -92,7 +92,7 @@ export class SocketServer { }); } - public updateStats(stats: Stats): void { + public updateStats(stats: Rspack.Stats): void { const compilationName = getCompilationName(stats.compilation); this.stats[compilationName] = stats; diff --git a/packages/core/src/types/config/dev.ts b/packages/core/src/types/config/dev.ts index b55289e134..02e001d92e 100644 --- a/packages/core/src/types/config/dev.ts +++ b/packages/core/src/types/config/dev.ts @@ -1,7 +1,6 @@ import type { IncomingMessage, ServerResponse } from 'node:http'; import type { WatchOptions } from 'chokidar'; import type { Rspack } from '../rspack'; -import type { Stats } from '../stats'; export type ProgressBarConfig = { id?: string; @@ -20,7 +19,7 @@ export type EnvironmentAPI = { /** * Get stats info about current environment. */ - getStats: () => Promise; + getStats: () => Promise; /** * Load and execute stats bundle in Server. diff --git a/packages/core/src/types/hooks.ts b/packages/core/src/types/hooks.ts index a5af59419f..680e8ea0e6 100644 --- a/packages/core/src/types/hooks.ts +++ b/packages/core/src/types/hooks.ts @@ -9,7 +9,6 @@ import type { } from './config'; import type { RsbuildEntry, RsbuildTarget } from './rsbuild'; import type { Rspack } from './rspack'; -import type { MultiStats, Stats } from './stats'; import type { HtmlRspackPlugin, WebpackConfig } from './thirdParty'; import type { MaybePromise } from './utils'; @@ -36,14 +35,14 @@ export type OnBeforeBuildFn = ( export type OnAfterEnvironmentCompileFn = ( params: CompileCommonParams & { - stats?: Stats; + stats?: Rspack.Stats; environment: EnvironmentContext; }, ) => MaybePromise; export type OnAfterBuildFn = ( params: CompileCommonParams & { - stats?: Stats | MultiStats; + stats?: Rspack.Stats | Rspack.MultiStats; environments: Record; }, ) => MaybePromise; @@ -52,7 +51,7 @@ export type OnCloseDevServerFn = () => MaybePromise; export type OnDevCompileDoneFn = (params: { isFirstCompile: boolean; - stats: Stats | MultiStats; + stats: Rspack.Stats | Rspack.MultiStats; environments: Record; }) => MaybePromise; diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts index bb758deb1e..33b74e3264 100644 --- a/packages/core/src/types/index.ts +++ b/packages/core/src/types/index.ts @@ -4,6 +4,5 @@ export * from './context'; export * from './utils'; export * from './plugin'; export * from './config'; -export * from './stats'; export * from './hooks'; export * from './thirdParty'; diff --git a/packages/core/src/types/stats.ts b/packages/core/src/types/stats.ts deleted file mode 100644 index b78641e6e5..0000000000 --- a/packages/core/src/types/stats.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { Compilation, StatsCompilation, StatsValue } from '@rspack/core'; - -export type { StatsError, StatsAsset } from '@rspack/core'; - -export declare class Stats { - constructor(statsJson: any); - hasErrors(): boolean; - hasWarnings(): boolean; - toJson(opts?: StatsValue): StatsCompilation; - toString(opts?: StatsValue): string; - compilation: Compilation; -} - -export declare class MultiStats { - stats: Stats[]; - hasErrors(): boolean; - hasWarnings(): boolean; - toJson(options?: StatsValue): StatsCompilation; - toString(options?: StatsValue): string; -}