Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Jarred-Sumner
Copy link
Collaborator

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

What does this PR do?

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
Copy link

robobun commented Dec 22, 2024

Updated 7:35 AM PT - Dec 22nd, 2024

@Jarred-Sumner, your commit 1059b90 has passed in #8419! 🎉


🧪   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
Copy link
Member

@paperdave paperdave left a comment

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

@@ -9,7 +9,7 @@
pub const DevServer = @This();
pub const debug = bun.Output.Scoped(.Bake, false);
pub const igLog = bun.Output.scoped(.IncrementalGraph, false);

const Chunk = @import("../bundler//bundle_v2.zig").Chunk;
Copy link
Member

Choose a reason for hiding this comment

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

avoid file imports if possible

const Chunk = bun.bundle_v2.Chunk

(move this at the end of the file)

@@ -6036,6 +6113,12 @@ pub const LinkerContext = struct {
}
}

if (experimental_html) {
for (html_chunks.values()) |*chunk| {
Copy link
Member

Choose a reason for hiding this comment

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

appendSliceAssumeCapacity

// 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
Member

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

}

pub fn onTag(this: *@This(), element: *lol.Element, path: []const u8, url_attribute: []const u8, kind: ImportKind) void {
_ = kind; // autofix
Copy link
Member

Choose a reason for hiding this comment

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

no autofix comments pls

remove the comment or make the parameter name _

},
// Catch-all for other links with href
.{
.selector = "link:not([rel~='stylesheet']):not([rel~='modulepreload']):not([rel~='manifest']):not([rel~='icon']):not([rel~='apple-touch-icon'])[href]",
Copy link
Member

Choose a reason for hiding this comment

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

not 100% sure if this is a good idea.

@@ -0,0 +1,297 @@
const std = @import("std");
Copy link
Member

Choose a reason for hiding this comment

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

whenever a file with a single struct is added, make that struct top-level and make the file name the name of that struct (html_scanner.zig to HTMLScanner.zig). when i do these, i then make one of the first line the text const StructName = @This() directly followed by the fields.

this pattern pairs really well with imports at the end of the file, so the start reads well

expect(page1Css).toContain(".shared");
expect(page2Css).toContain(".shared");
},
});
Copy link
Member

Choose a reason for hiding this comment

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

can a test that has a js entrypoint importing an html file be added

additionally a js file importing an html file importing the first js file

i assume these already work but would be good to have it tested

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.

3 participants