vite-plugin-resource-waste 1.1.7 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
var n={"static-unused-js":"\u9759\u6001\u672A\u4F7F\u7528 JS","unreachable-component":"\u4E0D\u53EF\u8FBE\u7EC4\u4EF6/\u9875\u9762","cache-miss":"\u7F13\u5B58\u7B56\u7565\u98CE\u9669","duplicate-dependency":"\u91CD\u590D\u4F9D\u8D56","import-pattern":"\u5BFC\u5165\u6A21\u5F0F\u6D6A\u8D39","dead-import":"\u672A\u4F7F\u7528 import","prefetch-waste":"\u9884\u52A0\u8F7D\u6D6A\u8D39"},i={high:"\u9AD8",medium:"\u4E2D",low:"\u4F4E"},p='<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>Resource Waste Report</title><style>body{font:14px/1.5 system-ui,sans-serif;margin:20px;color:#1e293b}table{border-collapse:collapse;width:100%;margin-top:12px}td,th{border:1px solid #cbd5e1;padding:8px;text-align:left;vertical-align:top}th{background:#f1f5f9}.badge{font-size:12px;font-weight:600}.cards{display:flex;flex-wrap:wrap;gap:12px;margin:12px 0}.card{border:1px solid #cbd5e1;border-radius:8px;padding:10px 14px;min-width:120px}.muted{color:#64748b;font-size:13px}code{font-size:12px;background:#f1f5f9;padding:2px 4px}</style></head><body><h1>\u524D\u7AEF\u8D44\u6E90\u6D6A\u8D39\u5206\u6790\u62A5\u544A</h1><p class="muted">{{META}}</p><div class="cards">{{SUMMARY}}</div><table><thead><tr><th>\u7EA7\u522B</th><th>\u7C7B\u522B</th><th>\u95EE\u9898</th><th>\u6587\u4EF6</th><th>\u4F20\u8F93</th><th>\u89E3\u6790</th><th>\u5EFA\u8BAE</th></tr></thead><tbody>{{ROWS}}</tbody></table><p class="muted">{{LEGEND}}</p></body></html>';function l(e){let{summary:s,issues:d}=e,o=d.map(t=>`<tr><td class="badge">${i[t.severity]}</td><td>${n[t.category]}</td><td><b>${a(t.title)}</b><br/><span class="muted">${a(t.detail)}</span></td><td><code>${a(t.file??"-")}</code></td><td>${t.cost.transferKb>0?t.cost.transferKb+" KB":"-"}</td><td>${t.cost.parseMsEstimate>0?t.cost.parseMsEstimate+" ms":"-"}</td><td>${a(t.suggestion)}</td></tr>`).join(""),c=r("\u95EE\u9898\u603B\u6570",s.totalIssues)+r("\u6D6A\u8D39\u4F20\u8F93",s.totalWasteTransferKb+" KB")+r("\u6D6A\u8D39\u89E3\u6790",s.totalParseMsEstimate+" ms")+r("\u9AD8\u5371",s.bySeverity.high);return p.replace("{{META}}",`\u751F\u6210: ${e.generatedAt} \xB7 \u6A21\u5F0F: ${e.mode}`).replace("{{SUMMARY}}",c).replace("{{ROWS}}",o||'<tr><td colspan="7">\u672A\u68C0\u6D4B\u5230\u8D44\u6E90\u6D6A\u8D39\u95EE\u9898</td></tr>').replace("{{LEGEND}}","\u6784\u5EFA\u671F\u9759\u6001\u5206\u6790
|
|
1
|
+
var n={"static-unused-js":"\u9759\u6001\u672A\u4F7F\u7528 JS","unreachable-component":"\u4E0D\u53EF\u8FBE\u7EC4\u4EF6/\u9875\u9762","cache-miss":"\u7F13\u5B58\u7B56\u7565\u98CE\u9669","duplicate-dependency":"\u91CD\u590D\u4F9D\u8D56","import-pattern":"\u5BFC\u5165\u6A21\u5F0F\u6D6A\u8D39","dead-import":"\u672A\u4F7F\u7528 import","prefetch-waste":"\u9884\u52A0\u8F7D\u6D6A\u8D39"},i={high:"\u9AD8",medium:"\u4E2D",low:"\u4F4E"},p='<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>Resource Waste Report</title><style>body{font:14px/1.5 system-ui,sans-serif;margin:20px;color:#1e293b}table{border-collapse:collapse;width:100%;margin-top:12px}td,th{border:1px solid #cbd5e1;padding:8px;text-align:left;vertical-align:top}th{background:#f1f5f9}.badge{font-size:12px;font-weight:600}.cards{display:flex;flex-wrap:wrap;gap:12px;margin:12px 0}.card{border:1px solid #cbd5e1;border-radius:8px;padding:10px 14px;min-width:120px}.muted{color:#64748b;font-size:13px}code{font-size:12px;background:#f1f5f9;padding:2px 4px}</style></head><body><h1>\u524D\u7AEF\u8D44\u6E90\u6D6A\u8D39\u5206\u6790\u62A5\u544A</h1><p class="muted">{{META}}</p><div class="cards">{{SUMMARY}}</div><table><thead><tr><th>\u7EA7\u522B</th><th>\u7C7B\u522B</th><th>\u95EE\u9898</th><th>\u6587\u4EF6</th><th>\u4F20\u8F93</th><th>\u89E3\u6790</th><th>\u5EFA\u8BAE</th></tr></thead><tbody>{{ROWS}}</tbody></table><p class="muted">{{LEGEND}}</p></body></html>';function l(e){let{summary:s,issues:d}=e,o=d.map(t=>`<tr><td class="badge">${i[t.severity]}</td><td>${n[t.category]}</td><td><b>${a(t.title)}</b><br/><span class="muted">${a(t.detail)}</span></td><td><code>${a(t.file??"-")}</code></td><td>${t.cost.transferKb>0?t.cost.transferKb+" KB":"-"}</td><td>${t.cost.parseMsEstimate>0?t.cost.parseMsEstimate+" ms":"-"}</td><td>${a(t.suggestion)}</td></tr>`).join(""),c=r("\u95EE\u9898\u603B\u6570",s.totalIssues)+r("\u6D6A\u8D39\u4F20\u8F93",s.totalWasteTransferKb+" KB")+r("\u6D6A\u8D39\u89E3\u6790",s.totalParseMsEstimate+" ms")+r("\u9AD8\u5371",s.bySeverity.high);return p.replace("{{META}}",`\u751F\u6210: ${e.generatedAt} \xB7 \u6A21\u5F0F: ${e.mode}`).replace("{{SUMMARY}}",c).replace("{{ROWS}}",o||'<tr><td colspan="7">\u672A\u68C0\u6D4B\u5230\u8D44\u6E90\u6D6A\u8D39\u95EE\u9898</td></tr>').replace("{{LEGEND}}","\u6784\u5EFA\u671F\u9759\u6001\u5206\u6790")}function r(e,s){return `<div class="card"><div class="muted">${e}</div><div><b>${s}</b></div></div>`}function a(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}export{l as renderHtmlReport};
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,6 @@ import M from'fs';import S from'path';var _={root:"src",reportDir:"resource-wast
|
|
|
3
3
|
${i}`),!!r){if(/^import\s+['"][^'"]+['"]\s*;?\s*$/.test(s.trim())){n();continue}/from\s+['"][^'"]+['"]\s*;?\s*$/.test(a)&&n();}}return r&&s.trim()&&t.push(s.trim()),t}function Me(e){return /^import\s+['"][^'"]+['"]\s*;?\s*$/.test(e.trim())}function ne(e){let t=[],o=e.match(/\{([\s\S]*)\}/)?.[1]??e;for(let s of o.split(",")){let r=s.trim();if(!r||r.startsWith("type "))continue;let n=r.match(/(?:\w+\s+as\s+)?(\w+)\s*$/);n&&t.push(n[1]);}return t}function ae(e){let t=[];for(let o of $e(e)){if(Me(o)||/^\s*import\s+type\s+/.test(o))continue;let s=o.match(/^\s*import\s+(?:type\s+)?([\s\S]+?)\s+from\s+['"]([^'"]+)['"]\s*;?\s*$/);if(!s)continue;let r=s[1].trim(),n=s[2];if(!(!r||r.startsWith("type ")||/\bimport\s/.test(r)))for(let i of we(r))t.push({localName:i,specifier:n});}return t}function ue(e,t,o){let s=se(t),r=new RegExp(`\\b${e}\\b`);if(r.test(s))return true;if(!o)return false;let n=se(o);if(r.test(n))return true;let i=Re(e);return !!(i!==e.toLowerCase()&&new RegExp(`<${i}[\\s/>]`,"i").test(n))}function Ee(e){let t=new Map;for(let o of e.sourceFiles){let s=R(o);if(!s)continue;let r=c(o),n=o.endsWith(".vue"),{script:i,template:a}=n?re(s):{script:s,template:""},u=ie(n?i:s);for(let{localName:l,specifier:m}of ae(i)){if(!m.startsWith(".")||!ue(l,u,a))continue;let p=oe(o,m);if(!p)continue;let f=t.get(r)??new Set;f.add(c(p)),t.set(r,f);}}return t}function Ce(e,t){let o=new Set(e.sourceFiles.map(i=>c(i))),s=new Set,r=[],n=i=>{let a=c(i);o.has(a)&&!s.has(a)&&r.push(a);};for(let i of e.entries){n(i);let a=e.parsedModules.get(c(i));if(a)for(let u of a.importedIds)n(u);}for(let i of e.sourceFiles){let a=c(i);e.parsedModules.get(a)?.isEntry&&n(a);}if(r.length===0)return o;for(;r.length;){let i=r.shift();if(!s.has(i)){s.add(i);for(let a of t.get(i)??[])s.has(a)||r.push(a);}}return s}function Pe(e,t){let o=e.endsWith(".vue"),{script:s,template:r}=o?re(t):{script:t,template:""},n=ie(o?s:t),i=[];for(let{localName:a,specifier:u}of ae(s))u.startsWith(".")&&(ue(a,n,r)||i.push({localName:a,specifier:u,resolved:oe(e,u)}));return i}function ce(e){return /\.(vue|tsx|jsx)$/i.test(e)&&/pages?|views?|components?|routes?/i.test(e)}function Fe(e,t,o){let s=ce(e);return t?"medium":o?"low":s?"medium":"low"}function le(e){if(!e.options.scanUnusedImports)return [];let t=[],o=xe(e),s=o.size>0,r=Ee(e),n=Ce(e,r),i=0;for(let a of e.sourceFiles){let u=c(a);if(!n.has(u))continue;let l=R(a);if(!l)continue;let m=v(e.projectRoot,a),p=Pe(a,l);if(p.length!==0)for(let{localName:f,specifier:d,resolved:g}of p){let I=g?v(e.projectRoot,g):d,b=g?ve(g,o):false,F=Fe(I,b,s),$=g?E(g):0,Ie=b?`${m} \u5F15\u5165\u4E86 \`${I}\`\uFF08\u7ED1\u5B9A \`${f}\`\uFF09\u4F46\u672A\u5728\u6587\u4EF6\u5185\u4F7F\u7528\u3002\u8BE5\u6A21\u5757\u4ECD\u51FA\u73B0\u5728\u6784\u5EFA\u4EA7\u7269\u4E2D\uFF08\u7EA6 ${$} KB\uFF09\u3002`:`${m} \u5F15\u5165\u4E86 \`${I}\`\uFF08\u7ED1\u5B9A \`${f}\`\uFF09\u4F46\u672A\u5728\u6587\u4EF6\u5185\u4F7F\u7528\u3002`;t.push({id:h("dead-import",i++),category:"dead-import",severity:F,title:`\u672A\u4F7F\u7528\u7684\u9759\u6001 import: ${f}`,file:m,detail:Ie,suggestion:ce(I)?`\u5220\u9664\u5197\u4F59 import\uFF1B\u82E5\u8DEF\u7531/\u9875\u9762\u5DF2\u4E0B\u7EBF\uFF0C\u786E\u8BA4 \`${I}\` \u662F\u5426\u4E5F\u53EF\u79FB\u9664\u3002`:`\u5220\u9664\u672A\u4F7F\u7528\u7684 import \`${f}\` from '${d}'\u3002`,cost:{transferKb:b?$:0,parseMsEstimate:b?y($):0,description:b?`\u5197\u4F59 import \u5BFC\u81F4\u7EA6 ${$} KB \u8FDB\u5165\u4EA7\u7269`:"\u7EF4\u62A4\u6027\u6D6A\u8D39"},metadata:{binding:f,specifier:d,importedModule:I,inBundle:b}});}}return t}function pe(e){if(!e.options.scanUnreachable)return [];let t=[],o=te(e),s=0,r=new Set;for(let n of o){if(n.startsWith("\0")||n.includes("virtual:"))continue;let i=S.isAbsolute(n)?c(n):c(S.resolve(e.projectRoot,n));r.add(i);}for(let n of e.sourceFiles){let i=c(n);if(e.entries.some(g=>c(S.resolve(g))===i))continue;let u=[...r].some(g=>g===i||i.endsWith(g)||g.endsWith(i)),l=e.parsedModules.get(i)??e.parsedModules.get(n),m=l&&l.importers.size>0;if(u||m)continue;let p=E(n),f=v(e.projectRoot,n),d=/pages?|views?|components?|routes?/i.test(f)&&/\.(vue|tsx|jsx)$/.test(f);t.push({id:h("unreachable",s++),category:"unreachable-component",severity:d?"high":"medium",title:d?"\u4E0D\u53EF\u8FBE\u9875\u9762/\u7EC4\u4EF6":"\u4E0D\u53EF\u8FBE\u6E90\u6587\u4EF6",file:f,detail:`${f} \u672A\u51FA\u73B0\u5728\u4EFB\u4F55\u6784\u5EFA\u5165\u53E3\u7684 module graph \u4E2D\uFF0C\u5C5E\u4E8E\u9759\u6001\u4E0D\u53EF\u8FBE\u4EE3\u7801\u3002`,suggestion:d?"\u82E5\u4E1A\u52A1\u5DF2\u4E0B\u7EBF\uFF0C\u5220\u9664\u8BE5\u9875\u9762/\u7EC4\u4EF6\u53CA\u5173\u8054\u9759\u6001\u8D44\u6E90\u3002":"\u786E\u8BA4\u662F\u5426\u4ECD\u88AB\u52A8\u6001\u8DEF\u5F84\u5F15\u7528\uFF1B\u82E5\u65E0\uFF0C\u8BF7\u5220\u9664\u6216\u79FB\u51FA src\u3002",cost:{transferKb:p,parseMsEstimate:y(p),description:`\u6587\u4EF6\u672C\u4F53 ${p} KB\uFF1B\u82E5\u88AB glob \u626B\u5165\u53EF\u80FD\u4EA7\u751F\u66F4\u5927 chunk \u6D6A\u8D39`},metadata:{isPageOrComponent:d}});}return t}function fe(e){let t=[],o=0,s=e.bundleFiles.filter(a=>a.isEntry),r=new Set(e.bundleFiles.filter(a=>a.type==="chunk").map(a=>a.fileName)),n=new Set,i=s.map(a=>a.fileName);for(;i.length;){let a=i.shift();if(n.has(a))continue;n.add(a);let u=e.bundleFiles.find(l=>l.fileName===a);if(u)for(let l of u.imports??[])r.has(l)&&!n.has(l)&&i.push(l);}for(let a of e.bundleFiles){if(a.type!=="chunk"||a.isEntry||n.has(a.fileName))continue;let u=Math.round(a.size/1024*100)/100,l=a.fileName.includes("legacy")||a.fileName.includes("admin");t.push({id:h("orphan-chunk",o++),category:"static-unused-js",severity:"low",title:"\u72EC\u7ACB\u5F02\u6B65 chunk\uFF08\u672A\u88AB\u9759\u6001\u5F15\u7528\uFF09",file:a.fileName,detail:`\u4EA7\u7269 chunk ${a.fileName} (${u} KB) \u4EC5\u901A\u8FC7\u52A8\u6001 import \u52A0\u8F7D\uFF0C\u672C\u8EAB\u4E0D\u662F\u6D6A\u8D39\uFF0C\u4F46\u82E5\u88AB\u5168\u7AD9 prefetch \u5219\u4F1A\u4EA7\u751F\u52A0\u8F7D\u6D6A\u8D39\u3002`,suggestion:"\u68C0\u67E5\u662F\u5426\u5BF9\u8BE5 chunk \u914D\u7F6E\u4E86\u4E0D\u5FC5\u8981\u7684 prefetch/preload\u3002",cost:{transferKb:0,parseMsEstimate:0,description:"\u672C\u8EAB\u4E3A\u6309\u9700\u52A0\u8F7D\uFF1B\u6D6A\u8D39\u53D6\u51B3\u4E8E prefetch \u7B56\u7565"},metadata:{chunkSizeKb:u,dynamicOnly:true,suspiciousPrefetch:l}});}return t}async function K(e){let t=await Promise.all([ee(e),Promise.resolve(le(e)),Promise.resolve(pe(e)),Promise.resolve(fe(e)),Promise.resolve(G(e)),Promise.resolve(J(e)),Promise.resolve(X(e))]);return je(t.flat())}function je(e){let t=new Set;return e.filter(o=>{let s=`${o.category}:${o.file}:${o.title}`;return t.has(s)?false:(t.add(s),true)})}function B(e){let t={high:0,medium:1,low:2};return [...e].sort((o,s)=>{let r=t[o.severity]-t[s.severity];return r!==0?r:s.cost.transferKb-o.cost.transferKb})}function me(e,t,o,s,r){let n=c(t),i=e.get(n)??{id:n,importers:new Set,importedIds:new Set,dynamicImporters:new Set,isEntry:false,code:void 0};i.isEntry=i.isEntry||r,o&&(i.code=o);for(let a of s)i.importedIds.add(c(a));e.set(n,i);for(let a of s){let u=c(a),l=e.get(u)??{id:u,importers:new Set,importedIds:new Set,dynamicImporters:new Set,isEntry:false};l.importers.add(n),e.set(u,l);}}function de(e){let t=[];for(let[o,s]of Object.entries(e))if(s.type==="chunk"){let r=s;t.push({fileName:o,type:"chunk",size:Buffer.byteLength(r.code,"utf-8"),modules:Object.keys(r.modules??{}),isEntry:r.isEntry??false,imports:r.imports,dynamicImports:r.dynamicImports});}else {let n=s.source,i=typeof n=="string"?Buffer.byteLength(n,"utf-8"):n instanceof Uint8Array?n.byteLength:0;t.push({fileName:o,type:"asset",size:i});}return t}function k(e,t){let o=ze(),s={high:0,medium:0,low:0},r=0,n=0;for(let i of e)o[i.category]++,s[i.severity]++,!i.metadata?.informational&&(r+=i.cost.transferKb,n+=i.cost.parseMsEstimate);return {generatedAt:new Date().toISOString(),projectRoot:t.projectRoot,mode:t.mode,outDir:t.outDir,summary:{totalIssues:e.length,totalWasteTransferKb:Math.round(r*100)/100,totalParseMsEstimate:Math.round(n*10)/10,byCategory:o,bySeverity:s},issues:e}}function ze(){return {"static-unused-js":0,"unreachable-component":0,"cache-miss":0,"duplicate-dependency":0,"import-pattern":0,"dead-import":0,"prefetch-waste":0}}function ge(e,t){let{summary:o,issues:s}=e,r=s.slice(0,5);if(console.log(`
|
|
4
4
|
[vite-plugin-resource-waste] Resource waste analysis complete
|
|
5
5
|
`),console.log(` Issues : ${o.totalIssues}`),console.log(` Waste (est.) : ${o.totalWasteTransferKb} KB transfer / ${o.totalParseMsEstimate} ms parse`),console.log(` Threshold : ${t} KB`),r.length>0){console.log(`
|
|
6
|
-
Top issues:`);for(let n of r)console.log(` [${n.severity.toUpperCase()}] ${n.title}${n.file?` (${n.file})`:""}`);}console.log("");}var he={cyan:e=>`\x1B[36m${e}\x1B[0m`,yellow:e=>`\x1B[33m${e}\x1B[0m`};function ye(e={}){let t={..._,...e},o={},s=process.cwd(),r="dist",n="production",i=[],a=[],u=new Map,l=(p=[])=>({root:t.root??"src",projectRoot:s,outDir:r,mode:n,entries:i,sourceFiles:a,parsedModules:u,bundleFiles:p,options:t});async function m(){if(!o.bundle)return;let p=de(o.bundle),f=l(p),d=B(await K(f)),g=k(d,{projectRoot:s,mode:n,outDir:r}),I=S.resolve(s,r,t.reportDir??"resource-waste");j(I);let b=S.join(I,"report.json");if(M.writeFileSync(b,JSON.stringify(g,null,2),"utf-8"),t.htmlReport){let{renderHtmlReport:F}=await import('./html-
|
|
6
|
+
Top issues:`);for(let n of r)console.log(` [${n.severity.toUpperCase()}] ${n.title}${n.file?` (${n.file})`:""}`);}console.log("");}var he={cyan:e=>`\x1B[36m${e}\x1B[0m`,yellow:e=>`\x1B[33m${e}\x1B[0m`};function ye(e={}){let t={..._,...e},o={},s=process.cwd(),r="dist",n="production",i=[],a=[],u=new Map,l=(p=[])=>({root:t.root??"src",projectRoot:s,outDir:r,mode:n,entries:i,sourceFiles:a,parsedModules:u,bundleFiles:p,options:t});async function m(){if(!o.bundle)return;let p=de(o.bundle),f=l(p),d=B(await K(f)),g=k(d,{projectRoot:s,mode:n,outDir:r}),I=S.resolve(s,r,t.reportDir??"resource-waste");j(I);let b=S.join(I,"report.json");if(M.writeFileSync(b,JSON.stringify(g,null,2),"utf-8"),t.htmlReport){let{renderHtmlReport:F}=await import('./html-HJC2K4KV.js');M.writeFileSync(S.join(I,"report.html"),F(g),"utf-8");}if(t.silent||(ge(g,t.wasteThresholdKb??300),console.log(` Report JSON : ${c(S.relative(s,b))}`),t.htmlReport&&console.log(` Report HTML : ${c(S.relative(s,S.join(I,"report.html")))}`)),t.failOnThreshold&&g.summary.totalWasteTransferKb>(t.wasteThresholdKb??300))throw new Error(`[vite-plugin-resource-waste] Estimated waste ${g.summary.totalWasteTransferKb} KB exceeds threshold ${t.wasteThresholdKb} KB`)}return {name:"vite-plugin-resource-waste",enforce:"post",configResolved(p){s=p.root,r=p.build.outDir,n=p.mode;let f=p.build.rollupOptions.input;typeof f=="string"?i=[c(w(s,f))]:Array.isArray(f)?i=f.map(d=>c(w(s,d))):f&&typeof f=="object"?i=Object.values(f).map(d=>c(w(s,d))):i=[c(w(s,"index.html"))];},async buildStart(){a=await A(l());},moduleParsed(p){p.id&&me(u,p.id,p.code??null,[...p.importedIds,...p.dynamicallyImportedIds??[]],p.isEntry);},generateBundle(p,f){o.bundle=f;},async closeBundle(){await m();},configureServer(p){p.httpServer?.once("listening",async()=>{if(n==="production")return;a=await A(l());let f=B(await K(l()));!t.silent&&f.length>0&&console.log(he.cyan(`
|
|
7
7
|
[vite-plugin-resource-waste]`)+he.yellow(` Dev mode: found ${f.length} potential resource waste issue(s). Run "vite build" for full report.
|
|
8
8
|
`));let d=S.resolve(s,"node_modules/.cache/resource-waste");j(d);let g=k(f,{projectRoot:s,mode:n,outDir:r});M.writeFileSync(S.join(d,"dev-report.json"),JSON.stringify(g,null,2),"utf-8");});}}}var Ae=ye;export{Ae as default,ye as resourceWaste};
|
package/package.json
CHANGED