MCPcopy
hub / github.com/zxlie/FeHelper / detectUnusedFiles

Function detectUnusedFiles

gulpfile.js:210–383  ·  view source on GitHub ↗
(outputDir = 'output-chrome/apps', cb)

Source from the content-addressed store, hash-verified

208
209// 检测未使用的静态文件(参数化outputDir)
210function detectUnusedFiles(outputDir = 'output-chrome/apps', cb) {
211 const allFiles = new Set();
212 const referencedFiles = new Set();
213 function shouldExcludeFile(filePath) {
214 if (filePath.includes('content-script.js') || filePath.includes('content-script.css')) return true;
215 if (filePath.includes('node_modules')) return true;
216 if (filePath.endsWith('fh-config.js')) return true;
217 return false;
218 }
219 function getAllFiles(dir) {
220 const files = fs.readdirSync(dir);
221 files.forEach(file => {
222 const fullPath = path.join(dir, file);
223 if (fs.statSync(fullPath).isDirectory()) {
224 if (file !== 'node_modules') {
225 getAllFiles(fullPath);
226 }
227 } else {
228 if (/\.(js|css|png|jpg|jpeg|gif|svg|woff2?|ttf|eot)$/i.test(file) && !shouldExcludeFile(fullPath)) {
229 const relativePath = path.relative(outputDir, fullPath);
230 allFiles.add(relativePath);
231 }
232 }
233 });
234 }
235 function findReferences(content, filePath) {
236 const fileDir = path.dirname(filePath);
237 const patterns = [
238 /['"`][^`'\"]*?([./\w-]+\.(?:js|css|png|jpg|jpeg|gif|svg|woff2?|ttf|eot))['"`]/g,
239 /url\(['"]?([./\w-]+(?:\.(?:png|jpg|jpeg|gif|svg|woff2?|ttf|eot))?)['"]?\)/gi,
240 /@import\s+['"]([^'\"]+\.css)['"];?/gi,
241 /(?:src|href)=['"](chrome-extension:\/\/[^/]+\/)?([^'"?#]+(?:\.(?:js|css|png|jpg|jpeg|gif|svg|woff2?|ttf|eot)))['"]/gi
242 ];
243 patterns.forEach((pattern, index) => {
244 let match;
245 while ((match = pattern.exec(content)) !== null) {
246 let extractedPath = '';
247 if (index === 3) {
248 extractedPath = match[2];
249 } else {
250 extractedPath = match[1];
251 }
252 if (!extractedPath || typeof extractedPath !== 'string') continue;
253 if (shouldExcludeFile(extractedPath)) continue;
254 let finalPathToAdd = '';
255 const isChromeExt = index === 3 && match[1];
256 if (isChromeExt || extractedPath.startsWith('/')) {
257 finalPathToAdd = extractedPath.startsWith('/') ? extractedPath.slice(1) : extractedPath;
258 } else {
259 const absolutePath = path.resolve(fileDir, extractedPath);
260 finalPathToAdd = path.relative(outputDir, absolutePath);
261 }
262 if (finalPathToAdd && !shouldExcludeFile(finalPathToAdd)) {
263 referencedFiles.add(finalPathToAdd.replace(/\\/g, '/'));
264 }
265 }
266 });
267 }

Callers 3

detectChromeFunction · 0.85
detectEdgeFunction · 0.85
detectFirefoxFunction · 0.85

Calls 7

runTestsFunction · 0.85
getAllFilesFunction · 0.85
processManifestFunction · 0.85
shouldExcludeFileFunction · 0.85
findReferencesFunction · 0.85
cbFunction · 0.85
hasMethod · 0.80

Tested by

no test coverage detected