UNPKG

6.05 kBTypeScriptView Raw
1// Type definitions for webpack-bundle-analyzer 4.4
2// Project: https://github.com/webpack-contrib/webpack-bundle-analyzer
3// Definitions by: Michael Strobel <https://github.com/kryops>
4// Vladimir Grenaderov <https://github.com/VladimirGrenaderov>
5// Max Boguslavskiy <https://github.com/maxbogus>
6// Piotr Błażejewicz <https://github.com/peterblazejewicz>
7// Kyle Hensel <https://github.com/k-yle>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 3.7
10
11/// <reference types="node" />
12
13import { Server } from 'http';
14import { WebpackPluginInstance, Compiler, StatsOptions, Stats as WebpackStats } from 'webpack';
15
16export namespace BundleAnalyzerPlugin {
17 // Copied from @types/webpack@4 as webpack@5 only has `any` defined at the moment.
18 // See https://github.com/webpack/webpack/issues/11630
19 namespace Stats {
20 type Preset = boolean | 'errors-only' | 'errors-warnings' | 'minimal' | 'none' | 'normal' | 'verbose';
21
22 type ToJsonOptionsObject = StatsOptions;
23
24 type ToJsonOptions = Preset | ToJsonOptionsObject;
25
26 type StatsExcludeFilter =
27 | string
28 | string[]
29 | RegExp
30 | RegExp[]
31 | ((assetName: string) => boolean)
32 | Array<(assetName: string) => boolean>;
33 }
34
35 type ExcludeAssetsPatternFn = (assetName: string) => boolean;
36 type ExcludeAssetsPattern = string | RegExp | ExcludeAssetsPatternFn;
37
38 interface Options {
39 /**
40 * Can be "server", "static" or "disabled".
41 * Defaults to "server".
42 * In "server" mode analyzer will start HTTP server to show bundle report.
43 * In "static" mode single HTML file with bundle report will be generated.
44 * In "json" mode single JSON file with bundle report will be generated
45 * In "disabled" mode you can use this plugin to just generate Webpack Stats JSON file by setting "generateStatsFile" to true.
46 */
47 analyzerMode?: 'server' | 'static' | 'json' | 'disabled' | undefined;
48
49 /**
50 * Host that will be used in `server` mode to start HTTP server.
51 * @default '127.0.0.1'
52 */
53 analyzerHost?: string | undefined;
54
55 /**
56 * Port that will be used in `server` mode to start HTTP server.
57 * @default 8888
58 */
59 analyzerPort?: number | 'auto' | undefined;
60
61 /**
62 * Path to bundle report file that will be generated in "static" mode.
63 * Relative to bundles output directory.
64 * @default 'report.html'
65 */
66 reportFilename?: string | undefined;
67
68 /**
69 * Content of the HTML title element; or a function of the form () => string that provides the content.
70 * @default function that returns pretty printed current date and time.
71 */
72 reportTitle?: string | (() => string) | undefined;
73
74 /**
75 * Module sizes to show in report by default.
76 * Should be one of "stat", "parsed" or "gzip".
77 * @default 'parsed'
78 */
79 defaultSizes?: 'parsed' | 'stat' | 'gzip' | undefined;
80
81 /**
82 * Automatically open report in default browser.
83 * @default true
84 */
85 openAnalyzer?: boolean | undefined;
86
87 /**
88 * If true, Webpack Stats JSON file will be generated in bundles output directory.
89 * @default false
90 */
91 generateStatsFile?: boolean | undefined;
92
93 /**
94 * Name of Webpack Stats JSON file that will be generated if generateStatsFile is true.
95 * Relative to bundles output directory.
96 * @default 'stats.json'
97 */
98 statsFilename?: string | undefined;
99
100 /**
101 * Options for stats.toJson() method.
102 * For example you can exclude sources of your modules from stats file with "source: false" option.
103 * @default null
104 */
105 statsOptions?: null | Stats.ToJsonOptions | undefined;
106
107 /**
108 * Patterns that will be used to match against asset names to exclude them from the report.
109 * If pattern is a string it will be converted to RegExp via `new RegExp(str)`.
110 * If pattern is a function it should have the following signature `(assetName: string) => boolean`
111 * and should return true to exclude matching asset.
112 * If multiple patterns are provided asset should match at least one of them to be excluded.
113 * @default null
114 */
115 excludeAssets?: null | ExcludeAssetsPattern | ExcludeAssetsPattern[] | undefined;
116
117 /**
118 * Log level. Can be "info", "warn", "error" or "silent".
119 * @default 'info'
120 */
121 logLevel?: 'info' | 'warn' | 'error' | 'silent' | undefined;
122 }
123
124 interface JsonReportItem {
125 label: string;
126 /** in bytes */
127 gzipSize: number;
128 concatenated?: boolean | undefined;
129 /** in bytes */
130 statSize: number;
131 /** in bytes */
132 parsedSize: number;
133
134 groups?: JsonReportItem[] | undefined;
135 path?: string | undefined;
136 inaccurateSizes?: boolean | undefined;
137 id?: number | null | undefined;
138 isAsset?: boolean | undefined;
139 }
140
141 /** The json report that will be produced if `analyzerMode: 'json'` */
142 type JsonReport = JsonReportItem[];
143}
144
145export class BundleAnalyzerPlugin implements WebpackPluginInstance {
146 opts: BundleAnalyzerPlugin.Options;
147 compiler?: Compiler;
148 server: null | Server;
149
150 constructor(options?: BundleAnalyzerPlugin.Options);
151
152 apply(compiler: Compiler): void;
153 /** @async */
154 startAnalyzerServer: (stats: WebpackStats) => Promise<void>;
155 /** @async */
156 generateJSONReport: (stats: WebpackStats) => Promise<void>;
157 generateStatsFile: (stats: WebpackStats) => Promise<void>;
158 /** @async */
159 generateStaticReport: (stats: WebpackStats) => Promise<void>;
160 getBundleDirFromCompiler: () => null | string;
161}
162
\No newline at end of file