-
-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node-resolve): Implement package exports / imports resolution alg…
…orithm according to Node documentation This fixes the package exports and imports resolution algorithm by strictly following the Node API documentation. For backwards compatibility a new option `allowExportsFolderMapping` is introduced which will enable deprecated folder mappings. Test case included Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
- Loading branch information
Showing
19 changed files
with
466 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
packages/node-resolve/src/package/resolvePackageExports.js
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
packages/node-resolve/src/package/resolvePackageExports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
InvalidModuleSpecifierError, | ||
InvalidConfigurationError, | ||
isMappings, | ||
isConditions, | ||
isMixedExports | ||
} from './utils'; | ||
import resolvePackageTarget from './resolvePackageTarget'; | ||
import resolvePackageImportsExports from './resolvePackageImportsExports'; | ||
|
||
/** | ||
* Implementation of PACKAGE_EXPORTS_RESOLVE | ||
*/ | ||
async function resolvePackageExports(context: any, subpath: string, exports: any) { | ||
// 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. | ||
throw new InvalidConfigurationError( | ||
context, | ||
'All keys must either start with ./, or without one.' | ||
); | ||
} | ||
|
||
// If subpath is equal to ".", then | ||
if (subpath === '.') { | ||
// Let mainExport be undefined. | ||
let mainExport: string | string[] | Record<string, any> | undefined; | ||
// If exports is a String or Array, or an Object containing no keys starting with ".", then | ||
if (typeof exports === 'string' || Array.isArray(exports) || isConditions(exports)) { | ||
// Set mainExport to exports | ||
mainExport = exports; | ||
// Otherwise if exports is an Object containing a "." property, then | ||
} else if (isMappings(exports)) { | ||
// Set mainExport to exports["."] | ||
mainExport = exports['.']; | ||
} | ||
|
||
// If mainExport is not undefined, then | ||
if (mainExport) { | ||
// Let resolved be the result of PACKAGE_TARGET_RESOLVE with target = mainExport | ||
const resolved = await resolvePackageTarget(context, { | ||
target: mainExport, | ||
patternMatch: '', | ||
isImports: false | ||
}); | ||
// If resolved is not null or undefined, return resolved. | ||
if (resolved) { | ||
return resolved; | ||
} | ||
} | ||
|
||
// Otherwise, if exports is an Object and all keys of exports start with ".", then | ||
} else if (isMappings(exports)) { | ||
// Let resolved be the result of PACKAGE_IMPORTS_EXPORTS_RESOLVE | ||
const resolvedMatch = await resolvePackageImportsExports(context, { | ||
matchKey: subpath, | ||
matchObj: exports, | ||
isImports: false | ||
}); | ||
|
||
// If resolved is not null or undefined, return resolved. | ||
if (resolvedMatch) { | ||
return resolvedMatch; | ||
} | ||
} | ||
|
||
// Throw a Package Path Not Exported error. | ||
throw new InvalidModuleSpecifierError(context); | ||
} | ||
|
||
export default resolvePackageExports; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
packages/node-resolve/src/package/resolvePackageImportsExports.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.