Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! esm: move package config helpers
  • Loading branch information
GeoffreyBooth committed Jul 25, 2022
commit c39d5801ddd14c7d594b4aa2e11ca3b0343901f5
36 changes: 25 additions & 11 deletions lib/internal/modules/esm/package_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const packageJsonReader = require('internal/modules/package_json_reader');
* }} PackageConfig
*/

const packageJSONCache = new SafeMap(); /* string -> PackageConfig */
/** @type {Map<string, PackageConfig>} */
const packageJSONCache = new SafeMap();


/**
Expand All @@ -34,7 +35,7 @@ const packageJSONCache = new SafeMap(); /* string -> PackageConfig */
* @param {string | URL | undefined} base
* @returns {PackageConfig}
*/
function getPackageConfig(path, specifier, base) {
function getPackageConfig(path, specifier, base) {
const existing = packageJSONCache.get(path);
if (existing !== undefined) {
return existing;
Expand Down Expand Up @@ -67,11 +68,19 @@ const packageJSONCache = new SafeMap(); /* string -> PackageConfig */

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 };
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' };
if (type !== 'module' && type !== 'commonjs') {
type = 'none';
}

const packageConfig = {
pjsonPath: path,
Expand All @@ -95,17 +104,22 @@ 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; }
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; }
if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) {
break;
}
}
const packageJSONPath = fileURLToPath(packageJSONUrl);
const packageConfig = {
Expand Down
9 changes: 7 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ const { Module: CJSModule } = require('internal/modules/cjs/loader');
const packageJsonReader = require('internal/modules/package_json_reader');
const { getPackageConfig, getPackageScopeConfig } = require('internal/modules/esm/package_config');

/**
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
*/


const userConditions = getOptionValue('--conditions');
const noAddons = getOptionValue('--no-addons');
const addonConditions = noAddons ? [] : ['node-addons'];
Expand Down Expand Up @@ -168,7 +173,7 @@ function fileExists(url) {
* 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node)
* 5. NOT_FOUND
* @param {URL} packageJSONUrl
* @param {import('internal/modules/esm/package_config.js').PackageConfig} packageConfig
* @param {PackageConfig} packageConfig
* @param {string | URL | undefined} base
* @returns {URL}
*/
Expand Down Expand Up @@ -536,7 +541,7 @@ function isConditionalExportsMainSugar(exports, packageJSONUrl, base) {
/**
* @param {URL} packageJSONUrl
* @param {string} packageSubpath
* @param {import('internal/modules/esm/package_config.js').PackageConfig} packageConfig
* @param {PackageConfig} packageConfig
* @param {string | URL | undefined} base
* @param {Set<string>} conditions
* @returns {URL}
Expand Down