"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getSummerCompiler=exports.summerCompilerFactory=exports.SummerCompilerFactory=void 0;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),lodash_1=tslib_1.__importDefault(require("lodash")),config_1=require("../config/config"),ciProject_1=require("../project/ciProject"),tools_1=require("../utils/tools"),locales_1=tslib_1.__importDefault(require("../utils/locales/locales")),packOptionsHelper_1=tslib_1.__importDefault(require("../utils/packOptionsHelper")),summerCompiler_1=require("./corecompiler/summerCompiler"),pluginconfig_1=require("./corecompiler/summer/pluginconfig"),WeappCompileCache="";class SummerCompilerFactory{constructor(){this.summerCompiler=null,this.project=null,this.createPromise=null,this.releaseCbs=[]}async getSummerCompiler(e,r){return this.shouldCreate(e)?(this.releaseLastCompiler(),this.project=e,this.summerCompiler=null,this.createPromise=this.createSummerCompiler(e,r),this.summerCompiler=await this.createPromise,this.summerCompiler.setLocale(locales_1.default.getLocale())):(this.summerCompiler||(this.summerCompiler=await this.createPromise),this.summerCompiler.updateOptions(this.getSummerOptions(e))),this.summerCompiler}shouldCreate(e){var r,t,i,o,s;if(!this.project)return!0;if(this.project.projectPath!==e.projectPath)return!0;if(!lodash_1.default.isEqual(this.project.packOptions,e.packOptions))return!0;const l=["es6","minified","postcss","uglifyFileName","compileWorklet","enhance","minifyWXML","minifyWXSS","disableUseStrict","swc","useCompilerPlugins","babelSetting","swcSetting","minifyWXMLSetting","condition"],m=null!==(t=null===(r=this.project)||void 0===r?void 0:r.setting)&&void 0!==t?t:{},p=null!==(i=e.setting)&&void 0!==i?i:{};for(const e of l){const r=m[e],t=p[e];if(!lodash_1.default.isEqual(r,t))return console.info(`shouldCreate return true, project.setting.${e}, before: ${JSON.stringify(r)}, after: ${JSON.stringify(t)}`),!0}return(null===(o=this.project)||void 0===o?void 0:o.miniprogramRoot)!==e.miniprogramRoot||(null===(s=this.project)||void 0===s?void 0:s.pluginRoot)!==e.pluginRoot}async releaseLastCompiler(){const{releaseCbs:e}=this;if(this.releaseCbs=[],this.createPromise)try{(await this.createPromise).destroy(),e.forEach(e=>{try{e()}catch(e){console.error("[SummerCompilerFactory] releaseLastCompiler release cb execute error: "+e)}})}catch(e){console.error("[SummerCompilerFactory] releaseLastCompiler error: "+e)}}ensureCacheDir(){const e=path_1.default.join("",(0,tools_1.generateMD5)(this.project.projectPath+"|summer"));return(0,tools_1.mkdirSync)(e),e}async createSummerCompiler(e,r){const t=new ciProject_1.CIProject(e);await t.ready();const i=this.ensureCacheDir(),o=this.getSummerOptions(e),s=new summerCompiler_1.SummerCompiler(t,i,o,{showBuildLog:()=>{}},r);return await s.ready(),s}getFileFilter(e){packOptionsHelper_1.default.updateState(e);return r=>{const t=r.startsWith(e.miniprogramRoot)&&packOptionsHelper_1.default.isIgnoredByRules(r.slice(e.miniprogramRoot.length)),i=e.pluginRoot&&r.startsWith(e.pluginRoot)&&packOptionsHelper_1.default.isIgnoredByRules(r.slice(e.pluginRoot.length));return!t&&!i}}getSummerOptions(e){const r=(0,pluginconfig_1.getSummerPluginConfig)(e.setting),t={[config_1.compileTypeConfig.weapp]:"miniProgram",[config_1.compileTypeConfig.plugin]:"miniProgramPlugin",[config_1.compileTypeConfig.game]:"miniGame",[config_1.compileTypeConfig.gamePlugin]:"miniGamePlugin"}[e.compileType];return{appid:e.appid,attr:e.attr,projectArchitecture:e.projectArchitecture,miniprogramRoot:e.miniprogramRoot,pluginRoot:e.pluginRoot,compileType:t,summerPlugins:r,setting:Object.assign({},e.setting)}}}exports.SummerCompilerFactory=SummerCompilerFactory,exports.summerCompilerFactory=new SummerCompilerFactory,exports.getSummerCompiler=exports.summerCompilerFactory.getSummerCompiler.bind(exports.summerCompilerFactory);