1 |
|
2 |
|
3 | import { Server } from "http";
|
4 | import { AddressInfo } from "net";
|
5 | import { Compiler, Stats as WebpackStats, StatsOptions, WebpackPluginInstance } from "webpack";
|
6 |
|
7 | export namespace BundleAnalyzerPlugin {
|
8 |
|
9 |
|
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 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | analyzerMode?: "server" | "static" | "json" | "disabled" | undefined;
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 | analyzerHost?: string | undefined;
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 | analyzerPort?: number | "auto" | undefined;
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 | analyzerUrl?: (options: { listenPort: string; listenHost: string; boundAddress: AddressInfo }) => string;
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 | reportFilename?: string | undefined;
|
64 |
|
65 | |
66 |
|
67 |
|
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 |
|
143 | export 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 |