refactor: more types

This commit is contained in:
alexander.akait 2024-02-16 21:03:51 +03:00
parent 1643e5b1a9
commit 2732c57143
3 changed files with 123 additions and 83 deletions

View File

@ -49,10 +49,6 @@ const makeSerializable = require("./util/makeSerializable");
const memoize = require("./util/memoize"); const memoize = require("./util/memoize");
/** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("webpack-sources").Source} Source */
/**
* @template T
* @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} NormalModuleLoaderContext
**/
/** @typedef {import("../declarations/WebpackOptions").Mode} Mode */ /** @typedef {import("../declarations/WebpackOptions").Mode} Mode */
/** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */ /** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
@ -548,7 +544,7 @@ class NormalModule extends Module {
* @param {Compilation} compilation the compilation * @param {Compilation} compilation the compilation
* @param {InputFileSystem} fs file system from reading * @param {InputFileSystem} fs file system from reading
* @param {NormalModuleCompilationHooks} hooks the hooks * @param {NormalModuleCompilationHooks} hooks the hooks
* @returns {NormalModuleLoaderContext<T>} loader context * @returns {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} loader context
*/ */
_createLoaderContext(resolver, options, compilation, fs, hooks) { _createLoaderContext(resolver, options, compilation, fs, hooks) {
const { requestShortener } = compilation.runtimeTemplate; const { requestShortener } = compilation.runtimeTemplate;
@ -614,7 +610,7 @@ class NormalModule extends Module {
return createHash(type || compilation.outputOptions.hashFunction); return createHash(type || compilation.outputOptions.hashFunction);
} }
}; };
/** @type {NormalModuleLoaderContext<T>} */ /** @type {import("../declarations/LoaderContext").NormalModuleLoaderContext<T>} */
const loaderContext = { const loaderContext = {
version: 2, version: 2,
getOptions: schema => { getOptions: schema => {

View File

@ -919,7 +919,7 @@ class NormalModuleFactory extends ModuleFactory {
* @param {string} unresolvedResource unresolved resource * @param {string} unresolvedResource unresolved resource
* @param {ResolverWithOptions} resolver resolver * @param {ResolverWithOptions} resolver resolver
* @param {ResolveContext} resolveContext resolver context * @param {ResolveContext} resolveContext resolver context
* @param {Parameters<import("enhanced-resolve")>[2]} callback callback * @param {(err: null | Error, res?: string | false, req?: ResolveRequest) => void} callback callback
*/ */
resolveResource( resolveResource(
contextInfo, contextInfo,

196
types.d.ts vendored
View File

@ -817,9 +817,7 @@ declare abstract class BasicEvaluatedExpression {
| TemplateElement | TemplateElement
): BasicEvaluatedExpression; ): BasicEvaluatedExpression;
} }
declare interface BuildInfo { type BuildInfo = KnownBuildInfo & Record<string, any>;
[index: string]: any;
}
type BuildMeta = KnownBuildMeta & Record<string, any>; type BuildMeta = KnownBuildMeta & Record<string, any>;
declare abstract class ByTypeGenerator extends Generator { declare abstract class ByTypeGenerator extends Generator {
map: Record<string, Generator>; map: Record<string, Generator>;
@ -958,15 +956,15 @@ declare interface CallbackAsyncQueue<T> {
declare interface CallbackCache<T> { declare interface CallbackCache<T> {
(err?: null | WebpackError, result?: T): void; (err?: null | WebpackError, result?: T): void;
} }
declare interface CallbackFunction<T> { declare interface CallbackFunction_1<T> {
(err?: null | Error, result?: T): any; (err?: null | Error, result?: T): any;
} }
declare interface CallbackFunction_2<T> {
(err?: null | Error, stats?: T): void;
}
declare interface CallbackNormalErrorCache<T> { declare interface CallbackNormalErrorCache<T> {
(err?: null | Error, result?: T): void; (err?: null | Error, result?: T): void;
} }
declare interface CallbackWebpack<T> {
(err?: Error, stats?: T): void;
}
type Cell<T> = undefined | T; type Cell<T> = undefined | T;
declare class Chunk { declare class Chunk {
constructor(name?: string, backCompat?: boolean); constructor(name?: string, backCompat?: boolean);
@ -2278,8 +2276,11 @@ declare class Compiler {
watchMode: boolean; watchMode: boolean;
getCache(name: string): CacheFacade; getCache(name: string): CacheFacade;
getInfrastructureLogger(name: string | (() => string)): WebpackLogger; getInfrastructureLogger(name: string | (() => string)): WebpackLogger;
watch(watchOptions: WatchOptions, handler: CallbackFunction<Stats>): Watching; watch(
run(callback: CallbackFunction<Stats>): void; watchOptions: WatchOptions,
handler: CallbackFunction_1<Stats>
): Watching;
run(callback: CallbackFunction_1<Stats>): void;
runAsChild( runAsChild(
callback: ( callback: (
err?: null | Error, err?: null | Error,
@ -2288,9 +2289,12 @@ declare class Compiler {
) => any ) => any
): void; ): void;
purgeInputFileSystem(): void; purgeInputFileSystem(): void;
emitAssets(compilation: Compilation, callback: CallbackFunction<void>): void; emitAssets(
emitRecords(callback: CallbackFunction<void>): void; compilation: Compilation,
readRecords(callback: CallbackFunction<void>): void; callback: CallbackFunction_1<void>
): void;
emitRecords(callback: CallbackFunction_1<void>): void;
readRecords(callback: CallbackFunction_1<void>): void;
createChildCompiler( createChildCompiler(
compilation: Compilation, compilation: Compilation,
compilerName: string, compilerName: string,
@ -2307,8 +2311,8 @@ declare class Compiler {
normalModuleFactory: NormalModuleFactory; normalModuleFactory: NormalModuleFactory;
contextModuleFactory: ContextModuleFactory; contextModuleFactory: ContextModuleFactory;
}; };
compile(callback: CallbackFunction<Compilation>): void; compile(callback: CallbackFunction_1<Compilation>): void;
close(callback: CallbackFunction<void>): void; close(callback: CallbackFunction_1<void>): void;
} }
declare class ConcatSource extends Source { declare class ConcatSource extends Source {
constructor(...args: (string | Source)[]); constructor(...args: (string | Source)[]);
@ -4822,6 +4826,9 @@ declare class Generator {
updateHash(hash: Hash, __1: UpdateHashContextGenerator): void; updateHash(hash: Hash, __1: UpdateHashContextGenerator): void;
static byType(map: Record<string, Generator>): ByTypeGenerator; static byType(map: Record<string, Generator>): ByTypeGenerator;
} }
declare interface GeneratorOptions {
[index: string]: any;
}
type GeneratorOptionsByModuleType = GeneratorOptionsByModuleTypeKnown & type GeneratorOptionsByModuleType = GeneratorOptionsByModuleTypeKnown &
GeneratorOptionsByModuleTypeUnknown; GeneratorOptionsByModuleTypeUnknown;
@ -6588,6 +6595,19 @@ declare interface KnownAssetInfo {
*/ */
related?: Record<string, string | string[]>; related?: Record<string, string | string[]>;
} }
declare interface KnownBuildInfo {
cacheable?: boolean;
parsed?: boolean;
fileDependencies?: LazySet<string>;
contextDependencies?: LazySet<string>;
missingDependencies?: LazySet<string>;
buildDependencies?: LazySet<string>;
valueDependencies?: Map<string, string | Set<string>>;
hash?: any;
assets?: Record<string, Source>;
assetsInfo?: Map<string, undefined | AssetInfo>;
snapshot?: null | Snapshot;
}
declare interface KnownBuildMeta { declare interface KnownBuildMeta {
moduleArgument?: string; moduleArgument?: string;
exportsArgument?: string; exportsArgument?: string;
@ -7717,7 +7737,7 @@ declare class Module extends DependenciesBlock {
* Module should be unsafe cached. Get data that's needed for that. * Module should be unsafe cached. Get data that's needed for that.
* This data will be passed to restoreFromUnsafeCache later. * This data will be passed to restoreFromUnsafeCache later.
*/ */
getUnsafeCacheData(): object; getUnsafeCacheData(): UnsafeCacheData;
/** /**
* Assuming this module is in the cache. Remove internal references to allow freeing some memory. * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
@ -8283,21 +8303,21 @@ declare class MultiCompiler {
outputFileSystem: OutputFileSystem; outputFileSystem: OutputFileSystem;
watchFileSystem: WatchFileSystem; watchFileSystem: WatchFileSystem;
intermediateFileSystem: IntermediateFileSystem; intermediateFileSystem: IntermediateFileSystem;
getInfrastructureLogger(name?: any): WebpackLogger; getInfrastructureLogger(name: string | (() => string)): WebpackLogger;
setDependencies(compiler: Compiler, dependencies: string[]): void; setDependencies(compiler: Compiler, dependencies: string[]): void;
validateDependencies(callback: CallbackFunction<MultiStats>): boolean; validateDependencies(callback: CallbackFunction_1<MultiStats>): boolean;
runWithDependencies( runWithDependencies(
compilers: Compiler[], compilers: Compiler[],
fn: (compiler: Compiler, callback: CallbackFunction<MultiStats>) => any, fn: (compiler: Compiler, callback: CallbackFunction_1<MultiStats>) => any,
callback: CallbackFunction<MultiStats> callback: CallbackFunction_1<MultiStats>
): void; ): void;
watch( watch(
watchOptions: WatchOptions | WatchOptions[], watchOptions: WatchOptions | WatchOptions[],
handler: CallbackFunction<MultiStats> handler: CallbackFunction_1<MultiStats>
): MultiWatching; ): MultiWatching;
run(callback: CallbackFunction<MultiStats>): void; run(callback: CallbackFunction_1<MultiStats>): void;
purgeInputFileSystem(): void; purgeInputFileSystem(): void;
close(callback: CallbackFunction<void>): void; close(callback: CallbackFunction_1<void>): void;
} }
declare interface MultiCompilerOptions { declare interface MultiCompilerOptions {
/** /**
@ -8316,10 +8336,10 @@ declare abstract class MultiStats {
declare abstract class MultiWatching { declare abstract class MultiWatching {
watchings: Watching[]; watchings: Watching[];
compiler: MultiCompiler; compiler: MultiCompiler;
invalidate(callback?: CallbackFunction<void>): void; invalidate(callback?: CallbackFunction_1<void>): void;
suspend(): void; suspend(): void;
resume(): void; resume(): void;
close(callback: CallbackFunction<void>): void; close(callback: CallbackFunction_1<void>): void;
} }
declare class NamedChunkIdsPlugin { declare class NamedChunkIdsPlugin {
constructor(options?: NamedChunkIdsPluginOptions); constructor(options?: NamedChunkIdsPluginOptions);
@ -8454,40 +8474,44 @@ declare class NormalModule extends Module {
userRequest: string; userRequest: string;
rawRequest: string; rawRequest: string;
binary: boolean; binary: boolean;
parser: Parser; parser?: Parser;
parserOptions?: Record<string, any>; parserOptions?: ParserOptions;
generator: Generator; generator?: Generator;
generatorOptions?: Record<string, any>; generatorOptions?: GeneratorOptions;
resource: string; resource: string;
resourceResolveData?: Record<string, any>; resourceResolveData?: Record<string, any>;
matchResource?: string; matchResource?: string;
loaders: LoaderItem[]; loaders: LoaderItem[];
error?: null | WebpackError; error: null | WebpackError;
/**
* restore unsafe cache data
*/
restoreFromUnsafeCache( restoreFromUnsafeCache(
unsafeCacheData?: any, unsafeCacheData: NormalModuleUnsafeCacheData,
normalModuleFactory?: any normalModuleFactory: NormalModuleFactory
): void; ): void;
createSourceForAsset( createSourceForAsset(
context: string, context: string,
name: string, name: string,
content: string, content: string | Buffer,
sourceMap?: any, sourceMap?: string | SourceMap,
associatedObjectForCache?: Object associatedObjectForCache?: Object
): Source; ): Source;
getCurrentLoader(loaderContext?: any, index?: any): null | LoaderItem; getCurrentLoader(loaderContext?: any, index?: number): null | LoaderItem;
createSource( createSource(
context: string, context: string,
content: string | Buffer, content: string | Buffer,
sourceMap?: any, sourceMap?: string | SourceMapSource,
associatedObjectForCache?: Object associatedObjectForCache?: Object
): Source; ): Source;
markModuleAsErrored(error: WebpackError): void; markModuleAsErrored(error: WebpackError): void;
applyNoParseRule(rule?: any, content?: any): any; applyNoParseRule(rule: any, content: string): boolean;
shouldPreventParsing(noParseRule?: any, request?: any): any; shouldPreventParsing(noParseRule: any, request: string): boolean;
static getCompilationHooks( static getCompilationHooks(
compilation: Compilation compilation: Compilation
): NormalModuleCompilationHooks; ): NormalModuleCompilationHooks;
static deserialize(context?: any): NormalModule; static deserialize(context: ObjectDeserializerContext): NormalModule;
} }
declare interface NormalModuleCompilationHooks { declare interface NormalModuleCompilationHooks {
loader: SyncHook<[object, NormalModule]>; loader: SyncHook<[object, NormalModule]>;
@ -8559,7 +8583,7 @@ declare interface NormalModuleCreateData {
/** /**
* the options of the parser used * the options of the parser used
*/ */
parserOptions?: Record<string, any>; parserOptions?: ParserOptions;
/** /**
* the generator used * the generator used
@ -8569,7 +8593,7 @@ declare interface NormalModuleCreateData {
/** /**
* the options of the generator used * the options of the generator used
*/ */
generatorOptions?: Record<string, any>; generatorOptions?: GeneratorOptions;
/** /**
* options used for resolving requests from this module * options used for resolving requests from this module
@ -8603,40 +8627,47 @@ declare abstract class NormalModuleFactory extends ModuleFactory {
], ],
Module Module
>; >;
createParser: HookMap<SyncBailHook<any, any>>; createParser: HookMap<SyncBailHook<[ParserOptions], Parser>>;
parser: HookMap<SyncHook<any>>; parser: HookMap<SyncBailHook<[any, ParserOptions], void>>;
createGenerator: HookMap<SyncBailHook<any, any>>; createGenerator: HookMap<SyncBailHook<[GeneratorOptions], Generator>>;
generator: HookMap<SyncHook<any>>; generator: HookMap<SyncBailHook<[any, GeneratorOptions], void>>;
createModuleClass: HookMap<SyncBailHook<any, any>>; createModuleClass: HookMap<SyncBailHook<[any, ResolveData], Module>>;
}>; }>;
resolverFactory: ResolverFactory; resolverFactory: ResolverFactory;
ruleSet: RuleSet; ruleSet: RuleSet;
context: string; context: string;
fs: InputFileSystem; fs: InputFileSystem;
parserCache: Map<string, WeakMap<Object, any>>; parserCache: Map<string, WeakMap<Object, Parser>>;
generatorCache: Map<string, WeakMap<Object, Generator>>; generatorCache: Map<string, WeakMap<Object, Generator>>;
cleanupForCache(): void; cleanupForCache(): void;
resolveResource( resolveResource(
contextInfo?: any, contextInfo: ModuleFactoryCreateDataContextInfo,
context?: any, context: string,
unresolvedResource?: any, unresolvedResource: string,
resolver?: any, resolver: ResolverWithOptions,
resolveContext?: any, resolveContext: ResolveContext,
callback?: any callback: (
err: null | Error,
res?: string | false,
req?: ResolveRequest
) => void
): void; ): void;
resolveRequestArray( resolveRequestArray(
contextInfo?: any, contextInfo: ModuleFactoryCreateDataContextInfo,
context?: any, context: string,
array?: any, array: LoaderItem[],
resolver?: any, resolver: ResolverWithOptions,
resolveContext?: any, resolveContext: ResolveContext,
callback?: any callback: CallbackFunction_2<LoaderItem[]>
): any; ): void;
getParser(type?: any, parserOptions?: object): any; getParser(type: string, parserOptions?: ParserOptions): Parser;
createParser(type: string, parserOptions?: { [index: string]: any }): Parser; createParser(type: string, parserOptions?: ParserOptions): Parser;
getGenerator(type?: any, generatorOptions?: object): undefined | Generator; getGenerator(type: string, generatorOptions?: GeneratorOptions): Generator;
createGenerator(type?: any, generatorOptions?: object): any; createGenerator(type: string, generatorOptions?: GeneratorOptions): Generator;
getResolver(type?: any, resolveOptions?: any): ResolverWithOptions; getResolver(
type: string,
resolveOptions?: ResolveOptionsWithDependencyType
): ResolverWithOptions;
} }
/** /**
@ -8680,7 +8711,7 @@ declare interface NormalModuleLoaderContext<OptionsType> {
utils: { utils: {
absolutify: (context: string, request: string) => string; absolutify: (context: string, request: string) => string;
contextify: (context: string, request: string) => string; contextify: (context: string, request: string) => string;
createHash: (algorithm?: string) => Hash; createHash: (algorithm?: string | typeof Hash) => Hash;
}; };
rootContext: string; rootContext: string;
fs: InputFileSystem; fs: InputFileSystem;
@ -8707,6 +8738,12 @@ declare class NormalModuleReplacementPlugin {
*/ */
apply(compiler: Compiler): void; apply(compiler: Compiler): void;
} }
type NormalModuleUnsafeCacheData = UnsafeCacheData & {
parser?: Parser;
parserOptions?: ParserOptions;
generator?: Generator;
generatorOptions?: GeneratorOptions;
};
type NormalizedStatsOptions = KnownNormalizedStatsOptions & type NormalizedStatsOptions = KnownNormalizedStatsOptions &
Omit< Omit<
StatsOptions, StatsOptions,
@ -9820,6 +9857,9 @@ declare class Parser {
state: ParserState state: ParserState
): ParserState; ): ParserState;
} }
declare interface ParserOptions {
[index: string]: any;
}
type ParserOptionsByModuleType = ParserOptionsByModuleTypeKnown & type ParserOptionsByModuleType = ParserOptionsByModuleTypeKnown &
ParserOptionsByModuleTypeUnknown; ParserOptionsByModuleTypeUnknown;
@ -10854,9 +10894,9 @@ type ResolverWithOptions = Resolver & WithOptions;
declare interface ResourceDataWithData { declare interface ResourceDataWithData {
resource: string; resource: string;
path: string; path?: string;
query: string; query?: string;
fragment: string; fragment?: string;
context?: string; context?: string;
data: Record<string, any>; data: Record<string, any>;
} }
@ -12989,6 +13029,10 @@ declare interface TrustedTypes {
policyName?: string; policyName?: string;
} }
declare const UNDEFINED_MARKER: unique symbol; declare const UNDEFINED_MARKER: unique symbol;
declare interface UnsafeCacheData {
factoryMeta?: FactoryMeta;
resolveOptions?: ResolveOptionsWebpackOptions;
}
declare interface UpdateHashContextDependency { declare interface UpdateHashContextDependency {
chunkGraph: ChunkGraph; chunkGraph: ChunkGraph;
runtime: RuntimeSpec; runtime: RuntimeSpec;
@ -13285,8 +13329,8 @@ declare interface WatcherInfo {
declare abstract class Watching { declare abstract class Watching {
startTime: null | number; startTime: null | number;
invalid: boolean; invalid: boolean;
handler: CallbackFunction<Stats>; handler: CallbackFunction_1<Stats>;
callbacks: CallbackFunction<void>[]; callbacks: CallbackFunction_1<void>[];
closed: boolean; closed: boolean;
suspended: boolean; suspended: boolean;
blocked: boolean; blocked: boolean;
@ -13322,10 +13366,10 @@ declare abstract class Watching {
dirs: Iterable<string>, dirs: Iterable<string>,
missing: Iterable<string> missing: Iterable<string>
): void; ): void;
invalidate(callback?: CallbackFunction<void>): void; invalidate(callback?: CallbackFunction_1<void>): void;
suspend(): void; suspend(): void;
resume(): void; resume(): void;
close(callback: CallbackFunction<void>): void; close(callback: CallbackFunction_1<void>): void;
} }
declare abstract class WeakTupleMap<T extends any[], V> { declare abstract class WeakTupleMap<T extends any[], V> {
set(...args: [T, ...V[]]): void; set(...args: [T, ...V[]]): void;
@ -13725,18 +13769,18 @@ type __Type_2 =
| __Type_2[]); | __Type_2[]);
declare function exports( declare function exports(
options: Configuration, options: Configuration,
callback?: CallbackWebpack<Stats> callback?: CallbackFunction_2<Stats>
): Compiler; ): Compiler;
declare function exports( declare function exports(
options: ReadonlyArray<Configuration> & MultiCompilerOptions, options: ReadonlyArray<Configuration> & MultiCompilerOptions,
callback?: CallbackWebpack<MultiStats> callback?: CallbackFunction_2<MultiStats>
): MultiCompiler; ): MultiCompiler;
declare namespace exports { declare namespace exports {
export const webpack: { export const webpack: {
(options: Configuration, callback?: CallbackWebpack<Stats>): Compiler; (options: Configuration, callback?: CallbackFunction_2<Stats>): Compiler;
( (
options: ReadonlyArray<Configuration> & MultiCompilerOptions, options: ReadonlyArray<Configuration> & MultiCompilerOptions,
callback?: CallbackWebpack<MultiStats> callback?: CallbackFunction_2<MultiStats>
): MultiCompiler; ): MultiCompiler;
}; };
export const validate: (options?: any) => void; export const validate: (options?: any) => void;