Skip to content

Commit

Permalink
feat(resolve): export resolve() function (#354)
Browse files Browse the repository at this point in the history
nft allows you to override the resolve step with a hook however it does
not expose its internal resolveDependency implementation. This means if
you want to take over resolving in certain cases you must do so in every
case. To make conditional or augmented resolving possible this change
reexports the resolveDependency function from the index. This way you
can use it in custom resolve hook implementations

---------

Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
gnoff and styfle authored Jul 14, 2023
1 parent f41f349 commit 0da80c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ The following FS functions can be hooked by passing them as options:
* `readlink(path): Promise<string>`
* `resolve(id: string, parent: string): Promise<string | string[]>`
##### Advanced Resolving
When providing a custom resolve hook you are responsible for returning one or more absolute paths to resolved files based on the `id` input. However it may be the case that you only want to augment or override the resolve behavior in certain cases. You can use `nft`'s underlying resolver by importing it. The builtin `resolve` function expects additional arguments that need to be forwarded from the hook
* `resolve(id: string, parent: string, job: Job, isCjs: boolean): Promise<string | string[]>`
Here is an example showing one id being resolved to a bespoke path while all other paths being resolved by the built-in resolver
```js
const { nodeFileTrace, resolve } = require('@vercel/nft');
const files = ['./src/main.js', './src/second.js'];
const { fileList } = await nodeFileTrace(files, { resolve: async (id, parent, job, isCjs) => {
if (id === './src/main.js') {
return '/path/to/some/resolved/main/file.js'
} else {
return resolve(id, parent, job, isCjs)
}
}});
```
#### TypeScript
The internal resolution supports resolving `.ts` files in traces by default.
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './types';
export { nodeFileTrace } from './node-file-trace';
export { nodeFileTrace } from './node-file-trace';
import resolveDependency from './resolve-dependency';
export { resolveDependency as resolve };

0 comments on commit 0da80c0

Please sign in to comment.