UNPKG

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