elysiajs / elysia-html

A plugin for Elysia that add support for returning html
MIT License
24 stars 18 forks source link

Using `Bun.build` will result into errors when running the output. #14

Closed awilderink closed 1 year ago

awilderink commented 1 year ago

If you build a project with bun using this package, you can't run the built file.

How to reproduce

This could also be an issue with bun build or @kitajs/html.

SeqSEE commented 1 year ago
await Bun.build({
    entrypoints: ['./src/index.tsx'],
    outdir: './dist',
    target: 'bun',
})

Same issue for me using a build.ts file and bun build.ts

awilderink commented 1 year ago

I found the issue. @kitajs/html is imported through require statements. This should be fine, but bun probably builds this wrong (maybe an issue should be created but I'm not sure how to describe it). You can fix it by swapping the require statement for imports. I want to submit a PR but the repo won't let me.

Here are the changes

// index.ts
...
import { createElement, Fragment as F } from  '@kitajs/html'
globalThis.ElysiaJSX = createElement
// @ts-ignore
globalThis.ElysiaJSX.Fragment = Fragment

export const Fragment = F as unknown as Component 

export default html
...

and

// declaration.ts
// remove all of the following since they have been moved to index.ts (imports in declaration files will make them not globally available, see https://stackoverflow.com/a/51114250)

// @ts-ignore
const { createElement, Fragment } = require('@kitajs/html')

// @ts-ignore
globalThis.ElysiaJSX = createElement
// @ts-ignore
globalThis.ElysiaJSX.Fragment = Fragment
arthurfiorette commented 1 year ago

Hey @awilderink. After this change, does this problem still occurs?

arthurfiorette commented 1 year ago

Seems to be a issue with CJS syntax when using the bun bundler: https://github.com/oven-sh/bun/issues/5654

akhar commented 1 year ago

Does it have any workaround at the moment?

arthurfiorette commented 1 year ago

I'm releasing a fix for @kitajs/html asap. But it will keep incompatibilities with https://github.com/elysiajs/elysia/issues/178.