@@ -11,7 +11,8 @@ const TARGET_DIR = process.argv[2] ? path.resolve(process.argv[2]) : path.join(p
1111if ( process . argv [ 2 ] === "--help" || process . argv [ 2 ] === "-h" ) {
1212 console . log ( "Usage: node find-unused-assets.mjs [directory]" ) ;
1313 console . log ( "" ) ;
14- console . log ( "Finds asset files that are not referenced from any HTML file in the target directory." ) ;
14+ console . log ( "Finds files that are not referenced from any HTML file in the target directory." ) ;
15+ console . log ( "Files matching patterns in test/unused-assets-allowlist.json are ignored." ) ;
1516 console . log ( "" ) ;
1617 console . log ( "Arguments:" ) ;
1718 console . log ( ' directory Directory to scan (default: "./build")' ) ;
@@ -23,41 +24,13 @@ if (process.argv[2] === "--help" || process.argv[2] === "-h") {
2324 process . exit ( 0 ) ;
2425}
2526
26- // Asset file extensions to check
27- const ASSET_EXTENSIONS = [
28- "css" ,
29- "js" ,
30- "jpg" ,
31- "jpeg" ,
32- "png" ,
33- "gif" ,
34- "svg" ,
35- "webp" ,
36- "woff" ,
37- "woff2" ,
38- "ttf" ,
39- "otf" ,
40- "eot" ,
41- "ico" ,
42- "mp4" ,
43- "webm" ,
44- "mp3" ,
45- "wav" ,
46- "ogg" ,
47- "pdf" ,
48- "zip" ,
49- "tar" ,
50- "gz" ,
51- ] ;
52-
53- // Find all asset files in the target directory
54- function findAssetFiles ( ) {
55- const extensions = ASSET_EXTENSIONS . map ( ( ext ) => `**/*.${ ext } ` ) ;
27+ // Find all files in the target directory (excluding directories)
28+ function findAllFiles ( ) {
5629 return glob
57- . sync ( extensions , {
30+ . sync ( "**/*" , {
5831 cwd : TARGET_DIR ,
5932 nocase : true ,
60- dot : false ,
33+ dot : true ,
6134 } )
6235 . filter ( ( file ) => fs . lstatSync ( path . join ( TARGET_DIR , file ) ) . isFile ( ) ) ;
6336}
@@ -211,7 +184,7 @@ function shouldIgnoreFile(filePath, config) {
211184}
212185
213186// Main execution
214- console . log ( "🧪 Checking for unused asset files" ) ;
187+ console . log ( "🧪 Checking for unused files" ) ;
215188console . log ( `📁 Scanning directory: ${ TARGET_DIR } ` ) ;
216189
217190// Verify target directory exists
@@ -220,11 +193,15 @@ if (!fs.existsSync(TARGET_DIR)) {
220193 process . exit ( 1 ) ;
221194}
222195
223- const assetFiles = findAssetFiles ( ) ;
196+ const allFiles = findAllFiles ( ) ;
224197const htmlFiles = findHtmlFiles ( ) ;
225198
226- if ( assetFiles . length === 0 ) {
227- console . log ( "✨ No asset files found in target directory" ) ;
199+ // Filter files to get potential unused files (excluding those in allowlist)
200+ const config = loadConfig ( ) ;
201+ const candidateFiles = allFiles . filter ( ( file ) => ! shouldIgnoreFile ( file , config ) ) ;
202+
203+ if ( candidateFiles . length === 0 ) {
204+ console . log ( "✨ No candidate files found to check (all files are in allowlist)" ) ;
228205 process . exit ( 0 ) ;
229206}
230207
@@ -233,28 +210,27 @@ if (htmlFiles.length === 0) {
233210 process . exit ( 1 ) ;
234211}
235212
236- console . log ( `📄 Found ${ assetFiles . length } asset files and ${ htmlFiles . length } HTML files` ) ;
213+ console . log ( `📄 Found ${ candidateFiles . length } candidate files and ${ htmlFiles . length } HTML files` ) ;
237214
238215const referencedAssets = extractAssetReferences ( htmlFiles ) ;
239- const config = loadConfig ( ) ;
240216const unusedAssets = [ ] ;
241217
242- assetFiles . forEach ( ( assetFile ) => {
243- const normalizedPath = path . normalize ( assetFile ) ;
218+ candidateFiles . forEach ( ( file ) => {
219+ const normalizedPath = path . normalize ( file ) ;
244220
245- if ( ! referencedAssets . has ( normalizedPath ) && ! shouldIgnoreFile ( normalizedPath , config ) ) {
221+ if ( ! referencedAssets . has ( normalizedPath ) ) {
246222 unusedAssets . push ( normalizedPath ) ;
247223 }
248224} ) ;
249225
250226if ( unusedAssets . length > 0 ) {
251- console . log ( "\n❌ Found unused asset files:" ) ;
227+ console . log ( "\n❌ Found unused files:" ) ;
252228 unusedAssets . forEach ( ( asset ) => {
253229 console . log ( ` ${ asset } ` ) ;
254230 } ) ;
255231
256- console . error ( `\n❌ Found ${ unusedAssets . length } unused asset files` ) ;
232+ console . error ( `\n❌ Found ${ unusedAssets . length } unused files` ) ;
257233 process . exit ( 1 ) ;
258234} else {
259- console . log ( "\n✨ No unused asset files found!" ) ;
235+ console . log ( "\n✨ No unused files found!" ) ;
260236}
0 commit comments