From b41d491c034d41801455a828c4d243496a3edc8b Mon Sep 17 00:00:00 2001 From: Michael Strobel Date: Tue, 20 Oct 2020 17:10:13 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#48857=20[webpack-b?= =?UTF-8?q?undle-analyzer]=20webpack@5=20compatibility=20by=20@kryops?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [webpack] Add WebpackPluginInstance interface for compatibility with webpack@5's own types * [webpack-bundle-analyzer] Add reportTitle option for 3.9 * [webpack-bundle-analyzer] webpack@5 compatibility Fixes #48807 * Fix lint error --- types/webpack-bundle-analyzer/index.d.ts | 104 +++++++++++++++++- .../webpack-bundle-analyzer-tests.ts | 2 + types/webpack/index.d.ts | 5 + 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/types/webpack-bundle-analyzer/index.d.ts b/types/webpack-bundle-analyzer/index.d.ts index e54f239e91db53..cb15875fc0edaa 100644 --- a/types/webpack-bundle-analyzer/index.d.ts +++ b/types/webpack-bundle-analyzer/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for webpack-bundle-analyzer 3.8 +// Type definitions for webpack-bundle-analyzer 3.9 // Project: https://github.com/webpack-contrib/webpack-bundle-analyzer // Definitions by: Michael Strobel // Vladimir Grenaderov @@ -6,9 +6,101 @@ // Piotr Błażejewicz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -import { Plugin, Compiler, Stats } from 'webpack'; +import { WebpackPluginInstance, Compiler } from 'webpack'; export namespace BundleAnalyzerPlugin { + // Copied from @types/webpack@4 as webpack@5 only has `any` defined at the moment. + // See https://github.com/webpack/webpack/issues/11630 + namespace Stats { + type Preset + = boolean + | 'errors-only' + | 'errors-warnings' + | 'minimal' + | 'none' + | 'normal' + | 'verbose'; + + interface ToJsonOptionsObject { + /** fallback value for stats options when an option is not defined (has precedence over local webpack defaults) */ + all?: boolean; + /** Add asset Information */ + assets?: boolean; + /** Sort assets by a field */ + assetsSort?: string; + /** Add built at time information */ + builtAt?: boolean; + /** Add information about cached (not built) modules */ + cached?: boolean; + /** Show cached assets (setting this to `false` only shows emitted files) */ + cachedAssets?: boolean; + /** Add children information */ + children?: boolean; + /** Add information about the `namedChunkGroups` */ + chunkGroups?: boolean; + /** Add built modules information to chunk information */ + chunkModules?: boolean; + /** Add the origins of chunks and chunk merging info */ + chunkOrigins?: boolean; + /** Add chunk information (setting this to `false` allows for a less verbose output) */ + chunks?: boolean; + /** Sort the chunks by a field */ + chunksSort?: string; + /** Context directory for request shortening */ + context?: string; + /** Display the distance from the entry point for each module */ + depth?: boolean; + /** Display the entry points with the corresponding bundles */ + entrypoints?: boolean; + /** Add --env information */ + env?: boolean; + /** Add errors */ + errors?: boolean; + /** Add details to errors (like resolving log) */ + errorDetails?: boolean; + /** Exclude assets from being displayed in stats */ + excludeAssets?: StatsExcludeFilter; + /** Exclude modules from being displayed in stats */ + excludeModules?: StatsExcludeFilter; + /** See excludeModules */ + exclude?: StatsExcludeFilter; + /** Add the hash of the compilation */ + hash?: boolean; + /** Set the maximum number of modules to be shown */ + maxModules?: number; + /** Add built modules information */ + modules?: boolean; + /** Sort the modules by a field */ + modulesSort?: string; + /** Show dependencies and origin of warnings/errors */ + moduleTrace?: boolean; + /** Add public path information */ + publicPath?: boolean; + /** Add information about the reasons why modules are included */ + reasons?: boolean; + /** Add the source code of modules */ + source?: boolean; + /** Add timing information */ + timings?: boolean; + /** Add webpack version information */ + version?: boolean; + /** Add warnings */ + warnings?: boolean; + /** Show which exports of a module are used */ + usedExports?: boolean; + /** Filter warnings to be shown */ + warningsFilter?: string | RegExp | Array | ((warning: string) => boolean); + /** Show performance hint when file size exceeds `performance.maxAssetSize` */ + performance?: boolean; + /** Show the exports of the modules */ + providedExports?: boolean; + } + + type ToJsonOptions = Preset | ToJsonOptionsObject; + + type StatsExcludeFilter = string | string[] | RegExp | RegExp[] | ((assetName: string) => boolean) | Array<(assetName: string) => boolean>; + } + type ExcludeAssetsPatternFn = (assetName: string) => boolean; type ExcludeAssetsPattern = string | RegExp | ExcludeAssetsPatternFn; @@ -42,6 +134,12 @@ export namespace BundleAnalyzerPlugin { */ reportFilename?: string; + /** + * Content of the HTML title element; or a function of the form () => string that provides the content. + * @default function that returns pretty printed current date and time. + */ + reportTitle?: string | (() => string); + /** * Module sizes to show in report by default. * Should be one of "stat", "parsed" or "gzip". @@ -93,7 +191,7 @@ export namespace BundleAnalyzerPlugin { } } -export class BundleAnalyzerPlugin extends Plugin { +export class BundleAnalyzerPlugin implements WebpackPluginInstance { constructor(options?: BundleAnalyzerPlugin.Options); apply(compiler: Compiler): void; diff --git a/types/webpack-bundle-analyzer/webpack-bundle-analyzer-tests.ts b/types/webpack-bundle-analyzer/webpack-bundle-analyzer-tests.ts index 4771271fdb643e..60e721dabc4204 100644 --- a/types/webpack-bundle-analyzer/webpack-bundle-analyzer-tests.ts +++ b/types/webpack-bundle-analyzer/webpack-bundle-analyzer-tests.ts @@ -10,12 +10,14 @@ const config: webpack.Configuration = { new BundleAnalyzerPlugin({ analyzerMode: 'json', analyzerPort: 'auto', + reportTitle: () => 'title', }), new BundleAnalyzerPlugin({ analyzerMode: 'server', analyzerHost: '127.0.0.1', analyzerPort: 8888, reportFilename: 'report.html', + reportTitle: 'title', defaultSizes: 'parsed', openAnalyzer: true, generateStatsFile: true, diff --git a/types/webpack/index.d.ts b/types/webpack/index.d.ts index 4132cacaf99423..e6ed898c388c4c 100644 --- a/types/webpack/index.d.ts +++ b/types/webpack/index.d.ts @@ -1447,6 +1447,11 @@ declare namespace webpack { apply(compiler: Compiler): void; } + // Compatibility with webpack@5's own types + // See https://github.com/webpack/webpack/issues/11630 + // tslint:disable-next-line no-empty-interface + interface WebpackPluginInstance extends Plugin {} + abstract class ResolvePlugin implements Tapable.Plugin { apply(resolver: any /* EnhancedResolve.Resolver */): void; }