From 52dd1146b59f17d6cc56ae90c58e5c0660e08806 Mon Sep 17 00:00:00 2001 From: DmitryScaletta Date: Sun, 12 Nov 2023 16:21:52 +0300 Subject: [PATCH 1/4] Don't load submodules with not installed optional dependencies See: https://github.com/metarhia/impress/issues/1937 --- lib/deps.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/deps.js b/lib/deps.js index 81fa996a..1cccbef4 100644 --- a/lib/deps.js +++ b/lib/deps.js @@ -54,8 +54,16 @@ const loadModule = (name) => { const subKeys = Object.keys(pkg.exports).map((key) => key.substring(2)); const subNames = subKeys.filter(validSubmodules); for (const subName of subNames) { - const sub = appRequire(name + '/' + subName); - lib[subName] = sub; + try { + const sub = appRequire(name + '/' + subName); + lib[subName] = sub; + } catch (e) { + if (e.message.startsWith("Cannot find module '")) { + const moduleName = e.message.substring(20, e.message.indexOf("'\n")); + const optional = pkg.peerDependenciesMeta?.[moduleName].optional; + if (optional) continue; else throw e; + } + } } return lib; }; From 095c9288f5fdc2911eaa082bfaf3c99d590e7062 Mon Sep 17 00:00:00 2001 From: DmitryScaletta Date: Sun, 12 Nov 2023 16:26:31 +0300 Subject: [PATCH 2/4] Skip submodules if they have already been loaded as a part of main module See: https://github.com/metarhia/impress/issues/1938 --- lib/deps.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/deps.js b/lib/deps.js index 1cccbef4..b343ebd4 100644 --- a/lib/deps.js +++ b/lib/deps.js @@ -56,12 +56,14 @@ const loadModule = (name) => { for (const subName of subNames) { try { const sub = appRequire(name + '/' + subName); + if (lib[subName] && lib[subName] === sub[subName]) continue; lib[subName] = sub; } catch (e) { if (e.message.startsWith("Cannot find module '")) { const moduleName = e.message.substring(20, e.message.indexOf("'\n")); const optional = pkg.peerDependenciesMeta?.[moduleName].optional; - if (optional) continue; else throw e; + if (optional) continue; + else throw e; } } } From 3377c945d16643278257d7f2bd5f6e905676c4dc Mon Sep 17 00:00:00 2001 From: DmitryScaletta Date: Sun, 12 Nov 2023 17:04:41 +0300 Subject: [PATCH 3/4] Fix checking whether a module is optional --- lib/deps.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/deps.js b/lib/deps.js index b343ebd4..94865097 100644 --- a/lib/deps.js +++ b/lib/deps.js @@ -61,7 +61,7 @@ const loadModule = (name) => { } catch (e) { if (e.message.startsWith("Cannot find module '")) { const moduleName = e.message.substring(20, e.message.indexOf("'\n")); - const optional = pkg.peerDependenciesMeta?.[moduleName].optional; + const optional = pkg.peerDependenciesMeta?.[moduleName]?.optional; if (optional) continue; else throw e; } From 056f2b9d8be9fc62a452166e73b882647b4a8b30 Mon Sep 17 00:00:00 2001 From: DmitryScaletta Date: Wed, 15 Nov 2023 17:17:23 +0300 Subject: [PATCH 4/4] Check 'module not found' error by error code --- lib/deps.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/deps.js b/lib/deps.js index 94865097..7dfc1203 100644 --- a/lib/deps.js +++ b/lib/deps.js @@ -58,13 +58,13 @@ const loadModule = (name) => { const sub = appRequire(name + '/' + subName); if (lib[subName] && lib[subName] === sub[subName]) continue; lib[subName] = sub; - } catch (e) { - if (e.message.startsWith("Cannot find module '")) { - const moduleName = e.message.substring(20, e.message.indexOf("'\n")); - const optional = pkg.peerDependenciesMeta?.[moduleName]?.optional; + } catch (err) { + if (err.code === 'MODULE_NOT_FOUND' && pkg.peerDependenciesMeta) { + const moduleName = metautil.between(err.message, "'", "'"); + const optional = pkg.peerDependenciesMeta[moduleName]?.optional; if (optional) continue; - else throw e; } + throw err; } } return lib;