Skip to content

Conversation

@gytic
Copy link
Contributor

@gytic gytic commented Dec 5, 2025

Motivation

I often use top-level awaits inside my application code.
When I want to package these applications with bun2nix
I then have to remove the --bytecode flag (see why).

For this to work I then have to re-implement the correct module path and output path options
which is a bit anoying.

Solution

Add an option for bun2nix.mkDerivation which toggles whether to use the --bytecode flag with the build command.

Other options

It might also be usefull to expose the entry point and expected out path via environment variables
so they can be consumed by custom flags set via bunBuildFlags.
However I choose the "simpler" derivation toggle because from my experience that is the only flag I oten need to control
and the rest stays as-is.

compiling to bytecode removes top-level await support.
This will make it easier for application that rely on top-level await
to omit the --bytecode flag
Copy link
Collaborator

@baileylu121 baileylu121 left a comment

Choose a reason for hiding this comment

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

Thanks very much for this, I hope it comes in handy, I'm generally quite happy with this, I just want to enable the nix-community CI for external repos before I merge it.

"--minify"
"--sourcemap"
]
++ lib.optional (module != null && bunCompileToBytecode) [
Copy link
Collaborator

Choose a reason for hiding this comment

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

lib.optional already returns a list, you can use lib.optionals instead.

This statement should really look something like this, seems like it was my mistake using it in the other case:

bunBuildFlags =
  if (args ? bunBuildFlags) then
    args.bunBuildFlags
  else
    lib.optionals (module != null) ([
      "${module}"
      "--outfile"
      "${pname}"
      "--compile"
      "--minify"
      "--sourcemap"
    ]
    ++ lib.optional bunCompileToBytecode "--bytecode"
    );

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.

2 participants