| 123 |
- !function(require, directRequire){
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkCodeQuality=exports.transCompileType=void 0;const tslib_1=require("tslib"),projectconfig_1=require("../core/json/projectconfig"),code_analyse_1=require("../vendor/code-analyse"),path_1=tslib_1.__importDefault(require("path")),qualitycheckoption_1=tslib_1.__importDefault(require("./utils/qualitycheckoption")),locales_1=tslib_1.__importDefault(require("../utils/locales/locales")),fomatable_string_1=require("../utils/locales/fomatable_string"),codeDenpendencyQualityChecker_1=require("./utils/codeDenpendencyQualityChecker");function transCompileType(e){if("miniProgram"===e.type)return"miniprogram";if("miniProgramPlugin"===e.type)return"plugin";if("miniGame"===e.type)return"game";if("miniGamePlugin"===e.type)return"gamePlugin";throw new Error("unknown compile type "+e.type)}async function checkCodeQuality(e){const t=transCompileType(e);if("gamePlugin"===t)throw new Error("gamePlugin is not support yet!");if("game"===t)throw new Error("game is not support yet!");if("plugin"===t)throw new Error("plugin is not support yet!");const s=new QualityChecker;return await s.check({project:e}),s.getResult()}exports.transCompileType=transCompileType,exports.checkCodeQuality=checkCodeQuality;const formatDesc=(e,...t)=>{const s=locales_1.default.getLocale();e.text="en"===s?new fomatable_string_1.FormatableString(e.descEn||"").format(...t):new fomatable_string_1.FormatableString(e.desc||"").format(...t)};class QualityChecker{constructor(){this.result=[],this._checkConfig=qualitycheckoption_1.default,this.checkerHandlerMap=new Map,this._codeDepsCheckerPromise=null,this.checkJSCompressIsOpen=async(e,t)=>{var s;let c=!1;formatDesc(e);return c=!!(null===(s=(await(0,projectconfig_1.getProjectConfigJSON)(t.project)).setting)||void 0===s?void 0:s.minified),c},this.checkWXMLCompressIsOpen=async(e,t)=>{var s;let c=!1;formatDesc(e);return c=!!(null===(s=(await(0,projectconfig_1.getProjectConfigJSON)(t.project)).setting)||void 0===s?void 0:s.minifyWXML),c},this.checkWXSSCompressIsOpen=async(e,t)=>{var s;let c=!1;formatDesc(e);return c=!!(null===(s=(await(0,projectconfig_1.getProjectConfigJSON)(t.project)).setting)||void 0===s?void 0:s.minifyWXSS),c},this.checkLazyCodeLoadingIsOpen=async(e,t)=>{let s=!1;formatDesc(e);const c=await(0,projectconfig_1.getProjectConfigJSON)(t.project),{miniprogramRoot:i=""}=c,n=await t.project.getFile(i,"app.json");return s=!!JSON.parse(n.toString("utf-8")).lazyCodeLoading,s},this.checkImageAndAudioSizeLimit=async(e,t)=>{const s=await this.getCodeDepsChecker(t),c=e.limit||200,i=s.caculateResourceFileSize(this._checkConfig.regList.IMAGE_AND_AUDIO_LIMIT)/1024<c;return formatDesc(e,""+c),i},this.checkPackageSizeLimit=async(e,t)=>{const s=(await this.getCodeDepsChecker(t)).caculatePackageSize(),c=e.limit||1.5,i=1024*c;let n=!0;formatDesc(e,""+c);for(const e of Object.values(s))e/1024>i&&(n=!1);return e.detail=s,n},this.getCodeDepsChecker=async e=>{const t=e.project;return this._codeDepsCheckerPromise||(this._codeDepsCheckerPromise=new Promise((s,c)=>{let i=(0,projectconfig_1.getProjectConfigJSON)(t).miniprogramRoot;i=i?path_1.default.posix.join(t.projectPath,i):t.projectPath;const n=new code_analyse_1.Analyzer({root:i,type:"miniprogram",plugins:[]});n.analyse().then(()=>{s(new codeDenpendencyQualityChecker_1.CodeDenpendencyQualityChecker(e.project,n))}).catch(e=>{c(e)})})),this._codeDepsCheckerPromise},this.checkJSDepsOnlyUsedByOtherSubPkg=async(e,t)=>{const s=(await this.getCodeDepsChecker(t)).checkOnlyUsedByOtherPackagesJs();e.detail=s,formatDesc(e,":");let c=!0;return s.size>0&&s.files.length>0&&(c=!1),c},this.checkComponentDepsOnlyUsedByOtherSubPkg=async(e,t)=>{const s=(await this.getCodeDepsChecker(t)).checkMainPackageUnusedComponents();return e.detail=s,formatDesc(e,":"),!0},this.checkUnusedPlugins=async(e,t)=>{const s=await this.getCodeDepsChecker(t),c=await s.checkUnusedPlugins();e.detail=c;let i=!0;return formatDesc(e,""+c.join(" , ")),c.length>0&&(i=!1),i},this.checkUnusedComponents=async(e,t)=>{const s=await this.getCodeDepsChecker(t),c=await s.checkUnusedComponents();e.detail=c,formatDesc(e,":");let i=!0;return c.length>0&&(i=!1),i},this.checkUnusedCodes=async(e,t)=>{var s,c;const i=(await this.getCodeDepsChecker(t)).checkUnusedCodeFiles();e.detail=i,formatDesc(e,i.length);let n=!0;i.length>0&&(n=!1);const r=await(0,projectconfig_1.getProjectConfigJSON)(t.project);return!(!1===(null===(s=r.setting)||void 0===s?void 0:s.ignoreDevUnusedFiles)||!1===(null===(c=r.setting)||void 0===c?void 0:c.ignoreUploadUnusedFiles))||n},this.checkerHandlerMap.set("JS_COMPRESS_OPEN",this.checkJSCompressIsOpen),this.checkerHandlerMap.set("WXML_COMPRESS_OPEN",this.checkWXMLCompressIsOpen),this.checkerHandlerMap.set("WXSS_COMPRESS_OPEN",this.checkWXSSCompressIsOpen),this.checkerHandlerMap.set("LAZYCODE_LOADING_OPEN",this.checkLazyCodeLoadingIsOpen),this.checkerHandlerMap.set("IMAGE_AND_AUDIO_LIMIT",this.checkImageAndAudioSizeLimit),this.checkerHandlerMap.set("PACKAGE_SIZE_LIMIT",this.checkPackageSizeLimit),this.checkerHandlerMap.set("CONTAINS_OTHER_PKG_JS",this.checkJSDepsOnlyUsedByOtherSubPkg),this.checkerHandlerMap.set("CONTAINS_OTHER_PKG_COMPONENTS",this.checkComponentDepsOnlyUsedByOtherSubPkg),this.checkerHandlerMap.set("CONTAINS_UNUSED_COMPONENTS",this.checkUnusedComponents),this.checkerHandlerMap.set("CONTAINS_UNUSED_PLUGINS",this.checkUnusedPlugins),this.checkerHandlerMap.set("CONTAINS_UNUSED_CODES",this.checkUnusedCodes)}async check(e,t){t||(t=[...this.checkerHandlerMap.keys()]);const s=await this.getCheckList(t);return await this.checkList(s,e)}async getCheckList(e){var t;const s=new Set(e);try{return((null===(t=this._checkConfig)||void 0===t?void 0:t.checkList)||[]).filter(e=>s.has(e.name||"")).filter(e=>e.enabled)}catch(e){return console.log(e),[]}}async checkList(e,t){if(!(null==e?void 0:e.length))return[];const s=[];e.forEach(e=>{s.push(this.checkItem(e,t))});const c=await Promise.all(s);return this.result=c.filter(e=>null!==e).map(e=>{const{name:t,success:s,text:c,docURL:i,detail:n}=e;return{name:t,success:s,text:c,docURL:i,detail:n}}),this._codeDepsCheckerPromise=null,this.result}async checkItem(e,t){const s=e.name;if(s&&this.checkerHandlerMap.has(s)){const c=this.checkerHandlerMap.get(s);let i=!1;try{i=await c(e,t)}catch(e){i=!0,console.error(e)}return Object.assign(Object.assign({},e),{success:i})}return null}getResult(){return this.result}}
- }(require("licia/lazyImport")(require), require)
|