| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9bca620 commit ac02a0b
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -598,14 +598,36 @@ filename scales linearly with the number of registered extensions. | |||
| 598 | 598 | In other words, adding extensions slows down the module loader and | |
| 599 | 599 | should be discouraged. | |
| 600 | 600 | ||
| 601 | - #### require.resolve() | ||
| 601 | + #### require.resolve(request[, options]) | ||
| 602 | 602 | <!-- YAML | |
| 603 | 603 | added: v0.3.0 | |
| 604 | + changes: | ||
| 605 | + - version: REPLACEME | ||
| 606 | + pr-url: https://github.com/nodejs/node/pull/16397 | ||
| 607 | + description: The `paths` option is now supported. | ||
| 604 | 608 | --> | |
| 605 | 609 | ||
| 610 | + * `request` {string} The module path to resolve. | ||
| 611 | + * `options` {Object} | ||
| 612 | + * `paths` {Array} Paths to resolve module location from. If present, these | ||
| 613 | + paths are used instead of the default resolution paths. Note that each of | ||
| 614 | + these paths is used as a starting point for the module resolution algorithm, | ||
| 615 | + meaning that the `node_modules` hierarchy is checked from this location. | ||
| 616 | + * Returns: {string} | ||
| 617 | + | ||
| 606 | 618 | Use the internal `require()` machinery to look up the location of a module, | |
| 607 | 619 | but rather than loading the module, just return the resolved filename. | |
| 608 | 620 | ||
| 621 | + #### require.resolve.paths(request) | ||
| 622 | + <!-- YAML | ||
| 623 | + added: REPLACEME | ||
| 624 | + --> | ||
| 625 | + | ||
| 626 | + * `request` {string} The module path whose lookup paths are being retrieved. | ||
| 627 | + * Returns: {Array} | ||
| 628 | + | ||
| 629 | + Returns an array containing the paths searched during resolution of `request`. | ||
| 630 | + | ||
| 609 | 631 | ## The `module` Object | |
| 610 | 632 | <!-- YAML | |
| 611 | 633 | added: v0.1.16 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1850,7 +1850,6 @@ cases: | |||
| 1850 | 1850 | [`process.kill()`]: #process_process_kill_pid_signal | |
| 1851 | 1851 | [`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | |
| 1852 | 1852 | [`require.main`]: modules.html#modules_accessing_the_main_module | |
| 1853 | - [`require.resolve()`]: modules.html#modules_require_resolve | ||
| 1854 | 1853 | [`setTimeout(fn, 0)`]: timers.html#timers_settimeout_callback_delay_args | |
| 1855 | 1854 | [Child Process]: child_process.html | |
| 1856 | 1855 | [Cluster]: cluster.html | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,12 +14,18 @@ function makeRequireFunction(mod) { | |||
| 14 | 14 | } | |
| 15 | 15 | } | |
| 16 | 16 | ||
| 17 | - function resolve(request) { | ||
| 18 | - return Module._resolveFilename(request, mod); | ||
| 17 | + function resolve(request, options) { | ||
| 18 | + return Module._resolveFilename(request, mod, false, options); | ||
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | 21 | require.resolve = resolve; | |
| 22 | 22 | ||
| 23 | + function paths(request) { | ||
| 24 | + return Module._resolveLookupPaths(request, mod, true); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + resolve.paths = paths; | ||
| 28 | + | ||
| 23 | 29 | require.main = process.mainModule; | |
| 24 | 30 | ||
| 25 | 31 | // Enable support to add extra extension types. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -500,12 +500,32 @@ function tryModuleLoad(module, filename) { | |||
| 500 | 500 | } | |
| 501 | 501 | } | |
| 502 | 502 | ||
| 503 | - Module._resolveFilename = function(request, parent, isMain) { | ||
| 503 | + Module._resolveFilename = function(request, parent, isMain, options) { | ||
| 504 | 504 | if (NativeModule.nonInternalExists(request)) { | |
| 505 | 505 | return request; | |
| 506 | 506 | } | |
| 507 | 507 | ||
| 508 | - var paths = Module._resolveLookupPaths(request, parent, true); | ||
| 508 | + var paths; | ||
| 509 | + | ||
| 510 | + if (typeof options === 'object' && options !== null && | ||
| 511 | + Array.isArray(options.paths)) { | ||
| 512 | + paths = []; | ||
| 513 | + | ||
| 514 | + for (var i = 0; i < options.paths.length; i++) { | ||
| 515 | + const path = options.paths[i]; | ||
| 516 | + const lookupPaths = Module._resolveLookupPaths(path, parent, true); | ||
| 517 | + | ||
| 518 | + if (!paths.includes(path)) | ||
| 519 | + paths.push(path); | ||
| 520 | + | ||
| 521 | + for (var j = 0; j < lookupPaths.length; j++) { | ||
| 522 | + if (!paths.includes(lookupPaths[j])) | ||
| 523 | + paths.push(lookupPaths[j]); | ||
| 524 | + } | ||
| 525 | + } | ||
| 526 | + } else { | ||
| 527 | + paths = Module._resolveLookupPaths(request, parent, true); | ||
| 528 | + } | ||
| 509 | 529 | ||
| 510 | 530 | // look up the filename first, since that's the cache key. | |
| 511 | 531 | var filename = Module._findPath(request, paths, isMain); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,55 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const path = require('path'); | ||
| 5 | + const nodeModules = path.join(__dirname, 'node_modules'); | ||
| 6 | + const nestedNodeModules = path.join(__dirname, 'node_modules', 'node_modules'); | ||
| 7 | + const nestedIndex = path.join(__dirname, 'nested-index'); | ||
| 8 | + | ||
| 9 | + // Test the default behavior. | ||
| 10 | + assert.strictEqual( | ||
| 11 | + require.resolve('bar'), | ||
| 12 | + path.join(nodeModules, 'bar.js') | ||
| 13 | + ); | ||
| 14 | + | ||
| 15 | + // Verify that existing paths are removed. | ||
| 16 | + assert.throws(() => { | ||
| 17 | + require.resolve('bar', { paths: [] }) | ||
| 18 | + }, /^Error: Cannot find module 'bar'$/); | ||
| 19 | + | ||
| 20 | + // Verify that resolution path can be overwritten. | ||
| 21 | + { | ||
| 22 | + // three.js cannot be loaded from this file by default. | ||
| 23 | + assert.throws(() => { | ||
| 24 | + require.resolve('three') | ||
| 25 | + }, /^Error: Cannot find module 'three'$/); | ||
| 26 | + | ||
| 27 | + // However, it can be found if resolution contains the nested index directory. | ||
| 28 | + assert.strictEqual( | ||
| 29 | + require.resolve('three', { paths: [nestedIndex] }), | ||
| 30 | + path.join(nestedIndex, 'three.js') | ||
| 31 | + ); | ||
| 32 | + | ||
| 33 | + // Resolution from nested index directory also checks node_modules. | ||
| 34 | + assert.strictEqual( | ||
| 35 | + require.resolve('bar', { paths: [nestedIndex] }), | ||
| 36 | + path.join(nodeModules, 'bar.js') | ||
| 37 | + ); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + // Verify that the default paths can be used and modified. | ||
| 41 | + { | ||
| 42 | + const paths = require.resolve.paths('bar'); | ||
| 43 | + | ||
| 44 | + assert.strictEqual(paths[0], nodeModules); | ||
| 45 | + assert.strictEqual( | ||
| 46 | + require.resolve('bar', { paths }), | ||
| 47 | + path.join(nodeModules, 'bar.js') | ||
| 48 | + ); | ||
| 49 | + | ||
| 50 | + paths.unshift(nestedNodeModules); | ||
| 51 | + assert.strictEqual( | ||
| 52 | + require.resolve('bar', { paths }), | ||
| 53 | + path.join(nestedNodeModules, 'bar.js') | ||
| 54 | + ); | ||
| 55 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,4 +35,5 @@ assert.strictEqual( | |||
| 35 | 35 | require.resolve(fixtures.path('nested-index', 'one')).toLowerCase()); | |
| 36 | 36 | assert.strictEqual('path', require.resolve('path')); | |
| 37 | 37 | ||
| 38 | - console.log('ok'); | ||
| 38 | + // Test configurable resolve() paths. | ||
| 39 | + require(fixtures.path('require-resolve.js')); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments