oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.16k stars 2.68k forks source link

How do i proactively add packages to single-file executable? #11366

Open seepine opened 3 months ago

seepine commented 3 months ago

Discussed in https://github.com/oven-sh/bun/discussions/11019

Originally posted by **seepine** May 12, 2024 Try `index.ts` ```ts import fastify from "fastify"; const server = fastify({ logger: { transport: { target: "pino-pretty", options: { colorize: true, singleLine: true, translateTime: "SYS:standard", ignore: "pid,hostname", }, }, }, }); ``` And install deps ``` bun i fastify pino-pretty ``` Build single file ``` bun build --compile ./index.ts --outfile dist/app ``` It can not found `pino-pretty` ```sh error: unable to determine transport target for "pino-pretty" at V (B:/~BUN/root/app:84:52190) at iM_ (B:/~BUN/root/app:84:51319) at Z (B:/~BUN/root/app:84:58976) at pF (B:/~BUN/root/app:84:94445) at UE (B:/~BUN/root/app:84:101222) at LT_ (B:/~BUN/root/app:84:102217) at AA (B:/~BUN/root/app:320:108536) at B:/~BUN/root/app:432:14192 ```
sirenkovladd commented 3 months ago

Darwin 23.5.0 arm64 arm Bun v1.1.10

❯ bun build --compile ./index.ts --outfile dist/app 
  [28ms]  bundle  287 modules
 [109ms] compile  dist/app

❯ ./dist/app                                        
Server listening on http://[::1]:51781
[2024-05-28 13:51:53.838 -0700] INFO: Server listening at http://[::1]:51781
[2024-05-28 13:51:53.841 -0700] INFO: Server listening at http://127.0.0.1:51781
seepine commented 3 months ago

Darwin 23.5.0 arm64 arm Bun v1.1.10

❯ bun build --compile ./index.ts --outfile dist/app 
  [28ms]  bundle  287 modules
 [109ms] compile  dist/app

❯ ./dist/app                                        
Server listening on http://[::1]:51781
[2024-05-28 13:51:53.838 -0700] INFO: Server listening at http://[::1]:51781
[2024-05-28 13:51:53.841 -0700] INFO: Server listening at http://127.0.0.1:51781

Hi, maybe you need to clean up the environment, such as packaging binary files in Dockerfile

FROM seepine/alpine:glibc-3.18
WORKDIR /workspace
COPY ./dist/app /workspace/app
ENTRYPOINT ./app
vktrl commented 1 month ago

@sirenkovladd's example only works because the bundle has hardcoded paths to files in the local environment, but they won't be resolved in docker or another machine.

The answer to the question that was asked is to import or require the module somewhere in the code. You could do this, for example:

const transports = {
  pretty: require('pino-pretty'),
  file: require('pino/file'),
  otlp: require('pino-opentelemetry-transport'),
};

The real issue is that some Pino runtime dependencies are not getting bundled because they're used in worker threads. It requires a bundler plugin to make it work. Info about this here: https://github.com/pinojs/pino/blob/main/docs/bundling.md

Edit: I've created a bun plugin for Pino (does not handle --compile though): https://github.com/vktrl/bun-plugin-pino