From 9f5ee9b4acf956ce08d15ff955ec30e56ff3015a Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Tue, 23 Apr 2024 12:43:46 -0400 Subject: [PATCH 1/3] fix(api/npm/module.js): preload package and ensure package type requested and loaded match --- api/npm/module.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/npm/module.js b/api/npm/module.js index d32c502b60..82e5c47cee 100644 --- a/api/npm/module.js +++ b/api/npm/module.js @@ -49,9 +49,12 @@ export async function resolve (specifier, origin = null, options = null) { const pathname = name.pathname.replace(name.value, '.') || '.' try { + pkg.load() // will call `pkg.load()` internally // can throw `ModuleNotFoundError` - const url = pkg.resolve(pathname, { prefix, type }) + const url = pkg.type === type + ? pkg.resolve(pathname, { prefix, type }) + : pkg.resolve(pathname, { prefix }) return { package: pkg, origin: pkg.origin, From ebfd80434c2eefcfd4489853cb71cd805963b799 Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Tue, 23 Apr 2024 12:46:15 -0400 Subject: [PATCH 2/3] chore(api/npm/module.js): clean up --- api/npm/module.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/api/npm/module.js b/api/npm/module.js index 82e5c47cee..8875e40eed 100644 --- a/api/npm/module.js +++ b/api/npm/module.js @@ -50,8 +50,6 @@ export async function resolve (specifier, origin = null, options = null) { try { pkg.load() - // will call `pkg.load()` internally - // can throw `ModuleNotFoundError` const url = pkg.type === type ? pkg.resolve(pathname, { prefix, type }) : pkg.resolve(pathname, { prefix }) From 9b71319f3960df43dfb91fcc412605ae4ac472a2 Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Tue, 23 Apr 2024 13:09:50 -0400 Subject: [PATCH 3/3] fix(api/commonjs/package.js): ensure package type is correctly derived --- api/commonjs/package.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/commonjs/package.js b/api/commonjs/package.js index bdd6cdcfb6..fc75581406 100644 --- a/api/commonjs/package.js +++ b/api/commonjs/package.js @@ -883,6 +883,10 @@ export class Package { } } + if (info.main && !info.module) { + this.#type = 'commonjs' + } + if (info.main) { this.#exports['.'].require = info.main if (info.type === 'module') { @@ -890,12 +894,13 @@ export class Package { } } - if (info.module) { + if (info.module && !info.main) { this.#exports['.'].import = info.module + this.#type = 'module' } if (typeof info.exports === 'string') { - if (type === 'commonjs') { + if (this.#type === 'commonjs') { this.#exports['.'].require = info.exports } else if (type === 'module') { this.#exports['.'].import = info.exports