<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Geoff Goodman</title>
    <description>The latest articles on DEV Community by Geoff Goodman (@ggoodman).</description>
    <link>https://dev.to/ggoodman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F268961%2Fa199e1df-3b5b-4355-94fb-e9797dc81c9b.jpeg</url>
      <title>DEV Community: Geoff Goodman</title>
      <link>https://dev.to/ggoodman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ggoodman"/>
    <language>en</language>
    <item>
      <title>Introducing Full-stack Plugins</title>
      <dc:creator>Geoff Goodman</dc:creator>
      <pubDate>Thu, 21 Apr 2022 15:20:01 +0000</pubDate>
      <link>https://dev.to/ggoodman/introducing-full-stack-plugins-2l7a</link>
      <guid>https://dev.to/ggoodman/introducing-full-stack-plugins-2l7a</guid>
      <description>&lt;p&gt;A full-stack plugin is a single integration point to hook into the build pipeline, server-side rendering (SSR) of requests, server-to-client handoff of data and client-side rendering (CSR). This kind of plugin offers a drop-in solution for integrating cross-stack tooling that would otherwise be terrifying for users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;If you're a participant in modern web development -- willing or not -- you're undoubtedly familiar with build plugins. You might use them to convert your TypeScript to JavaScript or to allow you to seamlessly import CSS files. These are little wonders of modern developer experience (DX) tooling that let you move faster than the language or browser standards would otherwise allow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Sometimes the ambition of these plugins exceeds the capabilities of the tooling.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But sometimes the DX is ... let's say, &lt;em&gt;'lacking'&lt;/em&gt;. Sometimes the ambition of these plugins exceeds the capabilities of the tooling. Sometimes integration means successfully navigating and understanding a maze of instructions in the &lt;em&gt;"Advanced"&lt;/em&gt; section of docs.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is where things fall apart
&lt;/h2&gt;

&lt;p&gt;Let's say that you are a happy user of &lt;a href="https://styled-components.com/"&gt;styled-components&lt;/a&gt;. Everything is great until you decide to adopt integrate server-side rendering (SSR). Getting something like this set up has huge benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A powerful and and expressive DX to author components colocated with their styles&lt;/li&gt;
&lt;li&gt;Critical CSS extraction during SSR&lt;/li&gt;
&lt;li&gt;CSS pre-processing using all your favourite tools&lt;/li&gt;
&lt;li&gt;Better search engine ranking and shorter first contentful paint timing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a very compelling set of benefits but the &lt;strong&gt;up-front setup is not for the faint of heart&lt;/strong&gt;. Setting this up will require you to tweak a bunch of things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://styled-components.com/docs/tooling#babel-plugin"&gt;Configure the babel plugin&lt;/a&gt; in your bundler &lt;em&gt;du jour&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://styled-components.com/docs/advanced#server-side-rendering"&gt;Wire up server-side rendering&lt;/a&gt;

&lt;ol&gt;
&lt;li&gt;Construct a per-request &lt;code&gt;ServerStyleSheet&lt;/code&gt; instance.&lt;/li&gt;
&lt;li&gt;Wrap your app in a &lt;code&gt;&amp;lt;StyleSheetManager /&amp;gt;&lt;/code&gt; component and pass in the &lt;code&gt;ServerStyleSheet&lt;/code&gt; instance.&lt;/li&gt;
&lt;li&gt;Modify the HTML served to include style tags produced by the &lt;code&gt;ServerStyleSheet&lt;/code&gt; instance.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's quite a lot.&lt;/p&gt;

&lt;p&gt;But that's not &lt;code&gt;styled-components&lt;/code&gt;' fault. That's just how our tooling works right now. You have build tooling and you have runtime tooling &lt;em&gt;and never the twain shall meet&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bridging East and West
&lt;/h2&gt;

&lt;p&gt;So how do we bridge this divide? We need to rethink the relationship between our bundler and the runtime and get the two talking to each other.&lt;/p&gt;

&lt;p&gt;Let's say you're using &lt;a href="https://vitejs.dev"&gt;vite&lt;/a&gt;, here's what a Full-stack Plugin might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FullStackPlugin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;VitePlugin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * Provide a set of references to server-side plugins.
   * 
   * A server-side plugin can participate in the server-side rendering
   * pipeline and do things like:
   * 
   * - Set up any necessary per-request context (instances).
   * - Wrap the main component in Providers wired up to the context.
   * - Modify the HTML markup for the response.
   * - Modify the HTTP headers for the response
   * - Contribute to the server-to-client hand-off of data.
   */&lt;/span&gt;
  &lt;span class="nl"&gt;getServerRendererPlugins&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;MaybeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;RendererPluginReference&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Provide a set of references to client-side plugins.
     * 
     * A client-side plugin can participate in the client-side rendering
     * pipeline and do things like:
     * 
     * - Set up any necessary per-request context, having access to that
     *   request's server-to-client handoff data.
     * - Wrap the main component in Providers wired up to the context.
     * - Perform any blocking, asynchronous operations before actually
     *   doing the initial hydration.
     */&lt;/span&gt;
  &lt;span class="nl"&gt;getClientRendererPlugins&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;MaybeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;RendererPluginReference&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Describe where to find the code for the runtime plugin, including
 * which export to use (defaults to the default export) and any
 * configuration that needs to be serialized into that plugin at runtime.
 */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;RendererPluginReference&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;exportName&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MaybeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine the power this gives you as a library author.&lt;/p&gt;

&lt;p&gt;Here are some use-cases that are cleanly solved with Full-stack Plugins:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;SSR extraction of critical CSS for tools like &lt;code&gt;styled-components&lt;/code&gt;, &lt;code&gt;linaria&lt;/code&gt;, &lt;code&gt;twind&lt;/code&gt;, etc...&lt;/li&gt;
&lt;li&gt;Server-side data loading with &lt;code&gt;react-query&lt;/code&gt; and automatically setting up server-to-client hand-off of that data.&lt;/li&gt;
&lt;li&gt;Zero-cost code-splitting and lazy-loading of components, pages and data.&lt;/li&gt;
&lt;li&gt;Deep integration of &lt;code&gt;react-router&lt;/code&gt; for simple SSR / CSR integration or even automatic filesystem-based routing like &lt;code&gt;remix&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that is just the tip of the iceberg. What else can you imagine?&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting involved
&lt;/h2&gt;

&lt;p&gt;If you find this idea compelling, come and chat on Discord: &lt;a href="https://discord.gg/dSHCnQxbba"&gt;https://discord.gg/dSHCnQxbba&lt;/a&gt;. This is not just a pipe dream. There is working code that fulfills this promise that's making its way into the next version of &lt;a href="https://nostalgie.dev"&gt;Nostalgie&lt;/a&gt;. With the power and speed of &lt;a href="https://vitejs.dev"&gt;Vite&lt;/a&gt;, the experience might just blow your mind.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover image by Clark Van Der Beken: &lt;a href="https://unsplash.com/photos/Tk0B3Dfkf_4"&gt;https://unsplash.com/photos/Tk0B3Dfkf_4&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tooling</category>
    </item>
  </channel>
</rss>
