Skip to content

Commit 80cadf2

Browse files
committed
esm: avoid throw when module specifier is not url
This particular exception is responsible for a lot of overhead when debugging with large esm codebases with VS Code and break on caught exceptions is enabled. VS Code silently suppresses this exception, but the mechanism it uses to do so is a bit slow so avoiding this common exception can speed up loading of esm code. In my scenario this saved over have of the startup run time (over 20 seconds). This should also make debugging without suppression of this exception more pleasant in other tools such as the Chrome dev tools when attached to NodeJs processes.
1 parent bd42673 commit 80cadf2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,12 +846,13 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
846846
} else if (protocol === 'file:' && specifier[0] === '#') {
847847
resolved = packageImportsResolve(specifier, base, conditions);
848848
} else {
849-
try {
850-
resolved = new URL(specifier);
851-
} catch (cause) {
849+
const url = URL.parse(specifier);
850+
if (url !== null) {
851+
resolved = url;
852+
} else {
852853
if (isData && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
853854
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
854-
setOwnProperty(error, 'cause', cause);
855+
setOwnProperty(error, 'cause', new TypeError("Invalid URL"));
855856
throw error;
856857
}
857858
resolved = packageResolve(specifier, base, conditions);

0 commit comments

Comments
 (0)