Skip to content

Commit 06bf489

Browse files
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. PR-URL: #61000 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 20bf328 commit 06bf489

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
ERR_INVALID_MODULE_SPECIFIER,
4343
ERR_INVALID_PACKAGE_CONFIG,
4444
ERR_INVALID_PACKAGE_TARGET,
45+
ERR_INVALID_URL,
4546
ERR_MODULE_NOT_FOUND,
4647
ERR_PACKAGE_IMPORT_NOT_DEFINED,
4748
ERR_PACKAGE_PATH_NOT_EXPORTED,
@@ -846,12 +847,13 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
846847
} else if (protocol === 'file:' && specifier[0] === '#') {
847848
resolved = packageImportsResolve(specifier, base, conditions);
848849
} else {
849-
try {
850-
resolved = new URL(specifier);
851-
} catch (cause) {
850+
const url = URLParse(specifier);
851+
if (url !== null) {
852+
resolved = url;
853+
} else {
852854
if (isData && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
853855
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
854-
setOwnProperty(error, 'cause', cause);
856+
setOwnProperty(error, 'cause', new ERR_INVALID_URL(specifier));
855857
throw error;
856858
}
857859
resolved = packageResolve(specifier, base, conditions);

0 commit comments

Comments
 (0)