Skip to content

Commit

Permalink
Add exception to allow fallbacks when resolving exports fail
Browse files Browse the repository at this point in the history
  • Loading branch information
tornadocontrib committed Apr 15, 2024
1 parent 9e7f576 commit 5da2db5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -187,7 +188,8 @@ export function nodeResolve(opts = {}) {
modulePaths,
rootDir,
ignoreSideEffectsForRoot,
allowExportsFolderMapping: options.allowExportsFolderMapping
allowExportsFolderMapping: options.allowExportsFolderMapping,
expectExportsError
});

const importeeIsBuiltin = isBuiltinModule(importee);
Expand Down
7 changes: 6 additions & 1 deletion packages/node-resolve/src/package/resolvePackageExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
31 changes: 18 additions & 13 deletions packages/node-resolve/src/resolveImportSpecifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
};
}
}
}
}
Expand Down Expand Up @@ -287,7 +290,8 @@ export default async function resolveImportSpecifiers({
modulePaths,
rootDir,
ignoreSideEffectsForRoot,
allowExportsFolderMapping
allowExportsFolderMapping,
expectExportsError
}) {
try {
const exportMapRes = await resolveWithExportMap({
Expand All @@ -304,7 +308,8 @@ export default async function resolveImportSpecifiers({
modulePaths,
rootDir,
ignoreSideEffectsForRoot,
allowExportsFolderMapping
allowExportsFolderMapping,
expectExportsError
});
if (exportMapRes) return exportMapRes;
} catch (error) {
Expand Down

0 comments on commit 5da2db5

Please sign in to comment.