diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index cae3200e677926..02bb99bf37d573 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -52,6 +52,7 @@ const { ObjectKeys, ObjectPrototypeHasOwnProperty, ReflectGet, + ReflectGetOwnPropertyDescriptor, SafeMap, SafeSet, String, @@ -216,12 +217,19 @@ class NativeModule { } // Used by user-land module loaders to compile and load builtins. - compileForPublicLoader() { + compileForPublicLoader(isPreloading) { if (!this.canBeRequiredByUsers) { // No code because this is an assertion against bugs // eslint-disable-next-line no-restricted-syntax throw new Error(`Should not compile ${this.id} for public use`); } + if (isPreloading) { + ObjectDefineProperty( + this, 'isPreloading', + ReflectGetOwnPropertyDescriptor( + nativeModuleRequire('internal/modules/cjs/loader').Module.prototype, + 'isPreloading')); + } this.compileForInternalLoader(); if (!this.exportKeys) { // When using --expose-internals, we do not want to reflect the named diff --git a/lib/internal/modules/cjs/helpers.js b/lib/internal/modules/cjs/helpers.js index 163c854ac26d48..56c52dbe4349d7 100644 --- a/lib/internal/modules/cjs/helpers.js +++ b/lib/internal/modules/cjs/helpers.js @@ -32,11 +32,11 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => { // TODO: Use this set when resolving pkg#exports conditions in loader.js. const cjsConditions = new SafeSet(['require', 'node', ...userConditions]); -function loadNativeModule(filename, request) { +function loadNativeModule(filename, request, isPreloading) { const mod = NativeModule.map.get(filename); if (mod) { debug('load native module %s', request); - mod.compileForPublicLoader(); + mod.compileForPublicLoader(isPreloading); return mod; } } diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index b129e94d9890c0..06e90d43bd51cd 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -781,7 +781,7 @@ Module._load = function(request, parent, isMain) { } } - const mod = loadNativeModule(filename, request); + const mod = loadNativeModule(filename, request, isPreloading); if (mod && mod.canBeRequiredByUsers) return mod.exports; // Don't call updateChildren(), Module constructor already does. diff --git a/lib/internal/test/binding.js b/lib/internal/test/binding.js index 882ea90093d039..063b9b5c900aad 100644 --- a/lib/internal/test/binding.js +++ b/lib/internal/test/binding.js @@ -4,4 +4,9 @@ process.emitWarning( 'These APIs are for internal testing only. Do not use them.', 'internal/test/binding'); -module.exports = { internalBinding }; +if (module.isPreloading) { + globalThis.internalBinding = internalBinding; + globalThis.primordials = primordials; +} + +module.exports = { internalBinding, primordials };