Skip to content

Commit 2413907

Browse files
jasnelltargos
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 <[email protected]> PR-URL: #36263 Backport-PR-URL: #36988 Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 36abc18 commit 2413907

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
@@ -128,6 +128,7 @@ const relativeResolveCache = ObjectCreate(null);
128128

129129
let requireDepth = 0;
130130
let statCache = null;
131+
let isPreloading = false;
131132

132133
function stat(filename) {
133134
filename = path.toNamespacedPath(filename);
@@ -219,6 +220,10 @@ ObjectDefineProperty(Module, 'wrapper', {
219220
}
220221
});
221222

223+
ObjectDefineProperty(Module.prototype, 'isPreloading', {
224+
get() { return isPreloading; }
225+
});
226+
222227
let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
223228
debug = fn;
224229
});
@@ -1202,6 +1207,8 @@ Module._preloadModules = function(requests) {
12021207
if (!ArrayIsArray(requests))
12031208
return;
12041209

1210+
isPreloading = true;
1211+
12051212
// Preloaded modules have a dummy parent module which is deemed to exist
12061213
// in the current working directory. This seeds the search path for
12071214
// preloaded modules.
@@ -1210,11 +1217,13 @@ Module._preloadModules = function(requests) {
12101217
parent.paths = Module._nodeModulePaths(process.cwd());
12111218
} catch (e) {
12121219
if (e.code !== 'ENOENT') {
1220+
isPreloading = false;
12131221
throw e;
12141222
}
12151223
}
12161224
for (let n = 0; n < requests.length; n++)
12171225
parent.require(requests[n]);
1226+
isPreloading = false;
12181227
};
12191228

12201229
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)