twostraws / Ignite

A static site generator for Swift developers.
MIT License
984 stars 34 forks source link

Feature Request: Component Dependencies should be added on first use #14

Open fbartho opened 1 month ago

fbartho commented 1 month ago

Some Components depend on specific assets like CSS, JS, or other resources. It’d be great if there was a way for components to dynamically register those assets only if they’re used by the page they’re rendering into.

As an example, there’s some Ignite code that tries to figure out which languages are in use in order to add the correct prism code for that language to the page. This relies currently on users configuring the languages at the page level.

With an appropriate API, the Prism CodeBlock component could instead inspect the language it’s provided as an attribute, and request that Ignite ensures the dependency for that language was included.

Proposal

  1. Macro for Simple Dependencies — some components always need specific CSS/JS, so this could be a macro tagged on the component as a whole. @depends(on: […])
  2. Some components need finer grained dependencies, potentially contingent on instance-specific data attributes, this could be resolved with a RenderContext method registerDependency

under the hood, these could be implemented using the willRender method proposed elsewhere

An advantage of this approach is that Ignite’s core could be agnostic of what JS/CSS is in your page, and the Bootstrap-specific components would only pull in bootstrap if they’re used. The majority of Ignite’s components/Elements seem agnostic of Bootstrap.

twostraws commented 1 month ago

I did this in an earlier implementation, but removed it before release. However, very roughly I think the solution could be trivial: all elements are rendered with the current page context provided, so they can adjust that as needed – Images that use icons could enable the icon CSS, CodeBlock with a language could add that highlighter, etc.