oven-sh / bun

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

Bundler: Support for IIFE #10872

Open aralroca opened 1 week ago

aralroca commented 1 week ago

What is the problem this feature would solve?

Many of us who are doing frontend frameworks use IIFE instead of ESM as it allows it to run during HTML streaming without having to wait to parse all the HTML.

According to this source:

Module scripts behave like defer by default – there's no way to make a module script block the HTML parser while it fetches.

What is the feature you are proposing to solve the problem?

The feature is to support "format": "iife" in Bun.build. It has been announced for more than 1 year in the documentation that it is planned but there is no issue about it, I create it so that it can be planned and also comment some aspects to take into account.

What alternatives have you considered?

For now, I have a workaround that works a little bit, since it breaks other things like dynamic imports. The workaround is to create a file that has all the imports and no exports and this is the entrypoint with format="esm", it is also important that the splitting is not there, because otherwise it breaks it. And then as postbuild create the iife on top of it.

This I have considered it as a temporary alternative, although I begin to find several problems like that the dynamic imports stop being dynamic when not having splitting=true, and it is not possible to put it because it would break the iife. After thinking about improving the workaround to support dynamic imports, I think it is better to create the issue in Bun since the format="iife" would solve it.