Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experimental native bun support #156

Merged
merged 25 commits into from
Sep 7, 2023
Merged

feat: experimental native bun support #156

merged 25 commits into from
Sep 7, 2023

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Aug 9, 2023

πŸ”— Linked issue

resolves #154

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

When running in Bun runtime we can leverage Bun's native typescript and CJS handling.

Running current fixtures against Bun, it seems platform support is extremely good and we can directly depend on Bun's require implementation πŸ’―

There might be more runtime differences but I think generally for the sake of performance, worth to make Jiti bypass all transforms.

Known issues:

  • import.meta.env is not supported
  • Inside typescript files, cannot import with .mts as .mjs

On known issues, jiti falls back to it's default behavior to increase compatibility

/cc @birkskyum @paperdave @Jarred-Sumner

TODO:

  • Cache invalidation support (important for config reload like nuxt.config.ts)
  • Disable when custom transform options/plugins are provided (untyped babel plugin)
  • Allow opting-out (using JITI_EXPERIMENTAL_BUN: false or experimentalBun: false)

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@pi0 pi0 changed the title feat: support native bun imports feat: experimental native bun support Aug 9, 2023
package.json Outdated Show resolved Hide resolved
@Jarred-Sumner
Copy link

Inside typescript files, cannot import with .mjs extension

This sounds like a bug. Does it really not work in general with .mjs or is there something more specific going wrong?

@Jarred-Sumner
Copy link

To make import.meta.env work, I think you could override the getter on the import.meta object and define an env property to return either Bun.env or process.env.

Something like

const proto = Object.getPrototypeOf(import.meta)
proto.env = process.env 

@pi0
Copy link
Member Author

pi0 commented Aug 9, 2023

This sounds like a bug. Does it really not work in general with .mjs or is there something more specific going wrong?

Reproduction: https://github.com/unjs/jiti/blob/main/test/fixtures/typescript/index.ts

image

.mjs from .ts work in general. It is related to .mts support

@pi0
Copy link
Member Author

pi0 commented Aug 9, 2023

To make import.meta.env work, I think you could override the getter on the import.meta object and define an env property to return either Bun.env or process.env.

Thanks! I wasn't sure if extending import.meta is something safe for Bun but i can add polyfill like this if seems good to you! Just to make sure it is not planned in short term for native import.meta.env support?

@pi0 pi0 marked this pull request as ready for review September 7, 2023 10:11
@pi0 pi0 merged commit 36d24a5 into main Sep 7, 2023
3 checks passed
@pi0 pi0 deleted the feat/bun-support branch September 7, 2023 10:26
@@ -49,6 +50,7 @@ const defaults: JITIOptions = {
alias: _EnvAlias,
nativeModules: _EnvNative || [],
transformModules: _EnvTransform || [],
experimentalBun: _ExpBun === undefined ? !!process.versions.bun : !!_ExpBun,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving Bun detection into https://github.com/unjs/std-env?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bun compatibility/bypass
4 participants