evanw / evanw/esbuild

Add resolveDir into OnResolveResult and OnLoadArgs

Open
#614 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Currently we have 2 hooks `onResolve()` and `onLoad()`, both have mostly symmetric input and output. However, `resolveDir` is respecting in result of `onLoad()` (`OnLoadResult`) only and using as input for `onResolve()` (`OnResolveArgs`). My suggestion is to respect `resolveDir` in `OnResolveResult` and pass it as `OnLoadArgs` into `onLoad()` too. `resolveDir` should be optional for `OnResolveResult` and be computed when omitted as `path.dirname(result.path)`.

This change may help to simplify custom loading a bit:

```js
onLoad({ ... }, args => ({
resolveDir: path.dirname(args.path),
contents: ...
}));
// ->
onLoad({ ... }, args => ({
resolveDir: args.resolveDir,
contents: ...
}));
```

It's much more useful when you need to specify `resolveDir` on resolving that differs from a path. Like in my case, when I use some common (generated) files for modules, but resolve imports depending on a module. Since `onResolve()` doesn't respect `resolveDir`, instead of:

```js
onResolve({ filter: /.../ }, args => ({
resolveDir: somethingFromPath(args.path),
path: 'path/to/common/file'
}));
```

I need to do the following (as workaround):

```js
onResolve({ filter: /.../ }, args => ({
namespace: 'common-file-example',
path: somethingFromPath(args.path)
}));
onLoad({ namespace: 'common-file-example', filter: /.*/ }, args => ({
resolveDir: args.path,
contents: fs.readFileSync('path/to/common/file')
}));
```

Personally, I prefer to avoid overloading loading behaviour as much as possible, because I believe `esbuild` makes it better. Such a change will avoid the need to do this.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start from the onResolve() and onLoad() plugin API entry points and trace how OnResolveResult, OnLoadArgs, and resolveDir move through module resolution and loading. Confirm the existing fallback from an omitted OnResolveResult.resolveDir, pass the resolved directory into onLoad(), and verify the documented examples and behavior for both hooks.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript
Domain
api, build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.