From 52dd1146b59f17d6cc56ae90c58e5c0660e08806 Mon Sep 17 00:00:00 2001 From: DmitryScaletta Date: Sun, 12 Nov 2023 16:21:52 +0300 Subject: [PATCH] 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; };