version>1.5.0-SNAPSHOT

This commit is contained in:
Булат Хайруллин 2024-09-25 16:43:51 +03:00
parent 2312476565
commit d3047eb80d
382 changed files with 54072 additions and 15705 deletions

View file

@ -0,0 +1,33 @@
const fs = require('fs');
const srcUrlRegex = /url\((\\?["'])?(?!data:)\S+(\\?["'])?\)/g;
export function normalizeCssPaths(params: {paths: string[], outDir: string}) {
params.paths = params.paths ? params.paths : [];
params.paths.forEach(path => normalizeCssPath(path, params.outDir));
}
function normalizeCssPath(path: string, outputDirectory: string) {
console.log(`Start processing ${path}`);
const css: string = fs.readFileSync(path, 'utf8');
let counter = 0;
const processedCss = css.replace(srcUrlRegex, (srcUrl: string) => {
if (srcUrl.search(outputDirectory) != -1) return srcUrl;
let fileName = getFileName(srcUrl);
let processedUrl = `url('${outputDirectory}/${fileName}')`;
counter++;
console.log(`Replaced ${srcUrl} -> ${processedUrl}`);
return processedUrl;
});
console.log(`Replaced ${counter} urls`);
fs.writeFileSync(path, processedCss);
}
function getFileName(srcUrl: string): string {
let url = srcUrl.substring(4, srcUrl.length - 1); // unbox 'url(...)'
url = url.replace(/(\\?["'])/g, '');
let urlPaths = url.split('/');
return urlPaths[urlPaths.length - 1].split('?')[0];
}