From 7bc724c2a0156128383b676f2b5558cc6d5d001f Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 16 Aug 2024 16:03:50 +0800 Subject: [PATCH] fix: missing type for `compiler.inputFileSystem` --- .../rspack-test-tools/src/processor/stats.ts | 10 +- packages/rspack/etc/api.md | 305 +++++++++++++++++- packages/rspack/src/Compilation.ts | 3 +- packages/rspack/src/Compiler.ts | 12 +- packages/rspack/src/MultiCompiler.ts | 8 +- .../rspack/src/node/NodeEnvironmentPlugin.ts | 11 +- .../rspack/src/node/NodeWatchFileSystem.ts | 4 +- 7 files changed, 327 insertions(+), 26 deletions(-) diff --git a/packages/rspack-test-tools/src/processor/stats.ts b/packages/rspack-test-tools/src/processor/stats.ts index b53980573122..4d35cd6dba31 100644 --- a/packages/rspack-test-tools/src/processor/stats.ts +++ b/packages/rspack-test-tools/src/processor/stats.ts @@ -47,9 +47,13 @@ export class StatsProcessor< ? instance.compilers : [instance]; for (const compiler of compilers) { + if (!compiler.inputFileSystem) { + continue; + } const ifs = compiler.inputFileSystem; - compiler.inputFileSystem = Object.create(ifs); - compiler.inputFileSystem.readFile = (...args: any[]) => { + const inputFileSystem = Object.create(ifs); + compiler.inputFileSystem = inputFileSystem; + inputFileSystem.readFile = (...args: any[]) => { const callback = args.pop(); ifs.readFile.apply( ifs, @@ -60,7 +64,7 @@ export class StatsProcessor< return callback(null, result); callback(null, escapeEOL(result.toString("utf-8"))); } - ]) + ]) as Parameters ); }; diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index b126409fe7f5..3b3a753595d4 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -6,6 +6,7 @@ /// +import type { Abortable } from 'node:events'; import { AsyncParallelHook } from '@rspack/lite-tapable'; import { AsyncSeriesBailHook } from '@rspack/lite-tapable'; import * as binding from '@rspack/binding'; @@ -764,6 +765,17 @@ export type BaseUri = z.infer; // @public (undocumented) const baseUri: z.ZodString; +// @public (undocumented) +type BigIntStatsCallback = (err: NodeJS.ErrnoException | null, stats?: IBigIntStats) => void; + +// @public (undocumented) +type BufferCallback = (err: NodeJS.ErrnoException | null, data?: Buffer) => void; + +// @public (undocumented) +type BufferEncodingOption = "buffer" | { + encoding: "buffer"; +}; + // @public (undocumented) class Cache_2 { constructor(); @@ -1142,7 +1154,7 @@ export class Compilation { afterSeal: liteTapable.AsyncSeriesHook<[], void>; }>; // (undocumented) - inputFileSystem: any; + inputFileSystem: InputFileSystem | null; // (undocumented) logging: Map; // (undocumented) @@ -1298,7 +1310,7 @@ export class Compiler { // (undocumented) infrastructureLogger: any; // (undocumented) - inputFileSystem: any; + inputFileSystem: InputFileSystem | null; // (undocumented) intermediateFileSystem: any; // (undocumented) @@ -1987,6 +1999,9 @@ const EnableWasmLoadingPlugin: { }; }; +// @public (undocumented) +type EncodingOption = ObjectEncodingOptions | BufferEncoding | undefined | null; + // @public (undocumented) export type Entry = z.infer; @@ -4705,6 +4720,14 @@ const htmlRspackPluginOptions: z.ZodObject<{ meta?: Record> | undefined; }>; +// @public (undocumented) +type IBigIntStats = IStatsBase & { + atimeNs: bigint; + mtimeNs: bigint; + ctimeNs: bigint; + birthtimeNs: bigint; +}; + // @public (undocumented) interface IDirent { // (undocumented) @@ -4798,6 +4821,28 @@ const infrastructureLogging: z.ZodObject<{ stream?: NodeJS.WritableStream | undefined; }>; +// @public (undocumented) +type InputFileSystem = { + readFile: ReadFile; + readFileSync?: ReadFileSync; + readlink: Readlink; + readlinkSync?: ReadlinkSync; + readdir: Readdir; + readdirSync?: ReaddirSync; + stat: Stat; + statSync?: StatSync; + lstat?: LStat; + lstatSync?: LStatSync; + realpath?: RealPath; + realpathSync?: RealPathSync; + readJson?: ReadJson; + readJsonSync?: ReadJsonSync; + purge?: Purge; + join?: (path1: string, path2: string) => string; + relative?: (from: string, to: string) => string; + dirname?: (path: string) => string; +}; + // @public (undocumented) type IStats = IStatsBase; @@ -4956,6 +5001,22 @@ interface JsFormatOptions { wrapIife?: boolean; } +// @public (undocumented) +type JsonArray = JsonValue[]; + +// @public (undocumented) +type JsonObject = { + [Key in string]: JsonValue; +} & { + [Key in string]?: JsonValue | undefined; +}; + +// @public (undocumented) +type JsonPrimitive = string | number | boolean | null; + +// @public (undocumented) +type JsonValue = JsonPrimitive | JsonObject | JsonArray; + // @public (undocumented) type KnownAssetInfo = { immutable?: boolean; @@ -6031,6 +6092,42 @@ const LogType: Readonly<{ // @public (undocumented) type LogTypeEnum = (typeof LogType)[keyof typeof LogType]; +// @public (undocumented) +type LStat = { + (path: PathLike, callback: StatsCallback): void; + (path: PathLike, options: (StatOptions & { + bigint?: false; + }) | undefined, callback: StatsCallback): void; + (path: PathLike, options: StatOptions & { + bigint: true; + }, callback: BigIntStatsCallback): void; + (path: PathLike, options: StatOptions | undefined, callback: StatsOrBigIntStatsCallback): void; +}; + +// @public (undocumented) +type LStatSync = { + (path: PathLike, options?: undefined): IStats; + (path: PathLike, options?: StatSyncOptions & { + bigint?: false; + throwIfNoEntry: false; + }): IStats | undefined; + (path: PathLike, options: StatSyncOptions & { + bigint: true; + throwIfNoEntry: false; + }): IBigIntStats | undefined; + (path: PathLike, options?: StatSyncOptions & { + bigint?: false; + }): IStats; + (path: PathLike, options: StatSyncOptions & { + bigint: true; + }): IBigIntStats; + (path: PathLike, options: StatSyncOptions & { + bigint: boolean; + throwIfNoEntry?: false; + }): IStats | IBigIntStats; + (path: PathLike, options?: StatSyncOptions): IStats | IBigIntStats | undefined; +}; + // @public (undocumented) type MapOptions = { columns?: boolean; module?: boolean }; @@ -7063,8 +7160,8 @@ export class MultiCompiler { infrastructureLog: liteTapable.MultiHook>; }; // (undocumented) - get inputFileSystem(): void; - set inputFileSystem(value: void); + get inputFileSystem(): InputFileSystem; + set inputFileSystem(value: InputFileSystem); // (undocumented) get intermediateFileSystem(): void; set intermediateFileSystem(value: void); @@ -7296,6 +7393,11 @@ export class NormalModuleReplacementPlugin { readonly resourceRegExp: RegExp; } +// @public (undocumented) +type ObjectEncodingOptions = { + encoding?: BufferEncoding | null; +}; + // @public (undocumented) export type Optimization = z.infer; @@ -9297,6 +9399,12 @@ export type Pathinfo = z.infer; // @public (undocumented) const pathinfo: z.ZodUnion<[z.ZodBoolean, z.ZodLiteral<"verbose">]>; +// @public (undocumented) +type PathLike = string | Buffer | URL; + +// @public (undocumented) +type PathOrFileDescriptor = PathLike | number; + // @public (undocumented) type Performance_2 = z.infer; export { Performance_2 as Performance } @@ -9457,6 +9565,9 @@ export type PublicPath = z.infer; // @public (undocumented) const publicPath: z.ZodUnion<[z.ZodLiteral<"auto">, z.ZodUnion<[z.ZodString, z.ZodFunction, z.ZodOptional>], z.ZodUnknown>, z.ZodString>]>]>; +// @public (undocumented) +type Purge = (files?: string | string[] | Set) => void; + // @public (undocumented) type RawReactOptions = { runtime?: "automatic" | "classic"; @@ -9484,6 +9595,133 @@ type RawSourceMap = { // @public (undocumented) type ReactOptions = RawReactOptions | undefined; +// @public (undocumented) +type Readdir = { + (path: PathLike, options: { + encoding: BufferEncoding | null; + withFileTypes?: false; + recursive?: boolean; + } | BufferEncoding | null | undefined, callback: ReaddirStringCallback): void; + (path: PathLike, options: { + encoding: "buffer"; + withFileTypes?: false; + recursive?: boolean; + } | "buffer", callback: ReaddirBufferCallback): void; + (path: PathLike, callback: ReaddirStringCallback): void; + (path: PathLike, options: (ObjectEncodingOptions & { + withFileTypes: true; + recursive?: boolean; + }) | BufferEncoding | null | undefined, callback: ReaddirStringOrBufferCallback): void; + (path: PathLike, options: ObjectEncodingOptions & { + withFileTypes: true; + recursive?: boolean; + }, callback: ReaddirDirentCallback): void; +}; + +// @public (undocumented) +type ReaddirBufferCallback = (err: NodeJS.ErrnoException | null, files?: Buffer[]) => void; + +// @public (undocumented) +type ReaddirDirentCallback = (err: NodeJS.ErrnoException | null, files?: IDirent[]) => void; + +// @public (undocumented) +type ReaddirStringCallback = (err: NodeJS.ErrnoException | null, files?: string[]) => void; + +// @public (undocumented) +type ReaddirStringOrBufferCallback = (err: NodeJS.ErrnoException | null, files?: string[] | Buffer[]) => void; + +// @public (undocumented) +type ReaddirSync = { + (path: PathLike, options: { + encoding: BufferEncoding | null; + withFileTypes?: false; + recursive?: boolean; + } | BufferEncoding | null): string[]; + (path: PathLike, options: { + encoding: "buffer"; + withFileTypes?: false; + recursive?: boolean; + } | "buffer"): Buffer[]; + (path: PathLike, options: (ObjectEncodingOptions & { + withFileTypes?: false; + recursive?: boolean; + }) | BufferEncoding | null): string[] | Buffer[]; + (path: PathLike, options: ObjectEncodingOptions & { + withFileTypes: true; + recursive?: boolean; + }): IDirent[]; +}; + +// @public (undocumented) +type ReadFile = { + (path: PathOrFileDescriptor, options: ({ + encoding: null | undefined; + flag?: string; + } & Abortable) | null | undefined, callback: BufferCallback): void; + (path: PathOrFileDescriptor, options: ({ + encoding: BufferEncoding; + flag?: string; + } & Abortable) | BufferEncoding, callback: StringCallback): void; + (path: PathOrFileDescriptor, options: (ObjectEncodingOptions & { + flag?: string; + } & Abortable) | BufferEncoding | null | undefined, callback: StringOrBufferCallback): void; + (path: PathOrFileDescriptor, callback: BufferCallback): void; +}; + +// @public (undocumented) +type ReadFileSync = { + (path: PathOrFileDescriptor, options: { + encoding: null | undefined; + flag?: string; + } | null): Buffer; + (path: PathOrFileDescriptor, options: { + encoding: BufferEncoding; + flag?: string; + } | BufferEncoding): string; + (path: PathOrFileDescriptor, options: (ObjectEncodingOptions & { + flag?: string; + }) | BufferEncoding | null): string | Buffer; +}; + +// @public (undocumented) +type ReadJson = (path: PathOrFileDescriptor, callback: ReadJsonCallback) => void; + +// @public (undocumented) +type ReadJsonCallback = (err: NodeJS.ErrnoException | Error | null, data?: JsonObject) => void; + +// @public (undocumented) +type ReadJsonSync = (path: PathOrFileDescriptor) => JsonObject; + +// @public (undocumented) +type Readlink = { + (path: PathLike, options: EncodingOption, callback: StringCallback): void; + (path: PathLike, options: BufferEncodingOption, callback: BufferCallback): void; + (path: PathLike, options: EncodingOption, callback: StringOrBufferCallback): void; + (path: PathLike, callback: StringCallback): void; +}; + +// @public (undocumented) +type ReadlinkSync = { + (path: PathLike, options: EncodingOption): string; + (path: PathLike, options: BufferEncodingOption): Buffer; + (path: PathLike, options: EncodingOption): string | Buffer; +}; + +// @public (undocumented) +type RealPath = { + (path: PathLike, options: EncodingOption, callback: StringCallback): void; + (path: PathLike, options: BufferEncodingOption, callback: BufferCallback): void; + (path: PathLike, options: EncodingOption, callback: StringOrBufferCallback): void; + (path: PathLike, callback: StringCallback): void; +}; + +// @public (undocumented) +type RealPathSync = { + (path: PathLike, options?: EncodingOption): string; + (path: PathLike, options: BufferEncodingOption): Buffer; + (path: PathLike, options?: EncodingOption): string | Buffer; +}; + // @public (undocumented) type RecursiveArrayOrRecord = { [index: string]: RecursiveArrayOrRecord; @@ -14271,6 +14509,23 @@ class SplitChunksPlugin extends RspackBuiltinPlugin { raw(compiler: Compiler): BuiltinPlugin; } +// @public (undocumented) +type Stat = { + (path: PathLike, callback: StatsCallback): void; + (path: PathLike, options: (StatOptions & { + bigint?: false; + }) | undefined, callback: StatsCallback): void; + (path: PathLike, options: StatOptions & { + bigint: true; + }, callback: BigIntStatsCallback): void; + (path: PathLike, options: StatOptions | undefined, callback: StatsOrBigIntStatsCallback): void; +}; + +// @public (undocumented) +type StatOptions = { + bigint?: boolean; +}; + // @public (undocumented) export class Stats { constructor(compilation: Compilation); @@ -14295,6 +14550,9 @@ export class Stats { // @public (undocumented) export type StatsAsset = KnownStatsAsset & Record; +// @public (undocumented) +type StatsCallback = (err: NodeJS.ErrnoException | null, stats?: IStats) => void; + // @public (undocumented) export type StatsChunk = KnownStatsChunk & Record; @@ -14592,6 +14850,9 @@ const statsOptions: z.ZodObject<{ warningsSpace?: number | undefined; }>; +// @public (undocumented) +type StatsOrBigIntStatsCallback = (err: NodeJS.ErrnoException | null, stats?: IStats | IBigIntStats) => void; + // @public (undocumented) class StatsPrinter { constructor(); @@ -14856,6 +15117,36 @@ const statsValue: z.ZodUnion<[z.ZodUnion<[z.ZodBoolean, z.ZodEnum<["normal", "no warningsSpace?: number | undefined; }>]>; +// @public (undocumented) +type StatSync = { + (path: PathLike, options?: undefined): IStats; + (path: PathLike, options?: StatSyncOptions & { + bigint?: false; + throwIfNoEntry: false; + }): IStats | undefined; + (path: PathLike, options: StatSyncOptions & { + bigint: true; + throwIfNoEntry: false; + }): IBigIntStats | undefined; + (path: PathLike, options?: StatSyncOptions & { + bigint?: false; + }): IStats; + (path: PathLike, options: StatSyncOptions & { + bigint: true; + }): IBigIntStats; + (path: PathLike, options: StatSyncOptions & { + bigint: boolean; + throwIfNoEntry?: false; + }): IStats | IBigIntStats; + (path: PathLike, options?: StatSyncOptions): IStats | IBigIntStats | undefined; +}; + +// @public (undocumented) +type StatSyncOptions = { + bigint?: boolean; + throwIfNoEntry?: boolean; +}; + // @public (undocumented) export type StrictModuleErrorHandling = z.infer; @@ -14868,6 +15159,12 @@ export type StrictModuleExceptionHandling = z.infer void; + +// @public (undocumented) +type StringOrBufferCallback = (err: NodeJS.ErrnoException | null, data?: string | Buffer) => void; + // @public (undocumented) export const SwcJsMinimizerRspackPlugin: { new (options?: SwcJsMinimizerRspackPluginOptions | undefined): { diff --git a/packages/rspack/src/Compilation.ts b/packages/rspack/src/Compilation.ts index b593ca522ebd..934cd3e163a6 100644 --- a/packages/rspack/src/Compilation.ts +++ b/packages/rspack/src/Compilation.ts @@ -50,6 +50,7 @@ import { StatsPrinter } from "./stats/StatsPrinter"; import { type AssetInfo, JsAssetInfo } from "./util/AssetInfo"; import MergeCaller from "./util/MergeCaller"; import { createFakeCompilationDependencies } from "./util/fake"; +import type { InputFileSystem } from "./util/fs"; import type Hash from "./util/hash"; import { memoizeValue } from "./util/memoize"; import { JsSource } from "./util/source"; @@ -224,7 +225,7 @@ export class Compilation { compiler: Compiler; resolverFactory: ResolverFactory; - inputFileSystem: any; + inputFileSystem: InputFileSystem | null; options: RspackOptionsNormalized; outputOptions: OutputNormalized; logging: Map; diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index aa5fccb6118e..939f834b1850 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -63,7 +63,11 @@ import type { RspackOptionsNormalized, RspackPluginInstance } from "./config"; -import type { OutputFileSystem, WatchFileSystem } from "./util/fs"; +import type { + InputFileSystem, + OutputFileSystem, + WatchFileSystem +} from "./util/fs"; export interface AssetEmittedInfo { content: Buffer; @@ -134,7 +138,7 @@ class Compiler { infrastructureLogger: any; watching?: Watching; - inputFileSystem: any; + inputFileSystem: InputFileSystem | null; intermediateFileSystem: any; outputFileSystem: OutputFileSystem | null; watchFileSystem: WatchFileSystem | null; @@ -508,9 +512,7 @@ class Compiler { } purgeInputFileSystem() { - if (this.inputFileSystem?.purge) { - this.inputFileSystem.purge(); - } + this.inputFileSystem?.purge?.(); } /** diff --git a/packages/rspack/src/MultiCompiler.ts b/packages/rspack/src/MultiCompiler.ts index 93bc3f9414a4..b8377b8368a2 100644 --- a/packages/rspack/src/MultiCompiler.ts +++ b/packages/rspack/src/MultiCompiler.ts @@ -16,7 +16,7 @@ import MultiWatching from "./MultiWatching"; import type { WatchOptions } from "./config"; import ConcurrentCompilationError from "./error/ConcurrentCompilationError"; import ArrayQueue from "./util/ArrayQueue"; -import type { WatchFileSystem } from "./util/fs"; +import type { InputFileSystem, WatchFileSystem } from "./util/fs"; interface Node { compiler: Compiler; @@ -162,7 +162,7 @@ export class MultiCompiler { throw new Error("Cannot read outputFileSystem of a MultiCompiler"); } - set inputFileSystem(value) { + set inputFileSystem(value: InputFileSystem) { for (const compiler of this.compilers) { compiler.inputFileSystem = value; } @@ -540,9 +540,7 @@ export class MultiCompiler { purgeInputFileSystem() { for (const compiler of this.compilers) { - if (compiler.inputFileSystem?.purge) { - compiler.inputFileSystem.purge(); - } + compiler.inputFileSystem?.purge?.(); } } diff --git a/packages/rspack/src/node/NodeEnvironmentPlugin.ts b/packages/rspack/src/node/NodeEnvironmentPlugin.ts index ec76bbee36b5..9828e0f3711c 100644 --- a/packages/rspack/src/node/NodeEnvironmentPlugin.ts +++ b/packages/rspack/src/node/NodeEnvironmentPlugin.ts @@ -44,17 +44,16 @@ export default class NodeEnvironmentPlugin { stream: infrastructureLogging.stream! }) as LoggerConsole) }); - compiler.inputFileSystem = new CachedInputFileSystem(fs, 60000); - const inputFileSystem = compiler.inputFileSystem; + + const inputFileSystem = new CachedInputFileSystem(fs, 60000); + compiler.inputFileSystem = inputFileSystem; compiler.outputFileSystem = fs; compiler.intermediateFileSystem = fs; - compiler.watchFileSystem = new NodeWatchFileSystem( - compiler.inputFileSystem - ); + compiler.watchFileSystem = new NodeWatchFileSystem(inputFileSystem); compiler.hooks.beforeRun.tap("NodeEnvironmentPlugin", compiler => { if (compiler.inputFileSystem === inputFileSystem) { compiler.fsStartTime = Date.now(); - inputFileSystem.purge(); + inputFileSystem?.purge(); } }); } diff --git a/packages/rspack/src/node/NodeWatchFileSystem.ts b/packages/rspack/src/node/NodeWatchFileSystem.ts index a31ab687abcd..1afb9b2b4160 100644 --- a/packages/rspack/src/node/NodeWatchFileSystem.ts +++ b/packages/rspack/src/node/NodeWatchFileSystem.ts @@ -131,7 +131,7 @@ export default class NodeWatchFileSystem implements WatchFileSystem { getAggregatedRemovals: util.deprecate( () => { const items = this.watcher?.aggregatedRemovals; - if (items && this.inputFileSystem && this.inputFileSystem.purge) { + if (items && this.inputFileSystem?.purge) { const fs = this.inputFileSystem; for (const item of items) { fs.purge?.(item); @@ -145,7 +145,7 @@ export default class NodeWatchFileSystem implements WatchFileSystem { getAggregatedChanges: util.deprecate( () => { const items = this.watcher?.aggregatedChanges; - if (items && this.inputFileSystem && this.inputFileSystem.purge) { + if (items && this.inputFileSystem?.purge) { const fs = this.inputFileSystem; for (const item of items) { fs.purge?.(item);