Skip to content

Commit 79846a8

Browse files
feat(core): enable parallel dependencies downloading
1 parent bad0c07 commit 79846a8

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/index.ts

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,20 @@ function hash(...valueList : any[]) : string {
386386
}
387387

388388

389+
/**
390+
* @internal
391+
*/
392+
class Loading {
393+
394+
promise : Promise<Module>;
395+
396+
constructor(promise : Promise<Module>) {
397+
398+
this.promise = promise;
399+
}
400+
}
401+
402+
389403
/**
390404
* Simple cache helper
391405
* preventCache usage: non-fatal error
@@ -481,9 +495,7 @@ function parseDeps(fileAst : t.File) : string[] {
481495
async function loadDeps(filename : string, deps : string[], options : Options) {
482496

483497
const { pathHandlers: { resolve } } = options;
484-
485-
for ( const dep of deps )
486-
await loadModule(resolve(filename, dep), options);
498+
await Promise.all(deps.map(dep => loadModule(resolve(filename, dep), options)))
487499
}
488500

489501

@@ -834,28 +846,46 @@ export async function loadModule(path : string, options_ : Options = throwNotDef
834846
...options_
835847
};
836848

837-
if ( path in moduleCache )
838-
return moduleCache[path];
839-
840-
if ( loadModule ) {
849+
if ( path in moduleCache ) {
841850

842-
const module = await loadModule(path, options);
843-
if ( module !== undefined )
844-
return moduleCache[path] = module;
851+
if ( moduleCache[path] instanceof Loading )
852+
return await moduleCache[path].promise;
853+
else
854+
return moduleCache[path];
845855
}
846856

847857

848-
const moduleHandlers = { ...defaultModuleHandlers, ...additionalModuleHandlers };
858+
moduleCache[path] = new Loading(
859+
860+
(async () => {
861+
862+
if ( loadModule ) {
863+
864+
const module = await loadModule(path, options);
865+
if ( module !== undefined )
866+
return moduleCache[path] = module;
867+
}
868+
869+
const moduleHandlers = { ...defaultModuleHandlers, ...additionalModuleHandlers };
870+
871+
const res = await getFile(path);
872+
873+
const file = typeof res === 'object' ? res : { content: res, extname: pathHandlers.extname(path) };
874+
875+
if ( !(file.extname in moduleHandlers) )
876+
throw new TypeError(`Unable to handle ${ file.extname } files (${ path }), see additionalModuleHandlers`);
849877

850-
const res = await getFile(path);
878+
if ( typeof file.content !== 'string' )
879+
throw new TypeError(`Invalid module content (${path}): ${ file.content }`);
851880

852-
const file = typeof res === 'object' ? res : { content: res, extname: pathHandlers.extname(path) };
853881

854-
if ( !(file.extname in moduleHandlers) )
855-
throw new TypeError(`Unable to handle ${ file.extname } files (${ path }), see additionalModuleHandlers`);
882+
const module = await moduleHandlers[file.extname](file.content, path, options);
883+
884+
return moduleCache[path] = module;
885+
886+
})()
856887

857-
if ( typeof file.content !== 'string' )
858-
throw new TypeError(`Invalid module content (${path}): ${ file.content }`);
888+
);
859889

860-
return moduleCache[path] = await moduleHandlers[file.extname](file.content, path, options);
890+
return await moduleCache[path].promise;
861891
}

0 commit comments

Comments
 (0)