Eleventy Plugin for JSX Bundling

1.2.9 · active · verified Tue Apr 21

eleventy-plugin-smol-jsx-bundler is an Eleventy plugin designed to bring robust bundling capabilities to projects leveraging JSX, React, Preact, or MDX. It currently stands at version 1.2.9 and provides a lightweight, pure JavaScript approach to managing CSS, JavaScript, or HTML bundles within an Eleventy site, conceptually mirroring Eleventy's built-in bundler but with direct support for JSX syntax. This plugin allows developers to embed bundle content directly within JSX or MDX components using functions like `bundle()`, `getBundle()`, and `getBundleFile()`, offering an integrated workflow for modern frontend development within Eleventy. Its key differentiators include enabling easily unit-testable JSX files, greater extensibility, and features like deduplication of bundle content. It also supports post-processing of bundles with external tools such as PostCSS, providing flexibility for complex build pipelines. While it doesn't specify a strict release cadence, its 1.x versioning suggests a stable API and active maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the `eleventy-plugin-smol-jsx-bundler` in `.eleventy.js` and then use `bundle`, `getBundle`, and `getBundleFile` within a JSX template to manage both inline and external CSS/JS assets, including post-processing with PostCSS.

// .eleventy.js
import jsxBundle from 'eleventy-plugin-smol-jsx-bundler';
import postcss from 'postcss';
import postcssNested from 'postcss-nested';

export default async (eleventyConfig) => {
  eleventyConfig.addPlugin(jsxBundle);

  // Example: Add a PostCSS filter to process CSS bundles
  eleventyConfig.addFilter('postcss', async (content) => {
    if (!content) return content;
    const result = await postcss([postcssNested]).process(content, { from: undefined });
    return result.css;
  });
  // Apply this filter to all 'css' bundles
  eleventyConfig.addTransform('css', 'postcss');
};

// src/index.tsx (or .mdx)
import { bundle, getBundle, getBundleFile } from 'eleventy-plugin-smol-jsx-bundler';

// Bundle some CSS specific to this page
bundle('css', `
  .page-header {
    font-family: sans-serif;
    color: #333;

    &__title {
      font-size: 2em;
      text-decoration: underline;
    }
  }
`);

// You can also bundle JavaScript, e.g., for interactivity
bundle('js', `
  console.log("Hello from Smol JSX Bundler!");
  document.addEventListener('DOMContentLoaded', () => {
    const button = document.getElementById('myButton');
    if (button) {
      button.addEventListener('click', () => alert('Button clicked!'));
    }
  });
`);

const MyPage = (props) => {
  return (
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>My Smol JSX Bundler Page</title>
        {/* Render inline CSS from the bundle */}
        <style dangerouslySetInnerHTML={{ __html: getBundle('css') }} />
        {/* Or link to a content-hashed CSS file */}
        <link rel="stylesheet" href={getBundleFile('css', 'css')} />
      </head>
      <body>
        <header className="page-header">
          <h1 className="page-header__title">Welcome to Eleventy with JSX!</h1>
        </header>
        <main>
          {/* Render page content, typically from markdown or other templating */}
          <div dangerouslySetInnerHTML={{ __html: props.content }} />
          <button id="myButton">Click Me</button>
        </main>
        <footer>
          <p>&copy; 2023 My Site</p>
        </footer>
        {/* Render inline JS from the bundle */}
        <script dangerouslySetInnerHTML={{ __html: getBundle('js') }} />
        {/* Or link to a content-hashed JS file */}
        <script src={getBundleFile('js', 'js')}></script>
      </body>
    </html>
  );
};

export const render = (props) => {
  return <MyPage content={props.content} />;
};

view raw JSON →