Skip to content

Commit

Permalink
Add a more complete require schema for node loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Dec 15, 2020
1 parent bc995c0 commit f25c346
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion source/loaders/node_loader/bootstrap/lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ function node_loader_trampoline_execution_path() {
// TODO
}

function node_loader_trampoline_load_from_file_require(p) {
/* First try to load the absolute path */
try {
return require(p);
} catch (e) {}

/* Then try to load without the path */
const basename = path.basename(p);

try {
return require(basename);
} catch (e) {}

/* Finally try to load without the path and extension */
const { name } = path.parse(basename);

return require(name);
}

function node_loader_trampoline_load_from_file(paths) {
if (!Array.isArray(paths)) {
throw new Error('Load from file paths must be an array, not ' + typeof paths);
Expand All @@ -48,7 +67,7 @@ function node_loader_trampoline_load_from_file(paths) {

for (let i = 0; i < paths.length; ++i) {
const p = paths[i];
const m = require(path.resolve(__dirname, p));
const m = node_loader_trampoline_load_from_file_require(path.resolve(__dirname, p));

handle[p] = node_loader_trampoline_module(m);
}
Expand Down

0 comments on commit f25c346

Please sign in to comment.