jest-resolve
Advanced tools
Comparing version 29.3.1 to 29.4.0
@@ -28,4 +28,44 @@ 'use strict'; | ||
} | ||
var _resolve2 = require('resolve.exports'); | ||
var resolve = _interopRequireWildcard(require('resolve.exports')); | ||
var _fileWalkers = require('./fileWalkers'); | ||
function _getRequireWildcardCache(nodeInterop) { | ||
if (typeof WeakMap !== 'function') return null; | ||
var cacheBabelInterop = new WeakMap(); | ||
var cacheNodeInterop = new WeakMap(); | ||
return (_getRequireWildcardCache = function (nodeInterop) { | ||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop; | ||
})(nodeInterop); | ||
} | ||
function _interopRequireWildcard(obj, nodeInterop) { | ||
if (!nodeInterop && obj && obj.__esModule) { | ||
return obj; | ||
} | ||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) { | ||
return {default: obj}; | ||
} | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) { | ||
return cache.get(obj); | ||
} | ||
var newObj = {}; | ||
var hasPropertyDescriptor = | ||
Object.defineProperty && Object.getOwnPropertyDescriptor; | ||
for (var key in obj) { | ||
if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor | ||
? Object.getOwnPropertyDescriptor(obj, key) | ||
: null; | ||
if (desc && (desc.get || desc.set)) { | ||
Object.defineProperty(newObj, key, desc); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
} | ||
newObj.default = obj; | ||
if (cache) { | ||
cache.set(obj, newObj); | ||
} | ||
return newObj; | ||
} | ||
function _interopRequireDefault(obj) { | ||
@@ -74,6 +114,34 @@ return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
if (path.startsWith('#')) { | ||
const closestPackageJson = (0, _fileWalkers.findClosestPackageJson)( | ||
options.basedir | ||
); | ||
if (!closestPackageJson) { | ||
throw new Error( | ||
`Jest: unable to locate closest package.json from ${options.basedir} when resolving import "${path}"` | ||
); | ||
} | ||
const pkg = (0, _fileWalkers.readPackageCached)(closestPackageJson); | ||
const resolved = resolve.imports( | ||
pkg, | ||
path, | ||
createResolveOptions(options.conditions) | ||
); | ||
if (resolved) { | ||
const target = resolved[0]; | ||
return target.startsWith('.') | ||
? // internal relative filepath | ||
(0, _path().resolve)((0, _path().dirname)(closestPackageJson), target) | ||
: // this is an external module, re-resolve it | ||
defaultResolver(target, options); | ||
} | ||
if (pkg.imports) { | ||
throw new Error( | ||
'`imports` exists, but no results - this is a bug in Jest. Please report an issue' | ||
); | ||
} | ||
} | ||
const segments = path.split('/'); | ||
let moduleName = segments.shift(); | ||
if (moduleName) { | ||
// TODO: handle `#` here: https://github.com/facebook/jest/issues/12270 | ||
if (moduleName.startsWith('@')) { | ||
@@ -89,10 +157,15 @@ moduleName = `${moduleName}/${segments.shift()}`; | ||
const pkg = (0, _fileWalkers.readPackageCached)(closestPackageJson); | ||
if (pkg.name === moduleName && pkg.exports) { | ||
const subpath = segments.join('/') || '.'; | ||
const resolved = (0, _resolve2.resolve)( | ||
if (pkg.name === moduleName) { | ||
const resolved = resolve.exports( | ||
pkg, | ||
subpath, | ||
segments.join('/') || '.', | ||
createResolveOptions(options.conditions) | ||
); | ||
if (!resolved) { | ||
if (resolved) { | ||
return (0, _path().resolve)( | ||
(0, _path().dirname)(closestPackageJson), | ||
resolved[0] | ||
); | ||
} | ||
if (pkg.exports) { | ||
throw new Error( | ||
@@ -102,6 +175,2 @@ '`exports` exists, but no results - this is a bug in Jest. Please report an issue' | ||
} | ||
return (0, _path().resolve)( | ||
(0, _path().dirname)(closestPackageJson), | ||
resolved | ||
); | ||
} | ||
@@ -120,19 +189,18 @@ } | ||
const pkg = (0, _fileWalkers.readPackageCached)(packageJsonPath); | ||
if (pkg.exports) { | ||
const subpath = segments.join('/') || '.'; | ||
const resolved = (0, _resolve2.resolve)( | ||
pkg, | ||
subpath, | ||
createResolveOptions(options.conditions) | ||
); | ||
if (!resolved) { | ||
throw new Error( | ||
'`exports` exists, but no results - this is a bug in Jest. Please report an issue' | ||
); | ||
} | ||
const resolved = resolve.exports( | ||
pkg, | ||
segments.join('/') || '.', | ||
createResolveOptions(options.conditions) | ||
); | ||
if (resolved) { | ||
return (0, _path().resolve)( | ||
(0, _path().dirname)(packageJsonPath), | ||
resolved | ||
resolved[0] | ||
); | ||
} | ||
if (pkg.exports) { | ||
throw new Error( | ||
'`exports` exists, but no results - this is a bug in Jest. Please report an issue' | ||
); | ||
} | ||
} | ||
@@ -155,4 +223,4 @@ } | ||
// if it's a relative import or an absolute path, exports are ignored | ||
// if it's a relative import or an absolute path, imports/exports are ignored | ||
const shouldIgnoreRequestForExports = path => | ||
path.startsWith('.') || (0, _path().isAbsolute)(path); |
@@ -159,2 +159,3 @@ /** | ||
getModulePaths(from: string): Array<string>; | ||
getGlobalPaths(moduleName?: string): Array<string>; | ||
getModuleID( | ||
@@ -161,0 +162,0 @@ virtualMocks: Map<string, boolean>, |
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.GlobalPaths = void 0; | ||
exports.default = nodeModulesPaths; | ||
@@ -119,1 +120,14 @@ function path() { | ||
} | ||
function findGlobalPaths() { | ||
const {root} = path().parse(process.cwd()); | ||
const globalPath = path().join(root, 'node_modules'); | ||
const resolvePaths = require.resolve.paths('/'); | ||
if (resolvePaths) { | ||
// the global paths start one after the root node_modules | ||
const rootIndex = resolvePaths.indexOf(globalPath); | ||
return rootIndex > -1 ? resolvePaths.slice(rootIndex + 1) : []; | ||
} | ||
return []; | ||
} | ||
const GlobalPaths = findGlobalPaths(); | ||
exports.GlobalPaths = GlobalPaths; |
@@ -41,3 +41,3 @@ 'use strict'; | ||
var _isBuiltinModule = _interopRequireDefault(require('./isBuiltinModule')); | ||
var _nodeModulesPaths = _interopRequireDefault(require('./nodeModulesPaths')); | ||
var _nodeModulesPaths = _interopRequireWildcard(require('./nodeModulesPaths')); | ||
var _shouldLoadAsEsm = _interopRequireWildcard(require('./shouldLoadAsEsm')); | ||
@@ -170,3 +170,4 @@ function _interopRequireDefault(obj) { | ||
} catch (e) { | ||
if (options.throwIfNotFound) { | ||
// we always wanna throw if it's an internal import | ||
if (options.throwIfNotFound || path.startsWith('#')) { | ||
throw e; | ||
@@ -205,3 +206,4 @@ } | ||
} catch (e) { | ||
if (options.throwIfNotFound) { | ||
// we always wanna throw if it's an internal import | ||
if (options.throwIfNotFound || path.startsWith('#')) { | ||
throw e; | ||
@@ -453,4 +455,3 @@ } | ||
((0, _isBuiltinModule.default)(moduleName) || | ||
(moduleName.startsWith('node:') && | ||
(0, _isBuiltinModule.default)(moduleName.slice('node:'.length)))) && | ||
moduleName.startsWith('node:')) && | ||
!this._isAliasModule(moduleName) | ||
@@ -519,2 +520,8 @@ ); | ||
} | ||
getGlobalPaths(moduleName) { | ||
if (!moduleName || moduleName[0] === '.' || this.isCoreModule(moduleName)) { | ||
return []; | ||
} | ||
return _nodeModulesPaths.GlobalPaths; | ||
} | ||
getModuleID(virtualMocks, from, moduleName = '', options) { | ||
@@ -521,0 +528,0 @@ const stringifiedOptions = options ? JSON.stringify(options) : ''; |
{ | ||
"name": "jest-resolve", | ||
"version": "29.3.1", | ||
"version": "29.4.0", | ||
"repository": { | ||
@@ -22,12 +22,12 @@ "type": "git", | ||
"graceful-fs": "^4.2.9", | ||
"jest-haste-map": "^29.3.1", | ||
"jest-haste-map": "^29.4.0", | ||
"jest-pnp-resolver": "^1.2.2", | ||
"jest-util": "^29.3.1", | ||
"jest-validate": "^29.3.1", | ||
"jest-util": "^29.4.0", | ||
"jest-validate": "^29.4.0", | ||
"resolve": "^1.20.0", | ||
"resolve.exports": "^1.1.0", | ||
"resolve.exports": "^2.0.0", | ||
"slash": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@tsd/typescript": "~4.8.2", | ||
"@tsd/typescript": "^4.9.0", | ||
"@types/graceful-fs": "^4.1.3", | ||
@@ -44,3 +44,3 @@ "@types/pnpapi": "^0.0.2", | ||
}, | ||
"gitHead": "05deb8393c4ad71e19be2567b704dfd3a2ab5fc9" | ||
"gitHead": "4bc0e8acaf990e6618a7bed1dca67760c20bb12a" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
184130
14
2064
+ Added@types/node@22.8.7(transitive)
+ Addedresolve.exports@2.0.2(transitive)
- Removed@types/node@22.9.0(transitive)
- Removedresolve.exports@1.1.1(transitive)
Updatedjest-haste-map@^29.4.0
Updatedjest-util@^29.4.0
Updatedjest-validate@^29.4.0
Updatedresolve.exports@^2.0.0