"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.compileJS=compileJS;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),taskstatus_1=require("../../../../../utils/taskstatus"),workerThread_1=require("../../workerThread"),common_1=require("../../../../../utils/common"),config_1=require("../../../../../config/config"),tools_1=require("../../../../../utils/tools"),app_1=require("../../json/app"),common_2=require("../../json/common"),projectconfig_1=require("../../json/projectconfig"),game_1=tslib_1.__importDefault(require("../../json/game")),plugin_1=require("../../json/plugin/plugin");async function formatBabelRoot(e,t,o,r){const{type:a}=e;if(a===config_1.COMPILE_TYPE.miniProgram){const t=(0,app_1.getAppJSON)(e),a=(0,common_2.checkPagePathIsInIndependentSubpackage)(t,o);a&&(r=`${a.root}/${r}`),"object"==typeof t.functionalPages&&!0===t.functionalPages.independent&&o.startsWith("functional-pages/")&&(r="functional-pages/"+r),"string"==typeof t.openDataContext&&o.startsWith(t.openDataContext)&&(r=`${t.openDataContext}/${r}`),t.workers&&o.startsWith((0,tools_1.getWorkersPath)(t.workers))&&(r=`${(0,tools_1.getWorkersPath)(t.workers)}/${r}`)}else if(a===config_1.COMPILE_TYPE.miniGame){const t=await(0,game_1.default)(e),a=(0,common_2.checkFilePathIsInIndependentSubpackage)(t,o);a&&(r=`${a}/${r}`),"string"==typeof t.openDataContext&&o.startsWith(t.openDataContext)&&(r=`${t.openDataContext}/${r}`),t.workers&&o.startsWith((0,tools_1.getWorkersPath)(t.workers))&&(r=`${(0,tools_1.getWorkersPath)(t.workers)}/${r}`)}else if(a===config_1.COMPILE_TYPE.miniProgramPlugin||a===config_1.COMPILE_TYPE.miniGamePlugin){const t=await(0,plugin_1.getDevPluginJSON)(e);"string"==typeof t.workers&&o.startsWith(t.workers)&&(r=`${t.workers}/${r}`)}return(0,tools_1.normalizePath)(""+r)}async function compileJS(e,t,o){var r,a;const{setting:n={},onProgressUpdate:i=(()=>{}),root:s="",devToolsCompileCache:l}=o,c=path_1.default.posix.join(s,t);let g=[],u=o.babelRoot||"@babel/runtime";if(n.enhance){const o=await(0,projectconfig_1.getProjectConfigJSON)(e);g=(null===(a=null===(r=o.setting)||void 0===r?void 0:r.babelSetting)||void 0===a?void 0:a.ignore)||[],u=await formatBabelRoot(e,s,t,u)}const p=new taskstatus_1.TaskStatus(t),_=o.sourceCode?o.sourceCode:await e.getFile(s,t);async function f(){const o=await(0,workerThread_1.runTask)(workerThread_1.TASK_NAME.COMPILE_JS,{projectPath:e.projectPath,root:s,filePath:t,setting:n,code:_,babelRoot:u,babelIgnore:g},e=>{e===workerThread_1.ETaskStatus.progress?i(p):e===workerThread_1.ETaskStatus.done&&(p.done(),i(p))});return o.error&&(0,common_1.throwError)({msg:o.error.message,code:o.error.code,filePath:c}),o}let m={};if(l){const o=(0,tools_1.normalizePath)(path_1.default.posix.join(e.projectPath,s,t)),r=`${o}_${JSON.stringify(n)}`;m=await l.getFile(o,r),m&&!n.uglifyFileName||(m=await f(),l.setFile(o,m,r))}else m=await f();return Object.assign({filePath:t},m)}