Skip to content

Commit f0fd993

Browse files
authored
Merge pull request #248 from Juice10/main
2 parents 8280769 + 07559f7 commit f0fd993

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

lib/image.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ function encodeSvg(content) {
2727
);
2828
}
2929

30-
async function reduceAsync(array = [], reducer = (r) => r, initial) {
31-
for (const index of array.keys()) {
32-
initial = await reducer(initial, array[index]); /* eslint-disable-line no-await-in-loop */
33-
}
34-
35-
return initial;
36-
}
3730

3831
async function assertSize(resource, maxFileSize, throwError = true) {
3932
const {mime, contents = ''} = resource || {};
@@ -77,24 +70,22 @@ async function resolve(filepath, {assetPaths, maxFileSize, largeFileCallback, re
7770
}
7871
}
7972

80-
function getDataUriMapping(urls = [], options = {}) {
81-
return reduceAsync(
82-
[...new Set(urls)],
83-
async (result, url) => {
84-
const file = await resolve(url, options);
85-
if (file && file.mime && /image/.test(file.mime)) {
86-
result[url] = await getDataUri(file, options);
87-
} else if (options.largeFileCallback) {
88-
const largeFile = await resolve(url, {...options, maxFileSize: 0});
89-
if (largeFile && largeFile.mime && /image/.test(largeFile.mime)) {
90-
result[url] = await options.largeFileCallback(largeFile);
91-
}
73+
async function getDataUriMapping(urls = [], options = {}) {
74+
const uniqueUrls = [...new Set(urls)];
75+
const promises = uniqueUrls.map(async (url) => {
76+
const file = await resolve(url, options);
77+
if (file && file.mime && /image/.test(file.mime)) {
78+
return [url, await getDataUri(file, options)];
79+
} else if (options.largeFileCallback) {
80+
const largeFile = await resolve(url, {...options, maxFileSize: 0});
81+
if (largeFile && largeFile.mime && /image/.test(largeFile.mime)) {
82+
return [url, await options.largeFileCallback(largeFile)];
9283
}
93-
94-
return result;
95-
},
96-
{}
97-
);
84+
}
85+
return [url, null];
86+
});
87+
const results = await Promise.all(promises);
88+
return Object.fromEntries(results);
9889
}
9990

10091
module.exports.getDataUriMapping = getDataUriMapping;

0 commit comments

Comments
 (0)