Skip to content

Commit 1ac9084

Browse files
Copilotfulldecent
andcommitted
Update unused assets checker to scan all files with allowlist approach
- Add .htaccess, favicon.ico, .html, .php to allowlist patterns - Change from checking specific asset extensions to checking all files except allowlist - Update console messages and test expectations to reflect new approach - Maintains backward compatibility while being more comprehensive Co-authored-by: fulldecent <382183+fulldecent@users.noreply.github.com>
1 parent c6ba22d commit 1ac9084

File tree

3 files changed

+33
-48
lines changed

3 files changed

+33
-48
lines changed

test/find-unused-assets.mjs

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const TARGET_DIR = process.argv[2] ? path.resolve(process.argv[2]) : path.join(p
1111
if (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");
215188
console.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();
224197
const 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

238215
const referencedAssets = extractAssetReferences(htmlFiles);
239-
const config = loadConfig();
240216
const 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

250226
if (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
}

test/test-unused-assets-fixtures.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const testFixtures = [
77
name: "clean fixture (no unused assets)",
88
path: "test/fixtures/unused-assets/clean",
99
expectedExitCode: 0,
10-
shouldContain: ["✨ No unused asset files found!"],
10+
shouldContain: ["✨ No unused files found!"],
1111
},
1212
{
1313
name: "with-unused fixture (has unused assets)",
1414
path: "test/fixtures/unused-assets/with-unused",
1515
expectedExitCode: 1,
16-
shouldContain: ["Found 2 unused asset files", "assets/js/really-unused.js", "assets/images/unused.png"],
16+
shouldContain: ["Found 2 unused files", "assets/js/really-unused.js", "assets/images/unused.png"],
1717
},
1818
];
1919

test/unused-assets-allowlist.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
["assets/js/unused\\.js", "assets/images/dynamically-loaded.*", "^robots\\.txt$", "^\\.well-known/.*"]
1+
[
2+
"assets/js/unused\\.js",
3+
"assets/images/dynamically-loaded.*",
4+
"^robots\\.txt$",
5+
"^\\.well-known/.*",
6+
"^.*\\.html$",
7+
"^.*\\.php$",
8+
"^.*\\.htaccess$",
9+
"^favicon\\.ico$"
10+
]

0 commit comments

Comments
 (0)