-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #43967 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
- Loading branch information
1 parent
8c2d19b
commit e072c3a
Showing
3 changed files
with
153 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
'use strict'; | ||
|
||
const { | ||
JSONParse, | ||
SafeMap, | ||
StringPrototypeEndsWith, | ||
} = primordials; | ||
const { URL, fileURLToPath } = require('internal/url'); | ||
const { | ||
ERR_INVALID_PACKAGE_CONFIG, | ||
} = require('internal/errors').codes; | ||
|
||
const packageJsonReader = require('internal/modules/package_json_reader'); | ||
|
||
|
||
/** | ||
* @typedef {string | string[] | Record<string, unknown>} Exports | ||
* @typedef {'module' | 'commonjs'} PackageType | ||
* @typedef {{ | ||
* pjsonPath: string, | ||
* exports?: ExportConfig, | ||
* name?: string, | ||
* main?: string, | ||
* type?: PackageType, | ||
* }} PackageConfig | ||
*/ | ||
|
||
/** @type {Map<string, PackageConfig>} */ | ||
const packageJSONCache = new SafeMap(); | ||
|
||
|
||
/** | ||
* @param {string} path | ||
* @param {string} specifier | ||
* @param {string | URL | undefined} base | ||
* @returns {PackageConfig} | ||
*/ | ||
function getPackageConfig(path, specifier, base) { | ||
const existing = packageJSONCache.get(path); | ||
if (existing !== undefined) { | ||
return existing; | ||
} | ||
const source = packageJsonReader.read(path).string; | ||
if (source === undefined) { | ||
const packageConfig = { | ||
pjsonPath: path, | ||
exists: false, | ||
main: undefined, | ||
name: undefined, | ||
type: 'none', | ||
exports: undefined, | ||
imports: undefined, | ||
}; | ||
packageJSONCache.set(path, packageConfig); | ||
return packageConfig; | ||
} | ||
|
||
let packageJSON; | ||
try { | ||
packageJSON = JSONParse(source); | ||
} catch (error) { | ||
throw new ERR_INVALID_PACKAGE_CONFIG( | ||
path, | ||
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier), | ||
error.message | ||
); | ||
} | ||
|
||
let { imports, main, name, type } = packageJSON; | ||
const { exports } = packageJSON; | ||
if (typeof imports !== 'object' || imports === null) { | ||
imports = undefined; | ||
} | ||
if (typeof main !== 'string') { | ||
main = undefined; | ||
} | ||
if (typeof name !== 'string') { | ||
name = undefined; | ||
} | ||
// Ignore unknown types for forwards compatibility | ||
if (type !== 'module' && type !== 'commonjs') { | ||
type = 'none'; | ||
} | ||
|
||
const packageConfig = { | ||
pjsonPath: path, | ||
exists: true, | ||
main, | ||
name, | ||
type, | ||
exports, | ||
imports, | ||
}; | ||
packageJSONCache.set(path, packageConfig); | ||
return packageConfig; | ||
} | ||
|
||
|
||
/** | ||
* @param {URL | string} resolved | ||
* @returns {PackageConfig} | ||
*/ | ||
function getPackageScopeConfig(resolved) { | ||
let packageJSONUrl = new URL('./package.json', resolved); | ||
while (true) { | ||
const packageJSONPath = packageJSONUrl.pathname; | ||
if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json')) { | ||
break; | ||
} | ||
const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), resolved); | ||
if (packageConfig.exists) { | ||
return packageConfig; | ||
} | ||
|
||
const lastPackageJSONUrl = packageJSONUrl; | ||
packageJSONUrl = new URL('../package.json', packageJSONUrl); | ||
|
||
// Terminates at root where ../package.json equals ../../package.json | ||
// (can't just check "/package.json" for Windows support). | ||
if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) { | ||
break; | ||
} | ||
} | ||
const packageJSONPath = fileURLToPath(packageJSONUrl); | ||
const packageConfig = { | ||
pjsonPath: packageJSONPath, | ||
exists: false, | ||
main: undefined, | ||
name: undefined, | ||
type: 'none', | ||
exports: undefined, | ||
imports: undefined, | ||
}; | ||
packageJSONCache.set(packageJSONPath, packageConfig); | ||
return packageConfig; | ||
} | ||
|
||
|
||
module.exports = { | ||
getPackageConfig, | ||
getPackageScopeConfig, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters