Skip to content

Commit

Permalink
Support submodules in autoloader
Browse files Browse the repository at this point in the history
PR-URL: #1888
  • Loading branch information
tshemsedinov committed Aug 9, 2023
1 parent 675bd47 commit c27b318
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,31 @@ const loadPlugins = (lib) => {
}
};

const validSubmodules = (key) =>
key !== '' && !key.includes('*') && !key.includes('.');

const loadModule = (name) => {
const lib = appRequire(name);
const pkg = require(`${CWD}/node_modules/${name}/package.json`);
if (!pkg.exports) return lib;
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;
}
return lib;
};

for (const name of dependencies) {
if (name === 'impress') continue;
let lib = null;
try {
lib = internals.includes(name) ? require(`node:${name}`) : appRequire(name);
if (internals.includes(name)) {
lib = require(`node:${name}`);
} else {
lib = loadModule(name);
}
} catch {
if (npmpkg.includes(name) || !optional.includes(name)) {
notLoaded.add(name);
Expand Down

0 comments on commit c27b318

Please sign in to comment.