oliverjam / oliverjames-v3

My [previous] website and blog
https://v3.oliverjam.es
13 stars 3 forks source link

Svelte rewrite #13

Closed oliverjam closed 4 years ago

oliverjam commented 4 years ago

Rewrite everything using Svelte and the experimental Eleventy addExtension API. What could go wrong?! πŸ™ƒ

Important ELEVENTY_EXPERIMENTAL=true is required before running any eleventy scripts, otherwise you don't get the new API.

Should really extract all the Svelte stuff from .eleventy.js into a plugin, but it's fine here until it stabilises a bit. Might want to consider using eleventy-assets instead of rolling my own asset manager, but this works for now 🀷

tl;dr for the Svelte integration:

  1. Tell Eleventy to run our custom engine for .svelte files with the new addExtension API.
  2. Tell Eleventy not to read the file contents (Svelte does that for us)
  3. Provide a compile function that returns the HTML for each page
    • This uses svelte/register to import the .svelte file
    • Uses the Svelte SSR API to render the component to HTML, CSS and head contents
    • Adds the CSS and head contents to their respective asset managers (to keep track of them for the final render)
    • Return the rendered HTML
  4. The final top-level layout must call the getSvelteCssForPage and getSvelteHeadForPage functions (provided as Eleventy JS filters) to get the aggregated Svelte <style> and <svelte:head> stuff to render