| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 36abc18 commit 2413907
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,6 +76,14 @@ const requireUtil = createRequireFromPath('../src/utils/'); | |||
| 76 | 76 | requireUtil('./some-tool'); | |
| 77 | 77 | ``` | |
| 78 | 78 | ||
| 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 | + | ||
| 79 | 87 | ### `module.syncBuiltinESMExports()` | |
| 80 | 88 | <!-- YAML | |
| 81 | 89 | added: v12.12.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -128,6 +128,7 @@ const relativeResolveCache = ObjectCreate(null); | |||
| 128 | 128 | ||
| 129 | 129 | let requireDepth = 0; | |
| 130 | 130 | let statCache = null; | |
| 131 | + let isPreloading = false; | ||
| 131 | 132 | ||
| 132 | 133 | function stat(filename) { | |
| 133 | 134 | filename = path.toNamespacedPath(filename); | |
@@ -219,6 +220,10 @@ ObjectDefineProperty(Module, 'wrapper', { | |||
| 219 | 220 | } | |
| 220 | 221 | }); | |
| 221 | 222 | ||
| 223 | + ObjectDefineProperty(Module.prototype, 'isPreloading', { | ||
| 224 | + get() { return isPreloading; } | ||
| 225 | + }); | ||
| 226 | + | ||
| 222 | 227 | let debug = require('internal/util/debuglog').debuglog('module', (fn) => { | |
| 223 | 228 | debug = fn; | |
| 224 | 229 | }); | |
@@ -1202,6 +1207,8 @@ Module._preloadModules = function(requests) { | |||
| 1202 | 1207 | if (!ArrayIsArray(requests)) | |
| 1203 | 1208 | return; | |
| 1204 | 1209 | ||
| 1210 | + isPreloading = true; | ||
| 1211 | + | ||
| 1205 | 1212 | // Preloaded modules have a dummy parent module which is deemed to exist | |
| 1206 | 1213 | // in the current working directory. This seeds the search path for | |
| 1207 | 1214 | // preloaded modules. | |
@@ -1210,11 +1217,13 @@ Module._preloadModules = function(requests) { | |||
| 1210 | 1217 | parent.paths = Module._nodeModulePaths(process.cwd()); | |
| 1211 | 1218 | } catch (e) { | |
| 1212 | 1219 | if (e.code !== 'ENOENT') { | |
| 1220 | + isPreloading = false; | ||
| 1213 | 1221 | throw e; | |
| 1214 | 1222 | } | |
| 1215 | 1223 | } | |
| 1216 | 1224 | for (let n = 0; n < requests.length; n++) | |
| 1217 | 1225 | parent.require(requests[n]); | |
| 1226 | + isPreloading = false; | ||
| 1218 | 1227 | }; | |
| 1219 | 1228 | ||
| 1220 | 1229 | Module.syncBuiltinESMExports = function syncBuiltinESMExports() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + const assert = require('assert'); | ||
| 2 | + assert(module.isPreloading); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,19 @@ const fixtureE = fixtures.path('intrinsic-mutation.js'); | |||
| 27 | 27 | const fixtureF = fixtures.path('print-intrinsic-mutation-name.js'); | |
| 28 | 28 | const fixtureG = fixtures.path('worker-from-argv.js'); | |
| 29 | 29 | 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 | + }); | ||
| 30 | 43 | ||
| 31 | 44 | // Test preloading a single module works | |
| 32 | 45 | childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments