| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 709ada5 commit 8ff2501
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 | |
|---|---|---|---|
@@ -139,6 +139,7 @@ const relativeResolveCache = ObjectCreate(null); | |||
| 139 | 139 | ||
| 140 | 140 | let requireDepth = 0; | |
| 141 | 141 | let statCache = null; | |
| 142 | + let isPreloading = false; | ||
| 142 | 143 | ||
| 143 | 144 | function stat(filename) { | |
| 144 | 145 | filename = path.toNamespacedPath(filename); | |
@@ -231,6 +232,10 @@ ObjectDefineProperty(Module, 'wrapper', { | |||
| 231 | 232 | } | |
| 232 | 233 | }); | |
| 233 | 234 | ||
| 235 | + ObjectDefineProperty(Module.prototype, 'isPreloading', { | ||
| 236 | + get() { return isPreloading; } | ||
| 237 | + }); | ||
| 238 | + | ||
| 234 | 239 | function getModuleParent() { | |
| 235 | 240 | return moduleParentCache.get(this); | |
| 236 | 241 | } | |
@@ -1243,6 +1248,8 @@ Module._preloadModules = function(requests) { | |||
| 1243 | 1248 | if (!ArrayIsArray(requests)) | |
| 1244 | 1249 | return; | |
| 1245 | 1250 | ||
| 1251 | + isPreloading = true; | ||
| 1252 | + | ||
| 1246 | 1253 | // Preloaded modules have a dummy parent module which is deemed to exist | |
| 1247 | 1254 | // in the current working directory. This seeds the search path for | |
| 1248 | 1255 | // preloaded modules. | |
@@ -1251,11 +1258,13 @@ Module._preloadModules = function(requests) { | |||
| 1251 | 1258 | parent.paths = Module._nodeModulePaths(process.cwd()); | |
| 1252 | 1259 | } catch (e) { | |
| 1253 | 1260 | if (e.code !== 'ENOENT') { | |
| 1261 | + isPreloading = false; | ||
| 1254 | 1262 | throw e; | |
| 1255 | 1263 | } | |
| 1256 | 1264 | } | |
| 1257 | 1265 | for (let n = 0; n < requests.length; n++) | |
| 1258 | 1266 | parent.require(requests[n]); | |
| 1267 | + isPreloading = false; | ||
| 1259 | 1268 | }; | |
| 1260 | 1269 | ||
| 1261 | 1270 | 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