Write HTML using Swift Macrosthe first of its kind.
Oh look, another Swift HTML DSL. What makes this one special?
Here are a few reasons:
- Swift Macros essentially eliminate any runtime overhead due to their compile-time nature
- HTML macros enforce compile-time safety, can be used anywhere, and compile directly to strings (optionally
StaticString
s) which are easily manipulated - The output is minified at no performance cost
Syntax
// <div class="dark"><p>Macros are beautiful</p></div>
#div(attributes: [.class(["dark"])], [
#p(["Macros are beautiful"])
])
// <a href="https://github.com/RandomHashTags/litleagues" target="_blank"></a>
#a(href: "https://github.com/RandomHashTags/litleagues", target: ._blank)
// <input id="funny-number" max="420" min="69" name="funny_number" step="1" type="number" value="69">
#input(
attributes: [.id("funny-number")],
max: 420,
min: 69,
name: "funny_number",
step: 1,
type: .number,
value: "69"
)
The README has more information and benchmarks comparing performance with 4 popular HTML libraries (Elementary, Plot, Swift Html, HTMLKit) with more coming very soon (and more complex HTML tests).
Benchmark Spoiler
This library is the clear winner in all aspects. Performance pushed to the absolute limits of the Swift language.
A 1.0.0 release is just around the corner, and I plan to use it in production at https://litleagues.com (currently uses Leaf) beginning next month. I saw a minimum of 10ms performance improvement to page loading using this library over Leaf even for a small website (probably due to minification | plus no extra disk reads).
Check it out: GitHub - RandomHashTags/swift-htmlkit: Write HTML using Swift Macros.
Let me know what you think and how I can improve it to support your workflow.