Skip to content

Commit 0da80c0

Browse files
gnoffstyfle
andauthored
feat(resolve): export resolve() function (#354)
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>
1 parent f41f349 commit 0da80c0

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ The following FS functions can be hooked by passing them as options:
109109
* `readlink(path): Promise<string>`
110110
* `resolve(id: string, parent: string): Promise<string | string[]>`
111111
112+
##### Advanced Resolving
113+
114+
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
115+
116+
* `resolve(id: string, parent: string, job: Job, isCjs: boolean): Promise<string | string[]>`
117+
118+
Here is an example showing one id being resolved to a bespoke path while all other paths being resolved by the built-in resolver
119+
```js
120+
const { nodeFileTrace, resolve } = require('@vercel/nft');
121+
const files = ['./src/main.js', './src/second.js'];
122+
const { fileList } = await nodeFileTrace(files, { resolve: async (id, parent, job, isCjs) => {
123+
if (id === './src/main.js') {
124+
return '/path/to/some/resolved/main/file.js'
125+
} else {
126+
return resolve(id, parent, job, isCjs)
127+
}
128+
}});
129+
```
130+
112131
#### TypeScript
113132
114133
The internal resolution supports resolving `.ts` files in traces by default.

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './types';
2-
export { nodeFileTrace } from './node-file-trace';
2+
export { nodeFileTrace } from './node-file-trace';
3+
import resolveDependency from './resolve-dependency';
4+
export { resolveDependency as resolve };

0 commit comments

Comments
 (0)