Skip to main content

Advanced Guide: Authoring Plugins

Plugins are the primary way to extend Coralite's functionality. The framework's architecture is designed to be highly extensible via the createPlugin function.

With plugins, you can hook deep into the build lifecycle, register custom components globally, bundle external scripts, and inject custom helper functions directly into the frontend.

The Conceptual Flow #

Authoring a plugin involves understanding how Coralite synthesizes server-side rendering and client-side hydration. When you register a plugin, Coralite integrates it through the following flow:

  1. Registration & Bootstrapping: Coralite loads your plugin via createPlugin, reading its client.config and bundling any dependencies defined in client.imports.
  2. Server Execution: As the generator processes pages and encounters dynamic components, it fires your plugin's lifecycle hooks (e.g., onPageSet, onBeforePageRender). It also executes client.setup() asynchronously to fetch any required data.
  3. Data Binding & Injection: Coralite processes the component's tokens and slots. Crucially, it merges the data returned from your client.setup() into the component's values object.
  4. Client Hydration: Finally, Coralite serializes your component's client.script and your plugin's custom helpers. When the browser loads the page, the bundled modules are provided securely via the helpers context, enabling dynamic interactions.

Server-Side Lifecycle Hooks #

Plugins can hook into almost every phase of Coralite's build lifecycle to perform actions when files are processed or rendering occurs.

Global Component Registration #

Plugins can distribute their own HTML components. By providing an array of file paths to the components property in your plugin configuration, Coralite automatically reads, parses, and registers these files as global components during initialization.

This means users of your plugin can instantly use your custom tags (e.g., <my-awesome-slider>) without having to copy the HTML files into their own project's src/components/ directory.

Injecting Client Helpers #

The most powerful feature of Coralite plugins is the ability to provide reusable functionality directly to component scripts using client.helpers.

Helpers are injected into the context.helpers object available in every client.script. However, because Coralite is a static site generator, there is a strict boundary between server code and client code.

The Factory Pattern #

Helpers must be structured as "factory functions" that execute server-side. They accept a context object and return the actual executable function that will be serialized and sent to the browser.

To use client-only APIs (like window or document), the injected helper function must encapsulate that logic.

javascript
Code copied!
  // Server-side definition in createPlugin
  client: {
    helpers: {
      // This factory runs on the SERVER
      trackEvent: (context) => {
        // Access server-side config
        const apiKey = context.config.apiKey;
  
        // Return the function that runs on the CLIENT
        return (eventName) => {
          // Now we can safely use browser APIs
          window.navigator.sendBeacon('https://api.analytics.com', JSON.stringify({
            key: apiKey,
            event: eventName
          }));
        };
      }
    }
  }

Managing Dependencies with Imports #

If your plugin relies on an external library (like a charting library or an analytics SDK), you define it in client.imports.

Coralite handles bundling these external libraries—whether they are remote ESM URLs, local JS/JSON files, or sibling components. The imported modules are rigorously validated and then surfaced in the browser post-load, making them accessible via the context.imports object passed to your helper factories.


Ready to build your plugin? Consult the strict API signatures and configuration options in the createPlugin API Reference.

Start Building with Coralite!

Use the scaffolding script to get jump started into your next project with Coralite