Skip to content

Commit d4de7c7

Browse files
jasnelldanielleadams
authored andcommitted
module: add isPreloading indicator
Adds a `module.isPreloading` property that is `true` only during the preload (`-r`) phase of Node.js bootstrap. This provides modules an easy, non-hacky way of knowing if they are being loaded during preload. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #36263 Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 278862a commit d4de7c7

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

doc/api/module.md

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ const requireUtil = createRequireFromPath('../src/utils/');
7676
requireUtil('./some-tool');
7777
```
7878
79+
### `module.isPreloading`
80+
<!-- YAML
81+
added: REPLACEME
82+
-->
83+
84+
* Type: {boolean} `true` if the module is running during the Node.js preload
85+
phase.
86+
7987
### `module.syncBuiltinESMExports()`
8088
<!-- YAML
8189
added: v12.12.0

lib/internal/modules/cjs/loader.js

+9
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const relativeResolveCache = ObjectCreate(null);
139139

140140
let requireDepth = 0;
141141
let statCache = null;
142+
let isPreloading = false;
142143

143144
function stat(filename) {
144145
filename = path.toNamespacedPath(filename);
@@ -231,6 +232,10 @@ ObjectDefineProperty(Module, 'wrapper', {
231232
}
232233
});
233234

235+
ObjectDefineProperty(Module.prototype, 'isPreloading', {
236+
get() { return isPreloading; }
237+
});
238+
234239
function getModuleParent() {
235240
return moduleParentCache.get(this);
236241
}
@@ -1243,6 +1248,8 @@ Module._preloadModules = function(requests) {
12431248
if (!ArrayIsArray(requests))
12441249
return;
12451250

1251+
isPreloading = true;
1252+
12461253
// Preloaded modules have a dummy parent module which is deemed to exist
12471254
// in the current working directory. This seeds the search path for
12481255
// preloaded modules.
@@ -1251,11 +1258,13 @@ Module._preloadModules = function(requests) {
12511258
parent.paths = Module._nodeModulePaths(process.cwd());
12521259
} catch (e) {
12531260
if (e.code !== 'ENOENT') {
1261+
isPreloading = false;
12541262
throw e;
12551263
}
12561264
}
12571265
for (let n = 0; n < requests.length; n++)
12581266
parent.require(requests[n]);
1267+
isPreloading = false;
12591268
};
12601269

12611270
Module.syncBuiltinESMExports = function syncBuiltinESMExports() {

test/fixtures/ispreloading.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const assert = require('assert');
2+
assert(module.isPreloading);

test/parallel/test-preload.js

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ const fixtureE = fixtures.path('intrinsic-mutation.js');
2727
const fixtureF = fixtures.path('print-intrinsic-mutation-name.js');
2828
const fixtureG = fixtures.path('worker-from-argv.js');
2929
const fixtureThrows = fixtures.path('throws_error4.js');
30+
const fixtureIsPreloading = fixtures.path('ispreloading.js');
31+
32+
// Assert that module.isPreloading is false here
33+
assert(!module.isPreloading);
34+
35+
// Test that module.isPreloading is set in preloaded module
36+
// Test preloading a single module works
37+
childProcess.exec(
38+
`"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`,
39+
function(err, stdout, stderr) {
40+
assert.ifError(err);
41+
assert.strictEqual(stdout, 'B\n');
42+
});
3043

3144
// Test preloading a single module works
3245
childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`,

0 commit comments

Comments
 (0)