@@ -6,39 +6,45 @@ import {
6
6
processCssFilesToExtractClasses ,
7
7
writeTemplateClassesToFile ,
8
8
writeCssSelectorsToFile ,
9
- ExtractedData // Add this import
9
+ ExtractedData
10
10
} from './fileProcessors' ;
11
11
12
12
/**
13
- * Clears the UNCSS_TEMP_DIR directory or creates it if it doesn't exist
13
+ * Clears the temporary directory or creates it if it doesn't exist
14
+ *
15
+ * @param tempDir - Path to the temporary directory
16
+ * @param options - Options for the operation
14
17
*/
15
- function clearOrCreateTempDir ( tempDir : string , { isDebug = false } : { isDebug : boolean } ) : void {
18
+ function clearOrCreateTempDir ( tempDir : string , options : { isDebug : boolean } ) : void {
16
19
if ( fs . existsSync ( tempDir ) ) {
17
20
// Directory exists, clear its contents
18
21
fs . readdirSync ( tempDir ) . forEach ( ( file ) => {
19
22
const filePath = path . join ( tempDir , file ) ;
20
23
fs . unlinkSync ( filePath ) ;
21
24
} ) ;
22
- log ( `Cleared contents of ${ tempDir } ` , isDebug ) ;
25
+ log ( `[INFO] Cleared contents of ${ tempDir } ` , options . isDebug ) ;
23
26
} else {
24
27
// Directory doesn't exist, create it
25
28
fs . mkdirSync ( tempDir ) ;
26
- log ( `Created directory ${ tempDir } ` , isDebug ) ;
29
+ log ( `[INFO] Created directory ${ tempDir } ` , options . isDebug ) ;
27
30
}
28
31
}
29
32
30
33
/**
31
34
* Creates a flattened version of extracted classes
32
- * @param inputFileName - Name of the input file
33
- * @param outputFileName - Name of the output file
35
+ * @param {string } inputFileName - Name of the input file
36
+ * @param {string } outputFileName - Name of the output file
37
+ * @param {Object } options - Options for the operation
38
+ * @param {boolean } options.isDebug - Whether to show debug information
39
+ * @param {string } options.uncssTempDir - Path to the temporary directory
34
40
*/
35
41
function createFlattenedClasses (
36
42
inputFileName : string ,
37
43
outputFileName : string ,
38
- { isDebug = false , uncssTempDir } : { isDebug : boolean ; uncssTempDir : string }
44
+ options : { isDebug : boolean ; uncssTempDir : string }
39
45
) : void {
40
- const inputPath = path . join ( uncssTempDir , inputFileName ) ;
41
- const outputPath = path . join ( uncssTempDir , outputFileName ) ;
46
+ const inputPath = path . join ( options . uncssTempDir , inputFileName ) ;
47
+ const outputPath = path . join ( options . uncssTempDir , outputFileName ) ;
42
48
const items : ExtractedData [ ] = JSON . parse ( fs . readFileSync ( inputPath , 'utf8' ) ) ;
43
49
const flattenedItems = new Set < string > ( ) ;
44
50
@@ -51,7 +57,7 @@ function createFlattenedClasses(
51
57
} ) ;
52
58
53
59
fs . writeFileSync ( outputPath , JSON . stringify ( Array . from ( flattenedItems ) , null , 2 ) ) ;
54
- log ( `Flattened classes written to: ${ outputPath } ` , isDebug ) ;
60
+ log ( `[INFO] Flattened classes written to: ${ outputPath } ` , options . isDebug ) ;
55
61
}
56
62
57
63
/**
@@ -66,112 +72,44 @@ function isIgnoredClass(className: string, ignoredClassPatterns: RegExp[]): bool
66
72
67
73
/**
68
74
* Compares flattened classes from template and CSS files and generates a diff report
75
+ * @param {Object } options - Options for the comparison
76
+ * @param {boolean } options.isDebug - Whether to show debug information
77
+ * @param {string } options.uncssTempDir - Path to the temporary directory
78
+ * @param {RegExp[] } options.ignoredClassPatterns - Array of RegExp patterns for classes to ignore
79
+ * @param {string } options.outputFile - Name of the output file for the diff report
80
+ * @param {string } options.classesFromTemplatesFlattenedFileName - Name of the file containing flattened template classes
81
+ * @param {string } options.classesFromCssFlattenedFileName - Name of the file containing flattened CSS classes
69
82
*/
70
- function getUnusedCssClasses ( {
71
- isDebug = false ,
72
- uncssTempDir,
73
- ignoredClassPatterns,
74
- outputFile,
75
- classesFromTemplatesFlattenedFileName,
76
- classesFromCssFlattenedFileName
77
- } : {
83
+ function getUnusedCssClasses ( options : {
78
84
isDebug : boolean ;
79
85
uncssTempDir : string ;
80
86
ignoredClassPatterns : RegExp [ ] ;
81
87
outputFile : string ;
82
88
classesFromTemplatesFlattenedFileName : string ;
83
89
classesFromCssFlattenedFileName : string ;
84
90
} ) : void {
85
- const templateClassesPath = path . join ( uncssTempDir , classesFromTemplatesFlattenedFileName ) ;
86
- const cssClassesPath = path . join ( uncssTempDir , classesFromCssFlattenedFileName ) ;
91
+ const templateClassesPath = path . join ( options . uncssTempDir , options . classesFromTemplatesFlattenedFileName ) ;
92
+ const cssClassesPath = path . join ( options . uncssTempDir , options . classesFromCssFlattenedFileName ) ;
87
93
88
94
const templateClassesList = new Set < string > ( JSON . parse ( fs . readFileSync ( templateClassesPath , 'utf8' ) ) ) ;
89
95
const cssClassesList = new Set < string > ( JSON . parse ( fs . readFileSync ( cssClassesPath , 'utf8' ) ) ) ;
90
96
91
- const cssClassesNotFoundInTemplates = Array . from ( cssClassesList ) . filter ( ( cls : string ) => ! templateClassesList . has ( cls ) && ! isIgnoredClass ( cls , ignoredClassPatterns ) ) ;
97
+ const cssClassesNotFoundInTemplates = Array . from ( cssClassesList ) . filter (
98
+ ( cls ) => ! templateClassesList . has ( cls ) && ! isIgnoredClass ( cls , options . ignoredClassPatterns )
99
+ ) ;
92
100
93
- const templateClassesNotFoundInCss = Array . from ( templateClassesList ) . filter ( ( cls : string ) => ! cssClassesList . has ( cls ) && ! isIgnoredClass ( cls , ignoredClassPatterns ) ) ;
101
+ const templateClassesNotFoundInCss = Array . from ( templateClassesList ) . filter (
102
+ ( cls ) => ! cssClassesList . has ( cls ) && ! isIgnoredClass ( cls , options . ignoredClassPatterns )
103
+ ) ;
94
104
95
105
const diffReport = {
96
106
cssClassesNotFoundInTemplates,
97
107
templateClassesNotFoundInCss,
98
108
} ;
99
109
100
- const outputPath = path . join ( uncssTempDir , outputFile ) ;
110
+ const outputPath = path . join ( options . uncssTempDir , options . outputFile ) ;
101
111
fs . writeFileSync ( outputPath , JSON . stringify ( diffReport , null , 2 ) ) ;
102
- log ( `Diff report written to: ${ outputPath } ` , isDebug ) ;
103
- }
104
-
105
- interface InitOptions {
106
- isDebug ?: boolean ;
107
- }
108
-
109
- /**
110
- * Initializes and runs the unused CSS classes check
111
- * @param options - Options for the initialization
112
- */
113
- function init ( options : TwigUnusedCssFinderOptions = { } ) : void {
114
- console . info ( '------------ START CheckUnusedCssClasses ------------' ) ;
115
-
116
- const {
117
- uncssTempDir = './uncss-stats' ,
118
- twigDir = './templates' ,
119
- twigPattern = / \. t w i g $ / ,
120
- vueDir = './assets/js' ,
121
- vuePattern = / \. v u e $ / ,
122
- cssDir = './public/assets' ,
123
- cssPattern = / \. c s s $ / ,
124
- ignoredClassPatterns = [ / ^ j s - / ] ,
125
- classesFromCssFileName = 'all_classes_from_css.json' ,
126
- classesFromCssFlattenedFileName = 'all_classes_from_css_flattened.json' ,
127
- classesFromTemplatesFileName = 'all_classes_from_vue_and_twig.json' ,
128
- classesFromTemplatesFlattenedFileName = 'all_classes_from_vue_and_twig_flattened.json' ,
129
- outputFile = 'unused_css_classes_report.json' ,
130
- isDebug = false ,
131
- showHelperInfos = false ,
132
- } = options ;
133
-
134
- log ( '[TASK] Clearing or creating temp directory' , isDebug ) ;
135
- clearOrCreateTempDir ( uncssTempDir , { isDebug } ) ;
136
-
137
- log ( '[TASK] Reading template files' , isDebug ) ;
138
- const templateFiles = [
139
- ...findFiles ( twigDir , twigPattern ) ,
140
- ...findFiles ( vueDir , vuePattern ) ,
141
- ] ;
142
-
143
- log ( '[TASK] Processing template files and extracting CSS classes' , isDebug ) ;
144
- const templateClasses : ExtractedData [ ] = processTemplateFilesToExtractClasses ( templateFiles ) ;
145
-
146
- log ( '[TASK] Reading CSS files' , isDebug ) ;
147
- const cssFiles = findFiles ( cssDir , cssPattern ) ;
148
-
149
- log ( '[TASK] Processing CSS files' , isDebug ) ;
150
- const cssSelectors : ExtractedData [ ] = processCssFilesToExtractClasses ( cssFiles ) ;
151
-
152
- log ( '[TASK] Writing extracted CSS classes to file' , isDebug ) ;
153
- writeTemplateClassesToFile ( templateClasses , classesFromTemplatesFileName , uncssTempDir ) ;
154
-
155
- log ( '[TASK] Writing extracted CSS selectors to file' , isDebug ) ;
156
- writeCssSelectorsToFile ( cssSelectors , classesFromCssFileName , uncssTempDir ) ;
157
-
158
- log ( '[TASK] Creating flattened version of template classes' , isDebug ) ;
159
- createFlattenedClasses ( classesFromTemplatesFileName , classesFromTemplatesFlattenedFileName , { isDebug, uncssTempDir } ) ;
160
-
161
- log ( '[TASK] Creating flattened version of CSS classes' , isDebug ) ;
162
- createFlattenedClasses ( classesFromCssFileName , classesFromCssFlattenedFileName , { isDebug, uncssTempDir } ) ;
163
-
164
- log ( '[TASK] Comparing flattened classes' , isDebug ) ;
165
- getUnusedCssClasses ( {
166
- isDebug,
167
- uncssTempDir,
168
- ignoredClassPatterns,
169
- outputFile,
170
- classesFromTemplatesFlattenedFileName,
171
- classesFromCssFlattenedFileName
172
- } ) ;
173
-
174
- console . info ( '------------ END CheckUnusedCssClasses ------------' ) ;
112
+ log ( `[INFO] Diff report written to: ${ outputPath } ` , options . isDebug ) ;
175
113
}
176
114
177
115
export interface TwigUnusedCssFinderOptions {
@@ -266,6 +204,74 @@ export interface TwigUnusedCssFinderOptions {
266
204
showHelperInfos ?: boolean ;
267
205
}
268
206
207
+ /**
208
+ * Initializes and runs the unused CSS classes check
209
+ * @param {TwigUnusedCssFinderOptions } options - Options for the initialization
210
+ */
211
+ function initFn ( options : TwigUnusedCssFinderOptions = { } ) : void {
212
+ console . info ( '------------ [START] Twig unused css finder ------------' ) ;
213
+
214
+ const {
215
+ uncssTempDir = './uncss-stats' ,
216
+ twigDir = './templates' ,
217
+ twigPattern = / \. t w i g $ / ,
218
+ vueDir = './assets/js' ,
219
+ vuePattern = / \. v u e $ / ,
220
+ cssDir = './public/assets' ,
221
+ cssPattern = / \. c s s $ / ,
222
+ ignoredClassPatterns = [ / ^ j s - / ] ,
223
+ classesFromCssFileName = 'all_classes_from_css.json' ,
224
+ classesFromCssFlattenedFileName = 'all_classes_from_css_flattened.json' ,
225
+ classesFromTemplatesFileName = 'all_classes_from_vue_and_twig.json' ,
226
+ classesFromTemplatesFlattenedFileName = 'all_classes_from_vue_and_twig_flattened.json' ,
227
+ outputFile = 'unused_css_classes_report.json' ,
228
+ isDebug = false ,
229
+ showHelperInfos = false ,
230
+ } = options ;
231
+
232
+ log ( '[TASK] Clearing or creating temp directory' , isDebug ) ;
233
+ clearOrCreateTempDir ( uncssTempDir , { isDebug } ) ;
234
+
235
+ log ( '[TASK] Reading template files' , isDebug ) ;
236
+ const templateFiles = [
237
+ ...findFiles ( twigDir , twigPattern ) ,
238
+ ...findFiles ( vueDir , vuePattern ) ,
239
+ ] ;
240
+
241
+ log ( '[TASK] Processing template files and extracting CSS classes' , isDebug ) ;
242
+ const templateClasses : ExtractedData [ ] = processTemplateFilesToExtractClasses ( templateFiles ) ;
243
+
244
+ log ( '[TASK] Reading CSS files' , isDebug ) ;
245
+ const cssFiles = findFiles ( cssDir , cssPattern ) ;
246
+
247
+ log ( '[TASK] Processing CSS files' , isDebug ) ;
248
+ const cssSelectors : ExtractedData [ ] = processCssFilesToExtractClasses ( cssFiles ) ;
249
+
250
+ log ( '[TASK] Writing extracted CSS classes to file' , isDebug ) ;
251
+ writeTemplateClassesToFile ( templateClasses , classesFromTemplatesFileName , uncssTempDir ) ;
252
+
253
+ log ( '[TASK] Writing extracted CSS selectors to file' , isDebug ) ;
254
+ writeCssSelectorsToFile ( cssSelectors , classesFromCssFileName , uncssTempDir ) ;
255
+
256
+ log ( '[TASK] Creating flattened version of template classes' , isDebug ) ;
257
+ createFlattenedClasses ( classesFromTemplatesFileName , classesFromTemplatesFlattenedFileName , { isDebug, uncssTempDir } ) ;
258
+
259
+ log ( '[TASK] Creating flattened version of CSS classes' , isDebug ) ;
260
+ createFlattenedClasses ( classesFromCssFileName , classesFromCssFlattenedFileName , { isDebug, uncssTempDir } ) ;
261
+
262
+ log ( '[TASK] Comparing flattened classes' , isDebug ) ;
263
+ getUnusedCssClasses ( {
264
+ isDebug,
265
+ uncssTempDir,
266
+ ignoredClassPatterns,
267
+ outputFile,
268
+ classesFromTemplatesFlattenedFileName,
269
+ classesFromCssFlattenedFileName
270
+ } ) ;
271
+
272
+ console . info ( '------------ [END] Twig unused css finder ------------' ) ;
273
+ }
274
+
269
275
/**
270
276
* Runs the unused CSS classes check for Twig and Vue templates
271
277
* @param {Object } options - Configuration options
@@ -285,6 +291,6 @@ export interface TwigUnusedCssFinderOptions {
285
291
* @param {boolean } [options.isDebug=false] - Enable debug logging
286
292
* @param {boolean } [options.showHelperInfos=false] - Show additional helper information
287
293
*/
288
- export function twigUnusedCssFinder ( options : Partial < TwigUnusedCssFinderOptions > = { } ) {
289
- return init ( options ) ;
294
+ export function twigUnusedCssFinder ( options : Partial < TwigUnusedCssFinderOptions > = { } ) : void {
295
+ initFn ( options ) ;
290
296
}
0 commit comments