"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.random=void 0;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),tools_1=require("../../../../utils/tools"),customError_1=require("../../../../utils/customError"),debug_1=require("../../../../utils/debug"),sass=(0,debug_1.shouldRunInMainProcess)()?()=>{process.versions.electron="0.54.0";const s=require("sass");return delete process.versions.electron,s}:()=>require("sass");function random(){return(0,tools_1.generateMD5)(`${Math.random()}${Date.now()}`)}exports.random=random;const importWxssReg=/(?:^|\s)?(?:@import)(?:\s*)?(["'])([^"']+.wxss)\1(?:\s*);/g,importCssReg=/(?:^|\s)?(?:@import)(?:\s*)?(["'])([^"']+.css)\1(?:\s*);/g;async function sassRender(s,e){"undefined"!=typeof WEBIDE_WEBPECK_DEFINE_PLUGIN||sass().render(s,e)}function default_1(s){return{name:"summer-sass",resolveExt:{wxss:["sass","scss"]},async load(e,t){if(t.endsWith(".scss")||t.endsWith(".sass")){const o=(0,tools_1.pathRelative)(s.projectPath,t);let r=s.getFile("",o).toString();const a=s.getTargetPath(s.miniprogramRoot,"global");if(o!==a+".scss"&&o!==a+".sass"){let e=path_1.default.extname(o);const t=(0,tools_1.pathRelative)(s.miniprogramRoot,path_1.default.dirname(o))||".";s.exists(s.miniprogramRoot,"global"+e)||(e=".sass"===e?".scss":".sass"),s.exists(s.miniprogramRoot,"global"+e)&&(r=".sass"===e?`@use '${"."===t?".":(0,tools_1.pathRelative)(t,"./")}/global${e}'\n${r}`:`@use '${"."===t?".":(0,tools_1.pathRelative)(t,"./")}/global${e}';\n${r}`)}const i=[];r=r.replace(importWxssReg,(s,e,t)=>(i.push(t),s.replace(t,t.slice(0,-4)+"css")));const n=path_1.default.posix.dirname((0,tools_1.pathRelative)(s.projectPath,t));return new Promise((s,o)=>{const a=Date.now();sassRender({file:t,data:r,sourceMap:!0,sourceMapContents:!0,omitSourceMapUrl:!0,outFile:t.slice(0,-5)+".wxss",includePaths:this.rootPath?[this.rootPath]:[]},(r,l)=>{if(r){const s=(0,customError_1.makeCustomError)(r,customError_1.CustomErrors.SUMMER_PLUGIN_CODE_ERR);return void o(s)}if(!l)return void o(new Error("no result"));const u=l.css.toString("utf-8").replace(importCssReg,(s,e,t)=>{const o=t.slice(0,-3)+"wxss";return i.includes(o)?s.replace(t,o):s});if(l.stats.includedFiles.length>0)for(const s of l.stats.includedFiles)s!==t&&this.addWatchFile(s);const c=l.map?JSON.parse(l.map.toString("utf-8")):void 0;c&&(c.sourceRoot=n),s({targetPath:e,source:{sourceCode:u,inputMap:c},process:[{cost:Date.now()-a,pluginName:"summer-sass",action:"load"}]})})})}}}}exports.default=default_1;