diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index 3d389383d..79bee7d5d 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -58,6 +58,7 @@ export function nodeResolve(opts = {}) { const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false; const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true; const rootDir = resolve(options.rootDir || process.cwd()); + const expectExportsError = options.expectExportsError ?? false; let { dedupe } = options; let rollupOptions; @@ -187,7 +188,8 @@ export function nodeResolve(opts = {}) { modulePaths, rootDir, ignoreSideEffectsForRoot, - allowExportsFolderMapping: options.allowExportsFolderMapping + allowExportsFolderMapping: options.allowExportsFolderMapping, + expectExportsError }); const importeeIsBuiltin = isBuiltinModule(importee); diff --git a/packages/node-resolve/src/package/resolvePackageExports.ts b/packages/node-resolve/src/package/resolvePackageExports.ts index 493b0b4ec..f7b10a4e1 100644 --- a/packages/node-resolve/src/package/resolvePackageExports.ts +++ b/packages/node-resolve/src/package/resolvePackageExports.ts @@ -11,7 +11,7 @@ import resolvePackageImportsExports from './resolvePackageImportsExports'; /** * Implementation of PACKAGE_EXPORTS_RESOLVE */ -async function resolvePackageExports(context: any, subpath: string, exports: any) { +async function resolvePackageExports(context: any, subpath: string, exports: any, expectError?: boolean) { // If exports is an Object with both a key starting with "." and a key not starting with "." if (isMixedExports(exports)) { // throw an Invalid Package Configuration error. @@ -64,6 +64,11 @@ async function resolvePackageExports(context: any, subpath: string, exports: any } } + if (expectError) { + console.log(`Could not find valid exports for ${context?.importSpecifier}, falling back`); + return; + } + // Throw a Package Path Not Exported error. throw new InvalidModuleSpecifierError(context); } diff --git a/packages/node-resolve/src/resolveImportSpecifiers.js b/packages/node-resolve/src/resolveImportSpecifiers.js index d29af56c1..a2f0925c8 100644 --- a/packages/node-resolve/src/resolveImportSpecifiers.js +++ b/packages/node-resolve/src/resolveImportSpecifiers.js @@ -116,7 +116,8 @@ async function resolveWithExportMap({ modulePaths, rootDir, ignoreSideEffectsForRoot, - allowExportsFolderMapping + allowExportsFolderMapping, + expectExportsError }) { if (importSpecifier.startsWith('#')) { // this is a package internal import, resolve using package imports field @@ -208,16 +209,18 @@ async function resolveWithExportMap({ allowExportsFolderMapping, conditions: exportConditions }; - const resolvedPackageExport = await resolvePackageExports(context, subpath, pkgJson.exports); - const location = fileURLToPath(resolvedPackageExport); - if (location) { - return { - location: preserveSymlinks ? location : await resolveSymlink(location), - hasModuleSideEffects, - hasPackageEntry, - packageBrowserField, - packageInfo - }; + const resolvedPackageExport = await resolvePackageExports(context, subpath, pkgJson.exports, expectExportsError); + if (resolvedPackageExport) { + const location = fileURLToPath(resolvedPackageExport); + if (location) { + return { + location: preserveSymlinks ? location : await resolveSymlink(location), + hasModuleSideEffects, + hasPackageEntry, + packageBrowserField, + packageInfo + }; + } } } } @@ -287,7 +290,8 @@ export default async function resolveImportSpecifiers({ modulePaths, rootDir, ignoreSideEffectsForRoot, - allowExportsFolderMapping + allowExportsFolderMapping, + expectExportsError }) { try { const exportMapRes = await resolveWithExportMap({ @@ -304,7 +308,8 @@ export default async function resolveImportSpecifiers({ modulePaths, rootDir, ignoreSideEffectsForRoot, - allowExportsFolderMapping + allowExportsFolderMapping, + expectExportsError }); if (exportMapRes) return exportMapRes; } catch (error) {