Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node-resolve): Add exception to allow fallbacks when resolving exports fail. #1704

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
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
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
Loading