#Add files to executable with dynamic paths at compile time

1 messages · Page 1 of 1 (latest)

proud iron
#

Hi!

I wanted to make a simple hono server that would serve static files that would be packaged inside the executable produced by bun build --compile. I know this is "bleeding edge" and --compile isn't really ready yet but I really wanted to package an executable with bun and not with node due to the power and simpilicty bun introduces.

According to https://github.com/oven-sh/bun/issues/5445 there isn't a way to link a directory to be packaged. However files imported with type file were included in the executable.

That begs the question, can I somehow dynamically link a file to be included in the executable?

if (env.data.NODE_ENV == "production") {
    
    const zip = await import("./dist/frontend.zip");

    const unzipped = 
        unzipSync(new Uint8Array(await Bun.file(zip.default).arrayBuffer()))

}

The following code built with bun build --compile does include the frontend.zip inside, so it can be used. However the path to the zip file in my case is dynamic (dictated by .env).

So is there a way to import the file at compile time so it can be accessed with Bun.file? Maybe an --import flag would help followed by files to be imported or a special syntax so the compiler knows to replace said string with an env variable?

clear mist
proud iron
#

Actually everything I needed was indeed in the documentation although it wasn't mentioned in the executables section. In the end I used --define to define a path at compile time bun build --compile --loader .zip:file index.ts --define "FRONTEND_ZIP_PATH=./ui/dist/frontend.zip" --define "NODE_ENV=\"production\"" and imported it as a global

const zip = await import(FRONTEND_ZIP_PATH);
clear mist
#

Ah yeah we should add more examples with define