Skip to content

Commit fc86e09

Browse files
committed
Implement preload of metacall package for node loader.
1 parent 8d99bcb commit fc86e09

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

source/loaders/node_loader/bootstrap/lib/bootstrap.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ const util = require('util');
77

88
const cherow = require('./node_modules/cherow');
99

10-
// eslint-disable-next-line no-empty-function
1110
function node_loader_trampoline_initialize() {
12-
// Nothing to initialize yet
11+
try {
12+
// Preload MetaCall Monkey Patch
13+
require('metacall');
14+
} catch (e) {
15+
if (e.code === 'MODULE_NOT_FOUND') {
16+
// console.log('NodeJS Warning: MetaCall could not be preloaded');
17+
} else {
18+
console.log(`NodeJS Error (while preloading MetaCall): ${e.message}`);
19+
}
20+
}
1321
}
1422

1523
function node_loader_trampoline_is_callable(value) {

source/ports/node_port/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ mod.prototype.require = function (name) {
160160
/* Probably in the future we can differenciate between them, but it is not trivial */
161161
};
162162

163+
/* Try to load it with NodeJS first */
164+
try {
165+
return node_require.apply(this, [ name ]);
166+
} catch (e) {
167+
if (e.code !== 'MODULE_NOT_FOUND') {
168+
throw e;
169+
}
170+
}
171+
163172
const index = name.lastIndexOf('.');
164173

165174
if (index !== -1) {
@@ -204,8 +213,7 @@ mod.prototype.require = function (name) {
204213
};
205214

206215
/* Debug logs */
207-
if (process.env['NODE_ENV'] === 'debug' && addon !== undefined)
208-
{
216+
if (process.env['NODE_ENV'] === 'debug' && addon !== undefined) {
209217
addon.metacall_logs();
210218
}
211219

0 commit comments

Comments
 (0)