evanw / evanw/esbuild

Support non-relative run-time import paths

Open
#3,349 1 comment 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

Since https://github.com/evanw/esbuild/releases/tag/v0.19.0 there is *Run-time import paths*, which might be useful in some cases ([or not](https://github.com/evanw/esbuild/issues/3337)), however, this release features following:

> The import path expression must be a form of string concatenation and must start with either ./ or ../

But why does this restriction exist? When I was migrating from webpack I encountered code that tried to do import like this (simplified example);

```ts
let activateFeatureA = true
const file = require(`eslint/${activateFeatureA ? 'api.js' : 'cli.js'}`)

console.log(file)
```

this will eventually fail and esbuild without any warnings. However this will be okay:

```ts
let activateFeatureA = true
const file = require(`./node_modules/eslint/${activateFeatureA ? 'api.js' : 'cli.js'}`)

console.log(file)
```

So, in this first example, I believe esbuild still can resolve the first part before non-string expression (`eslint/`), why it doesn't do it?

**I also didn't find any way to intercept (or fix) this behavior with plugins**, so I don't know how to workaround. The most annoying part is that you don't get any warning by default or when I pass the following config values:

```
{
logLevel: 'info',
// with setting following or not:
logOverride: {
'unsupported-require-call': 'debug',
}
}
```

Only if you set `logLevel` to debug or verbose you eventually get the `unsupported-require-call` message:
```
{
logLevel: 'debug',
logOverride: {
'unsupported-require-call': 'debug', // can be debug verbose
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the two `require` examples against esbuild v0.19.0 or later, then read the runtime import-path behavior and the `unsupported-require-call` diagnostic. Trace how the non-relative prefix is handled and determine the intended resolution or warning behavior. Done means the first example is no longer silently unsupported, or its limitation is clearly surfaced.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.