seveibar / make-vfs

Quickly generate a module file that represents a directory as a virtual file system
5 stars 1 forks source link

add import-bunfile option #2

Closed myukselen closed 1 month ago

myukselen commented 1 month ago

Hi Severin,

Thanks for your nice cli! I repurposed to my need and it is working great! I'd love to share it back with you with this PR.

My usecase is to create a bun single file executable with elysia framework.

I utilize it in a quick and dirty way similar to this:

import staticFilesApp from "./static-files-app"

const app = new Elysia()
.onError((ctx) => {
    if (ctx.code === 'NOT_FOUND') {
        if (ctx.path.startsWith('/app/')) {
            // ctx.set.redirect = '/app/';  // looses path for the SPA
            ctx.set.status = 'OK'
            const path = ctx.path.slice(5);  // leading /app/ removed
            if (path in staticFilesApp) {
                return staticFilesApp[path];
            }
            return staticFilesApp['index.html'];
        } else {
            ctx.set.redirect = '/';
        }
        return '';
    }
})
...

Thanks, -Murat

seveibar commented 1 month ago

@myukselen we could add a section in the readme for how to use w/ Elysia, going to create an issue from this comment