@@ -3,9 +3,51 @@ import { validate } from "schema-utils";
3
3
import schema from "./options.json" ;
4
4
import { minify as minifyFn } from "./minify" ;
5
5
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
+
6
45
class JsonMinimizerPlugin {
46
+ /**
47
+ * @param {BasePluginOptions } [options]
48
+ */
7
49
constructor ( options = { } ) {
8
- validate ( schema , options , {
50
+ validate ( /** @type { Schema } */ ( schema ) , options , {
9
51
name : "Json Minimizer Plugin" ,
10
52
baseDataPath : "options" ,
11
53
} ) ;
@@ -17,6 +59,10 @@ class JsonMinimizerPlugin {
17
59
exclude,
18
60
} = options ;
19
61
62
+ /**
63
+ * @private
64
+ * @type {InternalPluginOptions }
65
+ */
20
66
this . options = {
21
67
test,
22
68
include,
@@ -25,18 +71,31 @@ class JsonMinimizerPlugin {
25
71
} ;
26
72
}
27
73
74
+ /**
75
+ * @param {any } error
76
+ * @param {string } file
77
+ * @param {string } context
78
+ * @returns {Error }
79
+ */
28
80
static buildError ( error , file , context ) {
29
81
return new Error (
30
82
`"${ file } " in "${ context } " from Json Minimizer:\n${ error } `
31
83
) ;
32
84
}
33
85
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
+ */
34
93
async optimize ( compiler , compilation , assets ) {
35
94
const cache = compilation . getCache ( "JsonMinimizerWebpackPlugin" ) ;
36
95
const assetsForMinify = await Promise . all (
37
96
Object . keys ( assets )
38
97
. filter ( ( name ) => {
39
- const { info } = compilation . getAsset ( name ) ;
98
+ const { info } = /** @type { Asset } */ ( compilation . getAsset ( name ) ) ;
40
99
41
100
// Skip double minimize assets from child compilation
42
101
if ( info . minimized ) {
@@ -56,7 +115,9 @@ class JsonMinimizerPlugin {
56
115
return true ;
57
116
} )
58
117
. map ( async ( name ) => {
59
- const { info, source } = compilation . getAsset ( name ) ;
118
+ const { info, source } = /** @type {Asset } */ (
119
+ compilation . getAsset ( name )
120
+ ) ;
60
121
61
122
const eTag = cache . getLazyHashedEtag ( source ) ;
62
123
const cacheItem = cache . getItemCache ( name , eTag ) ;
@@ -86,17 +147,21 @@ class JsonMinimizerPlugin {
86
147
input = input . toString ( ) ;
87
148
}
88
149
150
+ /**
151
+ * @type {InternalOptions }
152
+ */
89
153
const options = {
90
154
input,
91
- minimizerOptions : { ...this . options . minimizerOptions } ,
92
- minify : this . options . minify ,
155
+ minimizerOptions : this . options . minimizerOptions ,
93
156
} ;
94
157
95
158
try {
96
159
output = await minifyFn ( options ) ;
97
160
} catch ( error ) {
98
161
compilation . errors . push (
99
- JsonMinimizerPlugin . buildError ( error , name , compiler . context )
162
+ /** @type {WebpackError } */ (
163
+ JsonMinimizerPlugin . buildError ( error , name , compiler . context )
164
+ )
100
165
) ;
101
166
102
167
return ;
@@ -120,6 +185,10 @@ class JsonMinimizerPlugin {
120
185
Promise . all ( scheduledTasks ) ;
121
186
}
122
187
188
+ /**
189
+ * @param {Compiler } compiler
190
+ * @returns {void }
191
+ */
123
192
apply ( compiler ) {
124
193
const pluginName = this . constructor . name ;
125
194
@@ -140,8 +209,11 @@ class JsonMinimizerPlugin {
140
209
. tap (
141
210
"json-minimizer-webpack-plugin" ,
142
211
( 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
+ : ""
145
217
) ;
146
218
} ) ;
147
219
} ) ;
0 commit comments