@@ -3,9 +3,51 @@ import { validate } from "schema-utils";
33import schema from "./options.json" ;
44import { minify as minifyFn } from "./minify" ;
55
6+ /** @typedef {import("schema-utils/declarations/validate").Schema } Schema */
7+ /** @typedef {import("webpack").Compiler } Compiler */
8+ /** @typedef {import("webpack").Compilation } Compilation */
9+ /** @typedef {import("webpack").Asset } Asset */
10+ /** @typedef {import("webpack").WebpackError } WebpackError */
11+
12+ /** @typedef {RegExp | string } Rule */
13+
14+ /** @typedef {Rule[] | Rule } Rules */
15+
16+ /**
17+ * @typedef {Object } JSONOptions
18+ * @property {(this: any, key: string, value: any) => any | (number | string)[] | null } [replacer]
19+ * @property {string | number } [space]
20+ */
21+
22+ /**
23+ * @typedef {Object } BasePluginOptions
24+ * @property {Rules } [test]
25+ * @property {Rules } [include]
26+ * @property {Rules } [exclude]
27+ * @property {JSONOptions } [minimizerOptions]
28+ */
29+
30+ /**
31+ * @typedef {Object } MinimizedResult
32+ * @property {string } code
33+ */
34+
35+ /**
36+ * @typedef {Object } InternalOptions
37+ * @property {string } input
38+ * @property {JSONOptions } [minimizerOptions]
39+ */
40+
41+ /**
42+ * @typedef {BasePluginOptions } InternalPluginOptions
43+ */
44+
645class JsonMinimizerPlugin {
46+ /**
47+ * @param {BasePluginOptions } [options]
48+ */
749 constructor ( options = { } ) {
8- validate ( schema , options , {
50+ validate ( /** @type { Schema } */ ( schema ) , options , {
951 name : "Json Minimizer Plugin" ,
1052 baseDataPath : "options" ,
1153 } ) ;
@@ -17,6 +59,10 @@ class JsonMinimizerPlugin {
1759 exclude,
1860 } = options ;
1961
62+ /**
63+ * @private
64+ * @type {InternalPluginOptions }
65+ */
2066 this . options = {
2167 test,
2268 include,
@@ -25,18 +71,31 @@ class JsonMinimizerPlugin {
2571 } ;
2672 }
2773
74+ /**
75+ * @param {any } error
76+ * @param {string } file
77+ * @param {string } context
78+ * @returns {Error }
79+ */
2880 static buildError ( error , file , context ) {
2981 return new Error (
3082 `"${ file } " in "${ context } " from Json Minimizer:\n${ error } `
3183 ) ;
3284 }
3385
86+ /**
87+ * @private
88+ * @param {Compiler } compiler
89+ * @param {Compilation } compilation
90+ * @param {Record<string, import("webpack").sources.Source> } assets
91+ * @returns {Promise<void> }
92+ */
3493 async optimize ( compiler , compilation , assets ) {
3594 const cache = compilation . getCache ( "JsonMinimizerWebpackPlugin" ) ;
3695 const assetsForMinify = await Promise . all (
3796 Object . keys ( assets )
3897 . filter ( ( name ) => {
39- const { info } = compilation . getAsset ( name ) ;
98+ const { info } = /** @type { Asset } */ ( compilation . getAsset ( name ) ) ;
4099
41100 // Skip double minimize assets from child compilation
42101 if ( info . minimized ) {
@@ -56,7 +115,9 @@ class JsonMinimizerPlugin {
56115 return true ;
57116 } )
58117 . map ( async ( name ) => {
59- const { info, source } = compilation . getAsset ( name ) ;
118+ const { info, source } = /** @type {Asset } */ (
119+ compilation . getAsset ( name )
120+ ) ;
60121
61122 const eTag = cache . getLazyHashedEtag ( source ) ;
62123 const cacheItem = cache . getItemCache ( name , eTag ) ;
@@ -86,17 +147,21 @@ class JsonMinimizerPlugin {
86147 input = input . toString ( ) ;
87148 }
88149
150+ /**
151+ * @type {InternalOptions }
152+ */
89153 const options = {
90154 input,
91- minimizerOptions : { ...this . options . minimizerOptions } ,
92- minify : this . options . minify ,
155+ minimizerOptions : this . options . minimizerOptions ,
93156 } ;
94157
95158 try {
96159 output = await minifyFn ( options ) ;
97160 } catch ( error ) {
98161 compilation . errors . push (
99- JsonMinimizerPlugin . buildError ( error , name , compiler . context )
162+ /** @type {WebpackError } */ (
163+ JsonMinimizerPlugin . buildError ( error , name , compiler . context )
164+ )
100165 ) ;
101166
102167 return ;
@@ -120,6 +185,10 @@ class JsonMinimizerPlugin {
120185 Promise . all ( scheduledTasks ) ;
121186 }
122187
188+ /**
189+ * @param {Compiler } compiler
190+ * @returns {void }
191+ */
123192 apply ( compiler ) {
124193 const pluginName = this . constructor . name ;
125194
@@ -140,8 +209,11 @@ class JsonMinimizerPlugin {
140209 . tap (
141210 "json-minimizer-webpack-plugin" ,
142211 ( minimized , { green, formatFlag } ) =>
143- // eslint-disable-next-line no-undefined
144- minimized ? green ( formatFlag ( "minimized" ) ) : undefined
212+ minimized
213+ ? /** @type {Function } */ ( green ) (
214+ /** @type {Function } */ ( formatFlag ) ( "minimized" )
215+ )
216+ : ""
145217 ) ;
146218 } ) ;
147219 } ) ;
0 commit comments