"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getBabelRoot=getBabelRoot,exports.isNotIgnoredByProjectConfig=isNotIgnoredByProjectConfig,exports.compileOther=compileOther,exports.compileJSFiles=compileJSFiles,exports.compileWXSSFiles=compileWXSSFiles,exports.compileWXMLFiles=compileWXMLFiles,exports.getUploadProjectConfig=getUploadProjectConfig;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),tools_1=require("../../../../utils/tools"),taskstatus_1=require("../../../../utils/taskstatus"),index_1=require("../workerThread/index"),helper_util_1=require("../../../../utils/helper_util"),projectconfig_1=require("../json/projectconfig"),game_1=tslib_1.__importDefault(require("../json/game")),signaturejson_1=require("../json/signaturejson"),config_1=require("../../../../config/config"),taskmanager_1=tslib_1.__importDefault(require("../../../../utils/taskmanager")),wxml_1=require("./handler/wxml"),wxss_1=require("./handler/wxss"),js_1=require("./handler/js");async function getBabelRoot(o){var t,e;const i=(0,projectconfig_1.getProjectConfigJSON)(o);let n=null===(e=null===(t=null==i?void 0:i.setting)||void 0===t?void 0:t.babelSetting)||void 0===e?void 0:e.outputPath;return n?(n=(0,tools_1.normalizePath)(n),n.replace(/(^[./\\])|(\/$)/g,""),n):"@babel/runtime"}function isNotIgnoredByProjectConfig(o,t,e){var i,n;const s=path_1.default.posix.relative(t,e),a=(null===(i=o.packOptions)||void 0===i?void 0:i.include)||[];if((0,tools_1.isFileIncluded)(s,a))return!0;const l=(null===(n=o.packOptions)||void 0===n?void 0:n.ignore)||[];return!(0,tools_1.isFileIgnored)(path_1.default.posix.relative(t,e),l)}async function copyFile(o,t){return{filePath:t,code:o.getFile("",t)}}async function compileOther(o,t,e){const{onProgressUpdate:i=(()=>{})}=e,n=new taskstatus_1.TaskStatus("compiling other files");i(n);const s=new taskmanager_1.default;for(const e of t)s.addTask(copyFile,o,e);const a=await s.runAllAsync(),l={};for(const o of a){const{code:t,filePath:e}=o;t&&(l[e]=t)}return n.done(),i(n),l}async function canWeCompileJS(o,t,e){let i;for(const o of t)if((0,tools_1.normalizePath)(e).startsWith((0,tools_1.normalizePath)((0,signaturejson_1.trailing)(o.fullPath,"/")))){i=o;break}if(i){return i.signature.findIndex(o=>(0,tools_1.normalizePath)(o.fullPath)===(0,tools_1.normalizePath)(e))>=0}return!0}async function compileJSFiles(o,t,e,i){const{setting:n={}}=i;let s="@babel/runtime";n.enhance&&(s=await getBabelRoot(o));const a=[];if(o.type===config_1.COMPILE_TYPE.miniGame||o.type===config_1.COMPILE_TYPE.miniGamePlugin){const n=await(0,signaturejson_1.getAllPluginSignatures)(o);for(const l of t){const t=path_1.default.join(o.projectPath,e,l);await canWeCompileJS(o,n,t)&&a.push((0,js_1.compileJS)(o,l,Object.assign(Object.assign({},i),{babelRoot:s,root:e})))}}else for(const n of t)a.push((0,js_1.compileJS)(o,n,Object.assign(Object.assign({},i),{babelRoot:s,root:e})));let l=[];try{l=await Promise.all(a)}catch(o){throw(0,index_1.abortTask)(index_1.TASK_NAME.COMPILE_JS),o}const r={},c=new Set;for(const o of l){const{filePath:t,map:i,code:n,helpers:s}=o,a=(0,tools_1.formatSourceMap)(i);void 0!==n&&(r[path_1.default.posix.normalize(path_1.default.posix.join(e,t))]=n),a&&(r[path_1.default.posix.normalize(path_1.default.posix.join(e,t+".map"))]=a),s.length>0&&s.forEach(o=>{c.add(o)})}return await(0,helper_util_1.appendHelpers)(o,c,path_1.default.join(e),r),r}async function compileWXSSFiles(o,t,e,i){const n=[];for(const s of t)n.push((0,wxss_1.compileWXSS)(o,s,Object.assign(Object.assign({},i),{root:e})));let s=[];try{s=await Promise.all(n)}catch(o){throw(0,index_1.abortTask)(index_1.TASK_NAME.COMPILE_WXSS),o}const a={};for(const o of s){const{filePath:t,code:i}=o;void 0!==i&&(a[path_1.default.posix.normalize(path_1.default.posix.join(e,t))]=i)}return a}async function compileWXMLFiles(o,t,e,i){const n=[];for(const s of t)n.push((0,wxml_1.compileWXML)(o,s,Object.assign(Object.assign({},i),{root:e})));let s=[];try{s=await Promise.all(n)}catch(o){throw(0,index_1.abortTask)(index_1.TASK_NAME.MINIFY_WXML),o}const a={};for(const o of s){const{filePath:t,code:i}=o;void 0!==i&&(a[path_1.default.posix.normalize(path_1.default.posix.join(e,t))]=i)}return a}async function getUploadProjectConfig(o,t){const e={miniprogramRoot:t.miniprogramRoot,localPlugins:[]};t.pluginRoot&&(e.pluginRoot=t.pluginRoot);const i=(0,game_1.default)(o),n=(o,t="")=>{const i=e.localPlugins||[];for(const e in o)if(o.hasOwnProperty(e)&&o[e]&&"string"==typeof o[e].path){const n=o[e],s=path_1.default.posix.normalize(path_1.default.posix.join(t,n.path.replace(/\\/g,"/")).replace(/^\/+/,""));i.push({alias:e,provider:n.provider,path:s})}e.localPlugins=i};if(i.plugins){const{plugins:o}=i;n(o)}const s=i.subpackages||i.subPackages;if(Array.isArray(s))for(const o of s)(null==o?void 0:o.plugins)&&n(o.plugins,o.root||"");return e}