@@ -7,8 +7,54 @@ const { writeStats } = require("./statsUtils");
77const utils = require ( "./utils" ) ;
88const viewer = require ( "./viewer" ) ;
99
10+ /** @typedef {import("net").AddressInfo } AddressInfo */
11+ /** @typedef {import("webpack").Compiler } Compiler */
12+ /** @typedef {import("webpack").OutputFileSystem } OutputFileSystem */
13+ /** @typedef {import("webpack").Stats } Stats */
14+ /** @typedef {import("webpack").StatsOptions } StatsOptions */
15+ /** @typedef {import("webpack").StatsAsset } StatsAsset */
16+ /** @typedef {import("webpack").StatsCompilation } StatsCompilation */
17+ /** @typedef {import("./sizeUtils").Algorithm } CompressionAlgorithm */
18+ /** @typedef {import("./Logger").Level } LogLever */
19+ /** @typedef {import("./viewer").ViewerServerObj } ViewerServerObj */
20+
21+ /** @typedef {string | boolean | StatsOptions } PluginStatsOptions */
22+
23+ // eslint-disable-next-line jsdoc/reject-any-type
24+ /** @typedef {any } EXPECTED_ANY */
25+
26+ /** @typedef {"static" | "json" | "server" | "disabled" } Mode */
27+ /** @typedef {string | RegExp | ((asset: string) => void) } Pattern */
28+ /** @typedef {null | Pattern | Pattern[] } ExcludeAssets */
29+ /** @typedef {"stat" | "parsed" | "gzip" | "brotli" | "zstd" } Sizes */
30+ /** @typedef {string | (() => string) } ReportTitle */
31+ /** @typedef {(options: { listenHost: string, listenPort: number, boundAddress: string | AddressInfo | null }) => string } AnalyzerUrl */
32+
33+ /**
34+ * @typedef {object } Options
35+ * @property {Mode= } analyzerMode analyzer mode
36+ * @property {string= } analyzerHost analyzer host
37+ * @property {"auto" | number= } analyzerPort analyzer port
38+ * @property {CompressionAlgorithm= } compressionAlgorithm compression algorithm
39+ * @property {string | null= } reportFilename report filename
40+ * @property {ReportTitle= } reportTitle report title
41+ * @property {Sizes= } defaultSizes default sizes
42+ * @property {boolean= } openAnalyzer open analyzer
43+ * @property {boolean= } generateStatsFile generate stats file
44+ * @property {string= } statsFilename stats filename
45+ * @property {PluginStatsOptions= } statsOptions stats options
46+ * @property {ExcludeAssets= } excludeAssets exclude assets
47+ * @property {LogLever= } logLevel exclude assets
48+ * @property {boolean= } startAnalyzer start analyzer
49+ * @property {AnalyzerUrl= } analyzerUrl start analyzer
50+ */
51+
1052class BundleAnalyzerPlugin {
53+ /**
54+ * @param {Options= } opts options
55+ */
1156 constructor ( opts = { } ) {
57+ /** @type {Required<Omit<Options, "analyzerPort" | "statsOptions">> & { analyzerPort: number, statsOptions: undefined | PluginStatsOptions } } */
1258 this . opts = {
1359 analyzerMode : "server" ,
1460 analyzerHost : "127.0.0.1" ,
@@ -19,31 +65,38 @@ class BundleAnalyzerPlugin {
1965 openAnalyzer : true ,
2066 generateStatsFile : false ,
2167 statsFilename : "stats.json" ,
22- statsOptions : null ,
68+ statsOptions : undefined ,
2369 excludeAssets : null ,
2470 logLevel : "info" ,
25- // deprecated
71+ // TODO deprecated
2672 startAnalyzer : true ,
2773 analyzerUrl : utils . defaultAnalyzerUrl ,
2874 ...opts ,
2975 analyzerPort :
30- "analyzerPort" in opts
31- ? opts . analyzerPort === "auto"
32- ? 0
33- : opts . analyzerPort
34- : 8888 ,
76+ opts . analyzerPort === "auto" ? 0 : ( opts . analyzerPort ?? 8888 ) ,
3577 } ;
3678
79+ /** @type {Compiler | null } */
80+ this . compiler = null ;
81+ /** @type {Promise<ViewerServerObj> | null } */
3782 this . server = null ;
3883 this . logger = new Logger ( this . opts . logLevel ) ;
3984 }
4085
86+ /**
87+ * @param {Compiler } compiler compiler
88+ */
4189 apply ( compiler ) {
4290 this . compiler = compiler ;
4391
92+ /**
93+ * @param {Stats } stats stats
94+ * @param {(err?: Error) => void } callback callback
95+ */
4496 const done = ( stats , callback ) => {
4597 callback ||= ( ) => { } ;
4698
99+ /** @type {(() => Promise<void>)[] } */
47100 const actions = [ ] ;
48101
49102 if ( this . opts . generateStatsFile ) {
@@ -72,7 +125,7 @@ class BundleAnalyzerPlugin {
72125 await Promise . all ( actions . map ( ( action ) => action ( ) ) ) ;
73126 callback ( ) ;
74127 } catch ( err ) {
75- callback ( err ) ;
128+ callback ( /** @type { Error } */ ( err ) ) ;
76129 }
77130 } ) ;
78131 } else {
@@ -83,13 +136,19 @@ class BundleAnalyzerPlugin {
83136 if ( compiler . hooks ) {
84137 compiler . hooks . done . tapAsync ( "webpack-bundle-analyzer" , done ) ;
85138 } else {
139+ // @ts -expect-error old webpack@4 API
86140 compiler . plugin ( "done" , done ) ;
87141 }
88142 }
89143
144+ /**
145+ * @param {StatsCompilation } stats stats
146+ * @returns {Promise<void> }
147+ */
90148 async generateStatsFile ( stats ) {
91149 const statsFilepath = path . resolve (
92- this . compiler . outputPath ,
150+ /** @type {Compiler } */
151+ ( this . compiler ) . outputPath ,
93152 this . opts . statsFilename ,
94153 ) ;
95154 await fs . promises . mkdir ( path . dirname ( statsFilepath ) , { recursive : true } ) ;
@@ -107,6 +166,10 @@ class BundleAnalyzerPlugin {
107166 }
108167 }
109168
169+ /**
170+ * @param {StatsCompilation } stats stats
171+ * @returns {Promise<void> }
172+ */
110173 async startAnalyzerServer ( stats ) {
111174 if ( this . server ) {
112175 ( await this . server ) . updateChartData ( stats ) ;
@@ -126,10 +189,15 @@ class BundleAnalyzerPlugin {
126189 }
127190 }
128191
192+ /**
193+ * @param {StatsCompilation } stats stats
194+ * @returns {Promise<void> }
195+ */
129196 async generateJSONReport ( stats ) {
130197 await viewer . generateJSONReport ( stats , {
131198 reportFilename : path . resolve (
132- this . compiler . outputPath ,
199+ /** @type {Compiler } */
200+ ( this . compiler ) . outputPath ,
133201 this . opts . reportFilename || "report.json" ,
134202 ) ,
135203 compressionAlgorithm : this . opts . compressionAlgorithm ,
@@ -139,11 +207,16 @@ class BundleAnalyzerPlugin {
139207 } ) ;
140208 }
141209
210+ /**
211+ * @param {StatsCompilation } stats stats
212+ * @returns {Promise<void> }
213+ */
142214 async generateStaticReport ( stats ) {
143215 await viewer . generateReport ( stats , {
144216 openBrowser : this . opts . openAnalyzer ,
145217 reportFilename : path . resolve (
146- this . compiler . outputPath ,
218+ /** @type {Compiler } */
219+ ( this . compiler ) . outputPath ,
147220 this . opts . reportFilename || "report.html" ,
148221 ) ,
149222 reportTitle : this . opts . reportTitle ,
@@ -156,18 +229,22 @@ class BundleAnalyzerPlugin {
156229 }
157230
158231 getBundleDirFromCompiler ( ) {
159- if ( typeof this . compiler . outputFileSystem . constructor === "undefined" ) {
160- return this . compiler . outputPath ;
232+ const outputFileSystemConstructor =
233+ /** @type {OutputFileSystem } */
234+ ( /** @type {Compiler } */ ( this . compiler ) . outputFileSystem ) . constructor ;
235+
236+ if ( typeof outputFileSystemConstructor === "undefined" ) {
237+ return /** @type {Compiler } */ ( this . compiler ) . outputPath ;
161238 }
162- switch ( this . compiler . outputFileSystem . constructor . name ) {
239+ switch ( outputFileSystemConstructor . name ) {
163240 case "MemoryFileSystem" :
164241 return null ;
165242 // Detect AsyncMFS used by Nuxt 2.5 that replaces webpack's MFS during development
166243 // Related: #274
167244 case "AsyncMFS" :
168245 return null ;
169246 default :
170- return this . compiler . outputPath ;
247+ return /** @type { Compiler } */ ( this . compiler ) . outputPath ;
171248 }
172249 }
173250}
0 commit comments