"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.importWxssCssReg=exports.importWxssReg=void 0,exports.default=default_1;const tslib_1=require("tslib"),tools_1=require("../../../../utils/tools"),customError_1=require("../../../../utils/customError"),path_1=tslib_1.__importDefault(require("path")),less=()=>require("less");function default_1(s,t){return{name:"summer-less",resolveExt:{wxss:"less"},async load(e,o){var r,i,a;if(o.endsWith(".less")){const l=Date.now(),p=(0,tools_1.pathRelative)(s.projectPath,o);let u=s.getFile("",p).toString();if(p!==s.getTargetPath(s.miniprogramRoot,"app")+".less"){const t=(null===(i=null===(r=s.setting)||void 0===r?void 0:r.lessSetting)||void 0===i?void 0:i.commonUseFilePath)||"app.less";if(s.exists(s.miniprogramRoot,t)){const e=(0,tools_1.pathRelative)(s.miniprogramRoot,path_1.default.dirname(p))||".";u=`@import (optional, reference) '${"."===e?".":(0,tools_1.pathRelative)(e,"./")}/${t}';\n${u}`}}const c=[];u=u.replace(exports.importWxssReg,(s,t,e)=>(c.push(e),s.replace(e,e+"?css")));try{const r=await require("less").render(u,{sourceMap:{outputSourceFiles:!0},paths:this.rootPath?[this.rootPath]:[],filename:o,globalVars:null!==(a=null==t?void 0:t.globalVars)&&void 0!==a?a:{}}),i=r.css.replace(exports.importWxssCssReg,(s,t,e)=>{const o=e.slice(0,-4);return c.includes(o)?s.replace(e,o):s});if(r.imports.length)for(const s of r.imports)this.addWatchFile(s);let p=void 0;return r.map&&(p=JSON.parse(r.map),p.sources=p.sources.map(t=>path_1.default.posix.relative(s.projectPath,t))),{targetPath:e,source:{sourceCode:i,inputMap:p},process:[{cost:Date.now()-l,pluginName:"summer-less",action:"load"}]}}catch(s){throw(0,customError_1.makeCustomError)(s,customError_1.CustomErrors.SUMMER_PLUGIN_CODE_ERR)}}}}}exports.importWxssReg=/(?:^|\s)?(?:@import)(?:\s*)?(["'])([^"']+.wxss)\1(?:\s*);/g,exports.importWxssCssReg=/(?:^|\s)?(?:@import)(?:\s*)?(["'])([^"']+.wxss\?css)\1(?:\s*);/g;