From 069f404e6611098c620a265d05782db36efb4fc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Sat, 18 Apr 2020 19:47:32 +0200 Subject: [PATCH] fix(webpack): correct Stats class details (#43971) * fix(webpack): correct Stats class details - remove abstract keyword for Stats class - add constructor with options - add class and instance methods - update tests https://github.com/webpack/webpack/blob/71eb5931dbd7db125109a95692065333cea936a9/lib/Stats.js /cc @ernestostifano Thanks! Fixes #43952 * Update types/webpack/test/index.ts Thx to @ofhouse for review of this part! Co-Authored-By: Felix Haus * Update types/webpack/index.d.ts Thx to @ofhouse for reviewing and suggestion! Co-Authored-By: Felix Haus * Correct `filterWarnings` after method params redefinition Missed when accepting the suggestion as part of PR review Thanks! Co-authored-by: Felix Haus --- types/webpack/index.d.ts | 16 +++++++++++++++- types/webpack/test/index.ts | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/types/webpack/index.d.ts b/types/webpack/index.d.ts index 97ab0d1324..5e2de7e933 100644 --- a/types/webpack/index.d.ts +++ b/types/webpack/index.d.ts @@ -22,6 +22,7 @@ // Daniel Chin // Daiki Ihara // Dion Shi +// Piotr Błażejewicz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -1409,20 +1410,33 @@ declare namespace webpack { apply(resolver: any /* EnhancedResolve.Resolver */): void; } - abstract class Stats { + class Stats { compilation: compilation.Compilation; hash?: string; startTime?: number; endTime?: number; + + static filterWarnings( + warnings: string[], + warningsFilter?: Array boolean)> + ): string[]; /** * Returns the default json options from the stats preset. * @param preset The preset to be transformed into json options. */ static presetToOptions(preset?: Stats.Preset): Stats.ToJsonOptionsObject; + + constructor(compilation: compilation.Compilation); + + formatFilePath(filePath: string): string; /** Returns true if there were errors while compiling. */ hasErrors(): boolean; /** Returns true if there were warnings while compiling. */ hasWarnings(): boolean; + /** Remove a prefixed "!" that can be specified to reverse sort order */ + normalizeFieldKey(field: string): string; + /** if a field is prefixed by a "!" reverse sort order */ + sortOrderRegular(field: string): boolean; /** Returns compilation information as a JSON object. */ toJson(options?: Stats.ToJsonOptions, forToString?: boolean): Stats.ToJsonOutput; /** Returns a formatted string of the compilation information (similar to CLI output). */ diff --git a/types/webpack/test/index.ts b/types/webpack/test/index.ts index 4e17f50c26..5aaa9e3949 100644 --- a/types/webpack/test/index.ts +++ b/types/webpack/test/index.ts @@ -1166,3 +1166,14 @@ class LoggingPlugin extends webpack.Plugin { }); } } + +// Stats Microsoft/DefinitelyTyped#43952 +const { Stats } = webpack; + +compiler.hooks.compilation.tap('SomePlugin', compilation => { + const stats = new Stats(compilation); // $ExpectType Stats + Stats.filterWarnings([], [(warning: string) => true]); // $ExpectType string[] + stats.formatFilePath('app/src/'); // $ExpectType string + stats.normalizeFieldKey('field'); // $ExpectType string + stats.sortOrderRegular('!field'); // $ExpectType boolean +});