UNPKG

1.39 kBJavaScriptView Raw
1import { isDataUrl, getMimeTypeFromSource, getEscapedString, resolveUrl, getSourceAndEncodeAsync, formatDataAsUrl, } from './Utils.web';
2const URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/g;
3export function shouldProcess(string) {
4 if (!string.search) {
5 return false;
6 }
7 return string.search(URL_REGEX) !== -1;
8}
9export async function batchProcessAllSourcesAsync(string, baseUrl, get) {
10 if (!shouldProcess(string)) {
11 return string;
12 }
13 const urls = readURLs(string);
14 let done = Promise.resolve(string);
15 for (const url of urls) {
16 done = done.then(string => processURLAsync(string, url, baseUrl, get));
17 }
18 return done;
19}
20function readURLs(urls) {
21 const result = [];
22 let match;
23 while ((match = URL_REGEX.exec(urls)) !== null) {
24 result.push(match[1]);
25 }
26 return result.filter(url => !isDataUrl(url));
27}
28async function processURLAsync(string, url, baseUrl = undefined, getSourceAsync = getSourceAndEncodeAsync) {
29 const finalURL = baseUrl ? resolveUrl(url, baseUrl) : url;
30 const data = await getSourceAsync(finalURL);
31 const dataUrl = formatDataAsUrl(data, getMimeTypeFromSource(url));
32 return string.replace(URLAsRegex(url), `$1${dataUrl}$3`);
33}
34function URLAsRegex(url) {
35 return new RegExp(`(url\\(['"]?)(${getEscapedString(url)})(['"]?\\))`, 'g');
36}
37//# sourceMappingURL=ProcessSources.web.js.map
\No newline at end of file