bun icon indicating copy to clipboard operation
bun copied to clipboard

Import "globals" using the `bun:` protocol

Open Conaclos opened this issue 11 months ago • 1 comments

What is the problem this feature would solve?

A bun "global" can be imported in two ways:

// 1. using `import`
import { sleep } from "bun"
sleep(1)

// 2. using the global variable `Bun`:
Bun.sleep(1)

The two approaches play badly with the current JS dev tools. The JS tools could be changed to properly support bun, but for now it is up to the developer to configure the tools. It is also unclear how bun should be supported. For example, a developer using the second approach will need to declare Bun globally in the linter config (ESlint, Biome, etc) to avoid Bun being flagged as an undeclared variable. The first approach is better in this respect, but other linter rules may consider it an undeclared dependency.

What is the feature you are proposing to solve the problem?

The use of protocols in imports have gained a lot of traction. node: is becoming the recommended way of importing node's builtin modules. Bun already use bun: for its built-in modules. Deno is using npm: and jsr:.

It looks like a missed opportunity for Bun not to always stick to the bun: protocol.

I propose allowing the import of bun's globals using the bun: protocol. For example, we could introduce bun:bun:

import { sleep } from "bun:bun"

Unfortunately, if I remember correctly, bun:bun was previously a thing. We could use another name. Some ideas:

  • bun:std
  • bun:base
  • bun: – however, this could be seen as an error?

We could even go beyond and phase out the use of the global Bun and the use of the bun import.

What alternatives have you considered?

No response

Conaclos avatar Mar 13 '24 10:03 Conaclos

Requirement

  • [ ] #6987

Malix-Labs avatar Apr 07 '24 11:04 Malix-Labs