import * as crypto from "crypto"; export function replaceImportFromStaticInJsCode( params: { ftlValuesGlobalName: string; jsCode: string; } ): { fixedJsCode: string; } { const { jsCode, ftlValuesGlobalName } = params; const fixedJsCode = jsCode!.replace( / [^ ]+"static\//g, ` window.${ftlValuesGlobalName}.url.resourcesPath + "/build/static/` ); return { fixedJsCode }; } export function replaceImportFromStaticInCssCode( params: { cssCode: string; } ): { fixedCssCode: string; cssGlobalsToDefine: Record; } { const { cssCode } = params; const cssGlobalsToDefine: Record = {}; new Set(cssCode.match(/url\(\/[^)]+\)[^;}]*/g) ?? []) .forEach(match => cssGlobalsToDefine[ "url" + crypto .createHash("sha256") .update(match) .digest("hex") .substring(0, 15) ] = match ); let fixedCssCode = cssCode; Object.keys(cssGlobalsToDefine).forEach( cssVariableName => //NOTE: split/join pattern ~ replace all fixedCssCode = fixedCssCode.split(cssGlobalsToDefine[cssVariableName]) .join(`var(--${cssVariableName})`) ); return { fixedCssCode, cssGlobalsToDefine }; } export function generateCssCodeToDefineGlobals( params: { cssGlobalsToDefine: Record; urlPathname: string; } ): { cssCodeToPrependInHead: string; } { const { cssGlobalsToDefine, urlPathname } = params; return { "cssCodeToPrependInHead": [ ":root {", ...Object.keys(cssGlobalsToDefine) .map(cssVariableName => [ `--${cssVariableName}:`, cssGlobalsToDefine[cssVariableName] .replace(new RegExp(`url\\(${urlPathname.replace(/\//g,"\\/")}`, "g"),"url(${url.resourcesPath}/build/") ].join(" ")) .map(line => ` ${line};`), "}" ].join("\n") }; }