Skip to content

Commit

Permalink
module: add isPreloading indicator
Browse files Browse the repository at this point in the history
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
Reviewed-By: Gerhard Stöbich <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
jasnell authored and danielleadams committed Dec 7, 2020
1 parent 278862a commit d4de7c7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const requireUtil = createRequireFromPath('../src/utils/');
requireUtil('./some-tool');
```
### `module.isPreloading`
<!-- YAML
added: REPLACEME
-->
* Type: {boolean} `true` if the module is running during the Node.js preload
phase.
### `module.syncBuiltinESMExports()`
<!-- YAML
added: v12.12.0
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const relativeResolveCache = ObjectCreate(null);

let requireDepth = 0;
let statCache = null;
let isPreloading = false;

function stat(filename) {
filename = path.toNamespacedPath(filename);
Expand Down Expand Up @@ -231,6 +232,10 @@ ObjectDefineProperty(Module, 'wrapper', {
}
});

ObjectDefineProperty(Module.prototype, 'isPreloading', {
get() { return isPreloading; }
});

function getModuleParent() {
return moduleParentCache.get(this);
}
Expand Down Expand Up @@ -1243,6 +1248,8 @@ Module._preloadModules = function(requests) {
if (!ArrayIsArray(requests))
return;

isPreloading = true;

// Preloaded modules have a dummy parent module which is deemed to exist
// in the current working directory. This seeds the search path for
// preloaded modules.
Expand All @@ -1251,11 +1258,13 @@ Module._preloadModules = function(requests) {
parent.paths = Module._nodeModulePaths(process.cwd());
} catch (e) {
if (e.code !== 'ENOENT') {
isPreloading = false;
throw e;
}
}
for (let n = 0; n < requests.length; n++)
parent.require(requests[n]);
isPreloading = false;
};

Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ispreloading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const assert = require('assert');
assert(module.isPreloading);
13 changes: 13 additions & 0 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ const fixtureE = fixtures.path('intrinsic-mutation.js');
const fixtureF = fixtures.path('print-intrinsic-mutation-name.js');
const fixtureG = fixtures.path('worker-from-argv.js');
const fixtureThrows = fixtures.path('throws_error4.js');
const fixtureIsPreloading = fixtures.path('ispreloading.js');

// Assert that module.isPreloading is false here
assert(!module.isPreloading);

// Test that module.isPreloading is set in preloaded module
// Test preloading a single module works
childProcess.exec(
`"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`,
function(err, stdout, stderr) {
assert.ifError(err);
assert.strictEqual(stdout, 'B\n');
});

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

0 comments on commit d4de7c7

Please sign in to comment.