|
| 1 | +import{Element,load}from"cheerio"; |
| 2 | +import{parse,stringify}from"css"; |
| 3 | +importMQSfrom"css-mediaquery"; |
| 4 | +import{readFileSync}from"fs"; |
| 5 | +import{readFile,writeFile}from"fs/promises"; |
| 6 | +import{cloneDeep}from"lodash"; |
| 7 | +import{basename,dirname,join,relative,resolve}from"path"; |
| 8 | +importconfigurationsfrom"../configLoader"; |
| 9 | +import{screenCats}from"./options"; |
| 10 | +import{makeDirf}from"./utils"; |
| 11 | +const{ screenSizes}=configurations; |
| 12 | + |
| 13 | +function_getCSSLinks(htmlFilePath:string):Promise<string[]>{ |
| 14 | +htmlFilePath=resolve(htmlFilePath);//making abs path |
| 15 | + |
| 16 | +constcssLinks:string[]=[]; |
| 17 | + |
| 18 | +returnnewPromise((resolve,reject)=>{ |
| 19 | +readFile(htmlFilePath,{encoding:"utf8"}) |
| 20 | +.then((htmlContent:string)=>{ |
| 21 | +const$=load(htmlContent); |
| 22 | + |
| 23 | +$('link[rel="stylesheet"]').each((_index:number,element:Element)=>{ |
| 24 | +cssLinks.push( |
| 25 | +join(dirname(htmlFilePath),$(element).attr("href")??"") |
| 26 | +); |
| 27 | +}); |
| 28 | + |
| 29 | +resolve(cssLinks); |
| 30 | +}) |
| 31 | +.catch(reject); |
| 32 | +}); |
| 33 | +} |
| 34 | + |
| 35 | +function_writeCSS( |
| 36 | +newCssFilePath:string, |
| 37 | +stylesheet:Record<string,any> |
| 38 | +):Promise<void>{ |
| 39 | +returnnewPromise((resolve,reject)=>{ |
| 40 | +makeDirf(dirname(newCssFilePath)); |
| 41 | + |
| 42 | +writeFile(newCssFilePath,stringify(stylesheet),{encoding:"utf8"}) |
| 43 | +.then(resolve) |
| 44 | +.catch(reject); |
| 45 | +}); |
| 46 | +} |
| 47 | + |
| 48 | +function_replaceCSSLinks( |
| 49 | +htmlPath:string, |
| 50 | +destinationPath:string, |
| 51 | +newcssLinks:string[] |
| 52 | +):Promise<void>{ |
| 53 | +returnnewPromise((resolve,reject)=>{ |
| 54 | +readFile(htmlPath,{encoding:"utf8"}) |
| 55 | +.then((htmlString:string)=>{ |
| 56 | +const$=load(htmlString); |
| 57 | + |
| 58 | +//remove old links |
| 59 | +$('link[rel="stylesheet"]').remove(); |
| 60 | + |
| 61 | +for(constcssLinkofnewcssLinks){ |
| 62 | +constmediaquery:string=`screen and (max-width:${ |
| 63 | +/*@ts-ignore */ |
| 64 | +screenSizes[basename(cssLink,".css").slice(-2)] |
| 65 | +}px)`; |
| 66 | + |
| 67 | +constcsstag=$( |
| 68 | +`<link rel="stylesheet" href="${cssLink}" media="${mediaquery}">` |
| 69 | +); |
| 70 | + |
| 71 | +$("head").append(csstag); |
| 72 | +} |
| 73 | + |
| 74 | +makeDirf(dirname(destinationPath)); |
| 75 | + |
| 76 | +//write to files |
| 77 | +writeFile(destinationPath,$.html()??"") |
| 78 | +.then(resolve) |
| 79 | +.catch(reject); |
| 80 | +}) |
| 81 | +.catch(reject); |
| 82 | +}); |
| 83 | +} |
| 84 | + |
| 85 | +exportasyncfunctiondivider( |
| 86 | +htmlFilePath:string, |
| 87 | +destinationBasePath:string |
| 88 | +){ |
| 89 | +constdestinationHtmlPath:string=join( |
| 90 | +destinationBasePath, |
| 91 | +relative(process.cwd(),htmlFilePath) |
| 92 | +); |
| 93 | + |
| 94 | +//getting css link from html |
| 95 | +constoldCssLinks:string[]=await_getCSSLinks(htmlFilePath); |
| 96 | + |
| 97 | +//combined CSS metas |
| 98 | +letcombinedCSSContent:string=""; |
| 99 | +letcombinedFileName:string=""; |
| 100 | + |
| 101 | +oldCssLinks.forEach((cssLink:string)=>{ |
| 102 | +constcssContent:string=readFileSync(cssLink,{encoding:"utf8"}); |
| 103 | + |
| 104 | +combinedFileName=`${combinedFileName}-${basename(cssLink,".css")}`; |
| 105 | +combinedCSSContent=combinedCSSContent+"\n"+cssContent; |
| 106 | +}); |
| 107 | + |
| 108 | +//setting reciept watermark in css filename |
| 109 | +constwatermark:string="~div-js"; |
| 110 | +combinedFileName=combinedFileName+watermark; |
| 111 | + |
| 112 | +//combined css getting splitted by diff screen dependency |
| 113 | +constrenderedCSS=parse(combinedCSSContent); |
| 114 | + |
| 115 | +//get media blocks with min or max width conditions |
| 116 | +constcssWithMediaQueries= |
| 117 | +cloneDeep(renderedCSS).stylesheet?.rules.filter( |
| 118 | +(rule)=> |
| 119 | +rule.type==="media"&& |
| 120 | +/*@ts-ignore */ |
| 121 | +(rule.media.includes("max-width")||rule.media.includes("min-width")) |
| 122 | +)??([]asany); |
| 123 | + |
| 124 | +//Non-Device dependant stylesheet |
| 125 | +constcommonCSS=cloneDeep(renderedCSS); |
| 126 | +/*@ts-ignore */ |
| 127 | +commonCSS.stylesheet.rules=commonCSS.stylesheet?.rules.filter((rule)=>{ |
| 128 | +if( |
| 129 | +//retain all possible rules except media |
| 130 | +rule.type==="rule"|| |
| 131 | +rule.type==="keyframes"|| |
| 132 | +rule.type==="font-face"|| |
| 133 | +rule.type==="supports"|| |
| 134 | +rule.type==="page"|| |
| 135 | +rule.type==="charset"|| |
| 136 | +rule.type==="import"|| |
| 137 | +rule.type==="document"|| |
| 138 | +rule.type==="namespace" |
| 139 | +){ |
| 140 | +returntrue; |
| 141 | +}elseif( |
| 142 | +//retain media css if that is not about max and min width |
| 143 | +rule.type==="media"&& |
| 144 | +/*@ts-ignore */ |
| 145 | +!rule.media.includes("max-width")&& |
| 146 | +/*@ts-ignore */ |
| 147 | +!rule.media.includes("min-width") |
| 148 | +){ |
| 149 | +returntrue; |
| 150 | +} |
| 151 | + |
| 152 | +returnfalse; |
| 153 | +})asany; |
| 154 | + |
| 155 | +//Device dependant stylesheets |
| 156 | +constdeviceDependantStylesheets:Partial<Record<screenCats,any>>={}; |
| 157 | + |
| 158 | +//list for newly generated links |
| 159 | +constnewCssLinks:string[]=[]; |
| 160 | + |
| 161 | +//Iterations on Different ScreenSizes |
| 162 | +for(constscreenKeyofObject.keys(screenSizes)){ |
| 163 | +//copy of original css |
| 164 | +deviceDependantStylesheets[screenKeyasscreenCats]= |
| 165 | +cloneDeep(renderedCSS); |
| 166 | + |
| 167 | +//filter to retain device dependant stylesheet |
| 168 | +deviceDependantStylesheets[screenKeyasscreenCats].stylesheet.rules= |
| 169 | +cloneDeep(cssWithMediaQueries).filter((rule:any)=>{ |
| 170 | +//emulating media query function |
| 171 | +constmediaQueryMatched:boolean=MQS.match(rule.media,{ |
| 172 | +/*@ts-ignore */ |
| 173 | +type:"screen", |
| 174 | +width:`${screenSizes[screenKeyasscreenCats]}px`, |
| 175 | +}); |
| 176 | + |
| 177 | +returnmediaQueryMatched; |
| 178 | +}); |
| 179 | + |
| 180 | +//combine common and deviceDependant |
| 181 | +deviceDependantStylesheets[screenKeyasscreenCats].stylesheet.rules= |
| 182 | +deviceDependantStylesheets[ |
| 183 | +screenKeyasscreenCats |
| 184 | +].stylesheet.rules.concat(commonCSS.stylesheet?.rules??[]); |
| 185 | + |
| 186 | +constdirNameofCSS:string=relative( |
| 187 | +process.cwd(), |
| 188 | +dirname(oldCssLinks[0]) |
| 189 | +); |
| 190 | + |
| 191 | +constnewCssFilePath:string=join( |
| 192 | +destinationBasePath, |
| 193 | +dirNameofCSS, |
| 194 | +`${combinedFileName}@${screenKey}.css` |
| 195 | +); |
| 196 | + |
| 197 | +constrelativeCssPath:string=relative( |
| 198 | +dirname(destinationHtmlPath), |
| 199 | +newCssFilePath |
| 200 | +); |
| 201 | + |
| 202 | +newCssLinks.push(relativeCssPath); |
| 203 | + |
| 204 | +await_writeCSS( |
| 205 | +newCssFilePath, |
| 206 | +deviceDependantStylesheets[screenKeyasscreenCats] |
| 207 | +); |
| 208 | +} |
| 209 | + |
| 210 | +returnnewPromise((resolve,reject)=>{ |
| 211 | +//replace links |
| 212 | +_replaceCSSLinks(htmlFilePath,destinationHtmlPath,newCssLinks) |
| 213 | +.then(resolve) |
| 214 | +.catch(reject); |
| 215 | +}); |
| 216 | +} |