Handle resolving JSDOM promises better

This commit is contained in:
Catalin Constantin Mititiuc 2024-05-30 22:51:56 -07:00
parent 921351bfb6
commit cf5e8a34fe

View File

@ -125,17 +125,17 @@ const resolveImportedSvg = {
build.onLoad({ filter: /\.svg$/ }, async (args) => { build.onLoad({ filter: /\.svg$/ }, async (args) => {
const document = (await JSDOM.fromFile(args.path)).window.document; const document = (await JSDOM.fromFile(args.path)).window.document;
const externalResourceUseEls = Array.from(document.querySelectorAll('use[href*=".svg"')); const externalResourceUseEls = Array.from(document.querySelectorAll('use[href*=".svg"'));
const readFiles = {};
const files = [...new Set([...externalResourceUseEls.map(el => const files = [...new Set([...externalResourceUseEls.map(el =>
el.getAttributeNS(null, 'href').match(/.+\.svg/).at(0) el.getAttributeNS(null, 'href').match(/.+\.svg/).at(0)
)])]; )])];
await Promise.all(files.map((filename) => const readFiles = await Promise.all(
JSDOM files.map(filename => JSDOM.fromFile(path.join(path.dirname(args.path), filename)))
.fromFile(path.join(path.dirname(args.path), filename)) ).then(result => result.reduce((acc, dom, index) => {
.then(dom => readFiles[filename] = dom.window.document) acc[files[index]] = dom.window.document;
)); return acc;
}, {}));
const refs = {}; const refs = {};