-
-
Notifications
You must be signed in to change notification settings - Fork 17
Design doc for reuse of defaultResolve #33
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
Open
jonaskello
wants to merge
13
commits into
nodejs:main
Choose a base branch
from
jonaskello:design-resolve-reuse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
043d9c3
Add doc
jonaskello 07293fc
Update doc/design-resolve-reuse.md
jonaskello dd62c6b
Add example API
jonaskello 3cee3ea
Remove impl code left in by mistake
jonaskello 7c0d408
Add example impl
jonaskello a262d0a
Correct call
jonaskello 9c1b37f
Add more example code
jonaskello 9a552ab
Add cxall to getConditionsSet
jonaskello 167fd57
Remove debug statement
jonaskello 3b695d6
Add some refactoring suggestions
jonaskello ca9fe00
Remove mention of file system hooks
jonaskello a802896
Resolver hook exampole
jonaskello 1f1fe71
Fallback to defaultResolve in example
jonaskello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,97 @@ | ||
# Re-use of node's esm resolve algorithm | ||
|
||
Some loaders want to run basically the same algorithm that node is doing by default (`defaultResolve`) but with a few tweaks. For example these loaders would benefit from re-use of `defaultResolve`: | ||
|
||
- Yarn PnP loader that read files from compressed archives instead of the file system | ||
- Typescript loader that translate the path to .ts/.tsx files | ||
|
||
In many cases it comes down to just altering how filsystem access is handled. For Yarn PnP it would just want to change where files are read, but the resolution algorithm would be the same as the default. | ||
|
||
Typescript files import from the future output so the imported file may not exist but need to be mapped back to the source file which potentially is in another directory (eg. if using project references and yarn workspaces). | ||
|
||
In other cases where larger alterations of the algorithm is desired it still might be useful to call into parts of the default algorithm(?). | ||
|
||
# Analysis of current file system access in defaultResolve | ||
|
||
The default resolve algorithm is implemented in [resolve.js](https://github.com/nodejs/node/blob/master/lib/internal/modules/esm/resolve.js). | ||
|
||
It uses filesystem by direct imports of `realpathSync` and `statSync` and also indirectly by import of `packageJsonReader`. | ||
|
||
`realpathSync` is only called from the top-level function `defaultResolve`. | ||
|
||
The following tree shows where `statSync` and `packageJsonReader.read` are called. It's not going all the way to the top of `defaultResolve` function but stopping at functions `packageResolve`, `finalizeResolution` and `moduleResolve`. | ||
|
||
- statSync | ||
|
||
- tryStatSync | ||
- finalizeResolution | ||
- packageResolve | ||
- fileExists | ||
- legacyMainResolve | ||
- packageResolve | ||
- resolveExtensionsWithTryExactName | ||
- resolveDirectoryEntry | ||
- finalizeResolution | ||
- finalizeResolution | ||
- resolveExtensions | ||
- resolveExtensionsWithTryExactName | ||
- resolveDirectoryEntry | ||
- finalizeResolution | ||
- finalizeResolution | ||
- resolveDirectoryEntry | ||
- finalizeResolution | ||
- resolveDirectoryEntry | ||
- finalizeResolution | ||
|
||
- packageJsonReader.read | ||
|
||
- getPackageConfig | ||
|
||
- getPackageScopeConfig | ||
- packageImportsResolve | ||
- moduleResolve | ||
- defaultResolve | ||
- getPackageType (not called within resolve.js) | ||
- packageResolve | ||
- packageResolve | ||
|
||
- resolveDirectoryEntry | ||
- finalizeResolution | ||
|
||
The analysis shows that filesystem access occurs mainly as an effect of calls to `packageResolve`, `finalizeResolution`, and `moduleResolve`. | ||
|
||
`moduleResolve` is only called by the top-level `defaultResolve` function. | ||
`finalizeResolution` is only called from `moduleResolve`. | ||
|
||
`packageResolve` is called in multiple places, it is also called recursively: | ||
|
||
- packageResolve | ||
|
||
- resolvePackageTargetString | ||
|
||
- resolvePackageTarget | ||
- resolvePackageTarget (recursive) | ||
- packageExportsResolve | ||
- packageResolve (recursive) | ||
- packageImportsResolve | ||
- moduleResolve | ||
- defaultResolve | ||
|
||
- moduleResolve | ||
- defaultResolve | ||
|
||
# Stragegies for re-use of defaultResolve | ||
|
||
## Filesystem hooks | ||
|
||
Using this strategy, the loader would provide hooks for filesystem calls, specifically hooks corresponding to `realpathSync`, `statSync` and `packageJsonReader.read`. | ||
|
||
## Utility functions | ||
|
||
This strategy would export utility functions so a custom loader could pick which parts of the default algorithm's logic it wants to call. If utility functions are exported, they probably should not do any filesystem access or throw exceptions (but instead return a value indicating success/fail). | ||
|
||
Specifically we could refactor `resolve.js` so that `packageExportsResolve` and `packageImportsResolve` are exported and take a custom `packageResolve` function instead of calling the internal one. The loader could then call `packageExportsResolve` and `packageImportsResolve` as utility functions while providing its own `packageResolve` so it can control filesystem access. | ||
|
||
If the loader should provide its own `packageResolve` it could be useful to break out some parts of the default implementation. Eg. the part that finds package.json by ascending the file system and the part that checks for self resolve within the current package. The self-resolve part does not do any filesystem access so it could perhaps be moved so it does not have to be part of the `packageResolve` that the loader provides. | ||
|
||
Using this strategy the utility functions exported would be free of filesystem side-effects and the loader would do any such effects needed itself. So this would be akin to a [functional-core-imperative-shell](https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell) stratgegy. Altough the utility functions would be "pure" only in the sense that they do no filesystem side-effects or throw exceptions. Altough the utility functions cannot be made 100% pure, they probably could be made idempotent. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.