Skip to content

Support bundling HTML files and their js, css, and assets in Bun.build and bun build - #15940

Merged
Jarred-Sumner merged 24 commits into
mainfrom
jarred/html-load
Dec 23, 2024
Merged

Jarred-Sumner merged 24 commits into
mainfrom
jarred/html-load

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Dec 22, 2024 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes #4688

This lets you bundle HTML files in Bun. It uses lol-html to parse and enqueue assets.

Currently, elements matching the following selectors are considered for either bundling (if js/css) or copied when non-external:

  • script[src]
  • link[rel='stylesheet'][href]
  • link[as='style'][href]
  • link[as='font'][href], link[type^='font/'][href]
  • link[as='image'][href]
  • link[as='video'][href], link[as='audio'][href]
  • link[as='worker'][href]
  • link[rel='manifest'][href]
  • link[rel='icon'][href], link[rel='apple-touch-icon'][href]
  • link:not([rel~='stylesheet']):not([rel~='modulepreload']):not([rel~='manifest']):not([rel~='icon']):not([rel~='apple-touch-icon'])[href]
  • img[src]
  • img[srcset]
  • video[src]
  • video[poster]
  • audio[src]
  • source[src]
  • source[srcset]
  • iframe[src]

This list will probably change before this merges. It should probably be configurable.

How did you verify your code works?

There are tests.

@robobun

robobun commented Dec 22, 2024 •

Copy link
Copy Markdown
Collaborator
Updated 10:09 AM PT - Dec 23rd, 2024

✅ @Jarred-Sumner, your commit 806c4b2 has passed in #8460! 🎉


🧪   try this PR locally:

bunx bun-pr 15940

@Jarred-Sumner
Jarred-Sumner marked this pull request as ready for review December 22, 2024 15:08

@small-angel small-angel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall legendary pr. very nice to have this.

random nitpicks and two that should be considered before merging. the main logic looks fine

Comment thread src/bake/DevServer.zig Resolved
Comment thread src/bundler/bundle_v2.zig Resolved
Comment thread src/bundler/bundle_v2.zig
// a <link rel="modulepreload" href="..." crossorigin> tag that
// points to the module or chunk's unique key so that we tell the
// browser to preload the user's code.
// We might also want to force <!DOCTYPE html> at the top of the file, but I'm not sure about that yet.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its fine to take no action re:doctype

the one person who wants to bundle quirks mode html can do so

Comment thread src/bundler/bundle_v2.zig Resolved
Comment thread src/html_scanner.zig Resolved
Comment thread src/html_scanner.zig Resolved
Comment thread test/bundler/bundler_html.test.ts Resolved
@ohroy

ohroy commented Dec 23, 2024

Copy link
Copy Markdown

awesome !!! but would you fix #5241 ? this block we switch to bun build

@Jarred-Sumner
Jarred-Sumner merged commit da54e81 into main Dec 23, 2024
@Jarred-Sumner
Jarred-Sumner deleted the jarred/html-load branch December 23, 2024 19:04
@marcospgp

Copy link
Copy Markdown

awesome addition!

robobun added a commit that referenced this pull request Oct 29, 2026
…unk namer

The note under `Multiple files share the same output path` names the
naming option to change. The report chose `entry` or `chunk` with its
own test. That test differs from the one in `compute_chunks` that chose
the template:

- The script and the stylesheet of a page printed
  `entry naming is '<chunk template>'` (since #15940).
- A page that only `import()` reaches printed
  `chunk naming is '<entry template>'` (since #15940).
- The entry point of an executable printed `entry naming is './[name]'`
  (since #41264, and for every collision with it since #41317). No
  naming option sets that template.
- The report kept one template for each option, so a second template
  under one option replaced the first.

`compute_chunks` now has one rule, `takes_entry_naming`. The namer and
`chunk_named_by` both call it, and the report matches on the result of
`chunk_named_by`. The chunk that `--compile` names after the output
file gets no naming note. The report prints one note for each template
of an option.
robobun added a commit that referenced this pull request Oct 29, 2026
…unk namer

The note under `Multiple files share the same output path` names the
naming option to change. The report chose `entry` or `chunk` with its
own test. That test differs from the one in `compute_chunks` that chose
the template:

- The script and the stylesheet of a page printed
  `entry naming is '<chunk template>'` (since #15940).
- A page that only `import()` reaches printed
  `chunk naming is '<entry template>'` (since #15940).
- The entry point of an executable printed `entry naming is './[name]'`
  (since #41264, and for every collision with it since #41317). No
  naming option sets that template.
- The report kept one template for each option, so a second template
  under one option replaced the first.

`compute_chunks` now has one rule, `takes_entry_naming`. The namer and
`chunk_named_by` both call it, and the report matches on the result of
`chunk_named_by`. The chunk that `--compile` names after the output
file gets no naming note. The report prints one note for each template
of an option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support index.html as an entrypoint

5 participants