!function(require, directRequire){ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CodeDenpendencyQualityChecker=void 0;const tslib_1=require("tslib"),path=tslib_1.__importStar(require("path")),babel_helper_1=require("../../utils/babel_helper");function findParent(e,a,t,s){const i=new Set(Array.isArray(e)?e:[e]),r=new Set,n=new Set;for(;i.size>0;){const e=i.values().next().value;r.add(e),i.delete(e),t(e)&&n.add(e),(null==s?void 0:s(e))||e.parentDeps.forEach(e=>{e.originModule&&!r.has(e.originModule)&&i.add(e.originModule)})}return Array.from(n.values())}function isCodeFile(e){return/\.(json|wxml|wxss|js|wxs|ts|less|sass|scss)/.test(e)}function isMiniProgramCodeFile(e){return/\.(json|wxml|wxss|js|wxs)/.test(e)}function mapToMiniProgramFile(e){const a=path.posix.extname(e);return isMiniProgramCodeFile(a)?e:".ts"===a?e.substring(0,e.length-3)+".js":e.substring(0,e.length-5)+".wxss"}class CodeDenpendencyQualityChecker{constructor(e,a){if(this.analyzer=a,this.reservedFile=e=>e.indexOf(this.babelRoot)>=0,a.invalid)throw new Error("analyzer is invalid");if(!a.graph)throw new Error("analyzer graph is not built");this.babelRoot=(0,babel_helper_1.getBabelOutputPath)(e)}checkUnusedCodeFiles(){const e=this.analyzer.fileHelper.getFileList("").filter(e=>isCodeFile(path.posix.extname(e))),a=new Set;return this.analyzer.graph.modules.forEach(t=>{const s=e.find(e=>e===t.path);s&&a.add(s)}),e.forEach(e=>{this.reservedFile(e)&&a.add(e)}),e.filter(e=>!a.has(e))}checkUnusedMiniProgramCodeFiles(){if(0===this.analyzer.compilerPlugins.length)return this.checkUnusedCodeFiles();const e=this.analyzer.fileHelper.getFileList("").filter(e=>isCodeFile(path.posix.extname(e))),a=new Set;this.analyzer.graph.modules.forEach(t=>{const s=e.find(e=>e===t.path);s&&a.add(mapToMiniProgramFile(s))}),e.forEach(e=>{this.reservedFile(e)&&a.add(e)});const t=new Set;return e.forEach(e=>{const s=mapToMiniProgramFile(e);a.has(s)||t.add(s)}),Array.from(t.values())}checkOnlyUsedByOtherPackagesJs(){const e=this.graph.modules.filter(e=>"Js"===e.type&&this.isLocateInMainPackage(e)).filter(e=>{let a=!1;return e.parentDeps.forEach(e=>{e.originModule&&this.isLocateInMainPackage(e.originModule)&&(a=!0)}),!a}).map(e=>e.path).filter(e=>!this.reservedFile(e));return{size:this.caculateFilesSize(e),files:e}}checkMainPackageUnusedComponents(){const e=this.graph.modules.filter(e=>"Component"===e.type&&this.isLocateInMainPackage(e)).filter(e=>0===findParent(e,this.graph,e=>"Page"===e.type&&this.isLocateInMainPackage(e)||"MainPackage"===e.type,e=>"Page"===e.type).length);e.filter(e=>{for(const a of e.deps.values())if("Component"!==a.type&&a.module&&a.module.parentDeps.size>1)return!1;return!0});const a=this.collectCompsFiles(e);return{size:this.caculateFilesSize(a),files:a,comps:e.map(e=>e.path.replace(/\.json$/,""))}}async checkUnusedPlugins(){if("miniprogram"!==this.analyzer.type)throw new Error("checkUnusedPlugins only support miniprogram, the analyzer.type is "+this.analyzer.type);const e=new Set,a=await this.analyzer.fileHelper.getJSON("app.json");a.plugins&&Object.keys(a.plugins).forEach(t=>{const s=a.plugins[t].provider;this.graph.modules.find(e=>{var a,i;return(null===(a=e.usePlugins)||void 0===a?void 0:a.has(t))||(null===(i=e.usePlugins)||void 0===i?void 0:i.has(s))})||e.add(s)});const t=a.subpackages||a.subPackages;return t&&t.forEach(a=>{const t=a.root;a.plugins&&Object.keys(a.plugins).forEach(s=>{const i=a.plugins[s].provider;this.graph.modules.find(e=>{var a,r;return e.path.startsWith(t)&&((null===(a=e.usePlugins)||void 0===a?void 0:a.has(s))||(null===(r=e.usePlugins)||void 0===r?void 0:r.has(i)))})||e.add(i)})}),Array.from(e)}async checkUnusedComponents(){const e=this.graph.modules.filter(e=>"Page"===e.type||"Component"===e.type||"MainPackage"===e.type||"SubPackage"===e.type),a=[],t=new Set;await Promise.all(e.map(async e=>{let s={};try{s=await this.analyzer.fileHelper.getJSON(e.path)||{}}catch(e){}const i=e=>!!s.componentPlaceholder&&Object.values(s.componentPlaceholder).includes(e),r=a=>{var t;const s=e.findChild("Wxml");return!!s&&(!!(null===(t=s.useTags)||void 0===t?void 0:t.has(a))||!!s.findChildren("Wxml").find(e=>{var t;return null===(t=e.useTags)||void 0===t?void 0:t.has(a)}))},n=Array.from(e.deps).filter(e=>"Component"===e.type);for(const s of n){("MainPackage"===e.type||"SubPackage"===e.type||r(s.name)||(o=s.name,l=void 0,null===(l=e.useTags)||void 0===l?void 0:l.has(o))||i(s.name))&&s.module?t.add(s.module):a.push({page:e.path,tag:s.name,path:s.path||void 0})}var o,l}));return this.graph.modules.filter(e=>"Component"===e.type&&!t.has(e)).map(e=>e.path)}get graph(){return this.analyzer.graph}getSubpackageRoots(){if(this.subpackageRoots)return this.subpackageRoots;const e=this.analyzer.graph.modules.find(e=>"MainPackage"===e.type).findChildren("SubPackage");return this.subpackageRoots=e.map(e=>e.path),this.subpackageRoots}isLocateInMainPackage(e){return!this.getSubpackageRoots().find(a=>e.path.startsWith(a))}caculateFilesSize(e){return e.reduce((e,a)=>{const t=this.analyzer.fileHelper.stat(a);return(null==t?void 0:t.size)&&(e+=t.size),e},0)}caculateResourceFileSize(e){var a,t;let s=0;return null===(t=null===(a=this.analyzer.serialize())||void 0===a?void 0:a.files)||void 0===t||t.forEach(a=>{e.includes(a.ext)&&(s+=a.size)}),s}caculatePackageSize(){var e,a;const t={__APP__:0};return null===(a=null===(e=this.analyzer.serialize())||void 0===e?void 0:e.files)||void 0===a||a.forEach(e=>{e.subPackage?(void 0===t[e.subPackage]&&(t[e.subPackage]=0),t[e.subPackage]+=e.size):t.__APP__+=e.size}),t}collectCompsFiles(e){return e.map(e=>{const a=[e.path];return e.deps.forEach(e=>{var t;"Component"!==e.type&&e.module&&a.push(null===(t=e.module)||void 0===t?void 0:t.path)}),a}).reduce((e,a)=>e.concat(a),[])}}exports.CodeDenpendencyQualityChecker=CodeDenpendencyQualityChecker; }(require("licia/lazyImport")(require), require)