| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 26ec996 commit 4be5047
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,6 +70,7 @@ const { | |||
| 70 | 70 | module_export_private_symbol, | |
| 71 | 71 | module_parent_private_symbol, | |
| 72 | 72 | }, | |
| 73 | + isInsideNodeModules, | ||
| 73 | 74 | } = internalBinding('util'); | |
| 74 | 75 | ||
| 75 | 76 | const { kEvaluated, createRequiredModuleFacade } = internalBinding('module_wrap'); | |
@@ -131,6 +132,7 @@ const { | |||
| 131 | 132 | setOwnProperty, | |
| 132 | 133 | getLazy, | |
| 133 | 134 | isWindows, | |
| 135 | + isUnderNodeModules, | ||
| 134 | 136 | } = require('internal/util'); | |
| 135 | 137 | const { | |
| 136 | 138 | makeContextifyScript, | |
@@ -1328,6 +1330,7 @@ Module.prototype.require = function(id) { | |||
| 1328 | 1330 | } | |
| 1329 | 1331 | }; | |
| 1330 | 1332 | ||
| 1333 | + let emittedRequireModuleWarning = false; | ||
| 1331 | 1334 | /** | |
| 1332 | 1335 | * Resolve and evaluate it synchronously as ESM if it's ESM. | |
| 1333 | 1336 | * @param {Module} mod CJS module instance | |
@@ -1347,29 +1350,46 @@ function loadESMFromCJS(mod, filename, format, source) { | |||
| 1347 | 1350 | setOwnProperty(process, 'mainModule', undefined); | |
| 1348 | 1351 | } else { | |
| 1349 | 1352 | const parent = mod[kModuleParent]; | |
| 1350 | - let messagePrefix; | ||
| 1351 | - if (parent) { | ||
| 1352 | - // In the case of the module calling `require()`, it's more useful to know its absolute path. | ||
| 1353 | - let from = parent.filename || parent.id; | ||
| 1354 | - // In the case of the module being require()d, it's more useful to know the id passed into require(). | ||
| 1355 | - const to = mod.id || mod.filename; | ||
| 1356 | - if (from === 'internal/preload') { | ||
| 1357 | - from = '--require'; | ||
| 1358 | - } else if (from === '<repl>') { | ||
| 1359 | - from = 'The REPL'; | ||
| 1360 | - } else if (from === '.') { | ||
| 1361 | - from = 'The entry point'; | ||
| 1362 | - } else { | ||
| 1363 | - from &&= `CommonJS module ${from}`; | ||
| 1353 | + | ||
| 1354 | + if (!emittedRequireModuleWarning) { | ||
| 1355 | + let shouldEmitWarning = false; | ||
| 1356 | + // Check if the require() comes from node_modules. | ||
| 1357 | + if (parent) { | ||
| 1358 | + shouldEmitWarning = !isUnderNodeModules(parent.filename); | ||
| 1359 | + } else if (mod[kIsCachedByESMLoader]) { | ||
| 1360 | + // It comes from the require() built for `import cjs` and doesn't have a parent recorded | ||
| 1361 | + // in the CJS module instance. Inspect the stack trace to see if the require() | ||
| 1362 | + // comes from node_modules and reduce the noise. If there are more than 100 frames, | ||
| 1363 | + // just give up and assume it is under node_modules. | ||
| 1364 | + shouldEmitWarning = !isInsideNodeModules(100, true); | ||
| 1364 | 1365 | } | |
| 1365 | - if (from && to) { | ||
| 1366 | - messagePrefix = `${from} is loading ES Module ${to} using require().\n`; | ||
| 1366 | + if (shouldEmitWarning) { | ||
| 1367 | + let messagePrefix; | ||
| 1368 | + if (parent) { | ||
| 1369 | + // In the case of the module calling `require()`, it's more useful to know its absolute path. | ||
| 1370 | + let from = parent.filename || parent.id; | ||
| 1371 | + // In the case of the module being require()d, it's more useful to know the id passed into require(). | ||
| 1372 | + const to = mod.id || mod.filename; | ||
| 1373 | + if (from === 'internal/preload') { | ||
| 1374 | + from = '--require'; | ||
| 1375 | + } else if (from === '<repl>') { | ||
| 1376 | + from = 'The REPL'; | ||
| 1377 | + } else if (from === '.') { | ||
| 1378 | + from = 'The entry point'; | ||
| 1379 | + } else { | ||
| 1380 | + from &&= `CommonJS module ${from}`; | ||
| 1381 | + } | ||
| 1382 | + if (from && to) { | ||
| 1383 | + messagePrefix = `${from} is loading ES Module ${to} using require().\n`; | ||
| 1384 | + } | ||
| 1385 | + } | ||
| 1386 | + emitExperimentalWarning('Support for loading ES Module in require()', | ||
| 1387 | + messagePrefix, | ||
| 1388 | + undefined, | ||
| 1389 | + parent?.require); | ||
| 1390 | + emittedRequireModuleWarning = true; | ||
| 1367 | 1391 | } | |
| 1368 | 1392 | } | |
| 1369 | - emitExperimentalWarning('Support for loading ES Module in require()', | ||
| 1370 | - messagePrefix, | ||
| 1371 | - undefined, | ||
| 1372 | - parent?.require); | ||
| 1373 | 1393 | const { | |
| 1374 | 1394 | wrap, | |
| 1375 | 1395 | namespace, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,59 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // This checks the experimental warning for require(esm) is disabled when the | ||
| 4 | + // require() comes from node_modules. | ||
| 5 | + require('../common'); | ||
| 6 | + const { spawnSyncAndAssert } = require('../common/child_process'); | ||
| 7 | + const fixtures = require('../common/fixtures'); | ||
| 8 | + | ||
| 9 | + const warningRE = /Support for loading ES Module in require\(\)/; | ||
| 10 | + | ||
| 11 | + // The fixtures are placed in a directory that includes "node_modules" in its name | ||
| 12 | + // to check false negatives. | ||
| 13 | + | ||
| 14 | + // require() in non-node_modules -> esm in node_modules should warn. | ||
| 15 | + spawnSyncAndAssert( | ||
| 16 | + process.execPath, | ||
| 17 | + [fixtures.path('es-modules', 'test_node_modules', 'require-esm.js')], | ||
| 18 | + { | ||
| 19 | + trim: true, | ||
| 20 | + stderr: warningRE, | ||
| 21 | + stdout: 'world', | ||
| 22 | + } | ||
| 23 | + ); | ||
| 24 | + | ||
| 25 | + // require() in non-node_modules -> require() in node_modules -> esm in node_modules | ||
| 26 | + // should not warn. | ||
| 27 | + spawnSyncAndAssert( | ||
| 28 | + process.execPath, | ||
| 29 | + [fixtures.path('es-modules', 'test_node_modules', 'require-require-esm.js')], | ||
| 30 | + { | ||
| 31 | + trim: true, | ||
| 32 | + stderr: '', | ||
| 33 | + stdout: 'world', | ||
| 34 | + } | ||
| 35 | + ); | ||
| 36 | + | ||
| 37 | + // Import in non-node_modules -> require() in node_modules -> esm in node_modules | ||
| 38 | + // should not warn. | ||
| 39 | + spawnSyncAndAssert( | ||
| 40 | + process.execPath, | ||
| 41 | + [fixtures.path('es-modules', 'test_node_modules', 'import-require-esm.mjs')], | ||
| 42 | + { | ||
| 43 | + trim: true, | ||
| 44 | + stderr: '', | ||
| 45 | + stdout: 'world', | ||
| 46 | + } | ||
| 47 | + ); | ||
| 48 | + | ||
| 49 | + // Import in non-node_modules -> import in node_modules -> | ||
| 50 | + // require() in node_modules -> esm in node_modules should not warn. | ||
| 51 | + spawnSyncAndAssert( | ||
| 52 | + process.execPath, | ||
| 53 | + [fixtures.path('es-modules', 'test_node_modules', 'import-import-require-esm.mjs')], | ||
| 54 | + { | ||
| 55 | + trim: true, | ||
| 56 | + stderr: '', | ||
| 57 | + stdout: 'world', | ||
| 58 | + } | ||
| 59 | + ); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments