npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eleventy-plugin-static-jsx

v1.0.8

Published

JSX as an Eleventy template format using static-jsx.

Downloads

6

Readme

eleventy-plugin-static-jsx

This is a plugin for Eleventy to add support for JSX and MDX as template formats, using static-jsx.

Installation

Install using NPM (or any similar tool, such as Yarn):

npm install eleventy-plugin-static-jsx

This requires static-jsx and Eleventy as peer dependencies. Since this requires Eleventy's support for custom file extension handlers, part of the 1.0 release, the beta 1.0 version of Eleventy must be used for now (until 1.0 is actually released):

npm install static-jsx @11ty/eleventy@beta

Then, add the plugin in your Eleventy configuration (.eleventy.js):

const jsx = require("eleventy-plugin-static-jsx");

module.exports = function (eleventyConfig) {
  eleventyConfig.addPlugin(jsx);
};

Usage

MDX

.mdx template files are supported. Any YAML frontmatter is treated as template data, and any Eleventy data (including an additional children member containing any content as RawHtml) is passed as the props object.

Here's an example of a simple MDX template:

---
layout: layouts/post.jsx
title: Simple post
---

import Chart from "../_includes/Chart.jsx";

This is a simple post with the title "{props.title}". Here is a chart showing
something:

<Chart />

JSX

Any .jsx template file must export at least a render function as the default export, which returns RawHtml (the return type of any JSX expression under static-jsx). A data object is passed to render containing all the Eleventy data corresponding to the template, and JavaScript template functions are accessible on this within the render function (similar to the built-in .11ty.js template format). In addition to the usual data, a children member is passed in the data option containing any content passed to the template wrapped in RawHtml so it behaves as children normally would in a JSX component.

Note: when writing a JSX template, use the children prop for child content (as in standard React convention) rather than Eleventy's content to avoid double escaping the output.

Here's an example of a simple JSX template using just a render function which serves as a top-level layout:

import { RawHtml } from "static-jsx";

export default function render({ children, title, today }) {
  return (
    <>
      {/* JSX doesn't support doctypes natively, so we have to use RawHtml */}
      {new RawHtml("<!DOCTYPE html>")}
      <html lang="en">
        <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
        </head>
        <body>
          <main>{children}</main>
          {/* formatCopyrightDate is a JavaScript template function */}
          <footer>
            Copyright {this.formatCopyrightDate(today)} Ian Johnson
          </footer>
        </body>
      </html>
    </>
  );
}

A JSX template may also export data, which may be a simple object or a (possibly async) function returning an object, to provide front matter data for the template. The following example uses this to create a specialized template for a blog post which in turn uses another template as a layout:

export const data = {
  layout: "layouts/main.jsx",
};

export default function render({ children, title }) {
  return (
    <>
      <header>
        <h1>{title}</h1>
      </header>

      <article>{children}</article>
    </>
  );
}

Caveats

Due to Node.js's current lack of (non-experimental) custom module loaders (which would be necessary for importing JSX and MDX files), exports and imports are internally transformed into something that allows a custom import function to be substituted. This internal transformation is missing several cases of import and export syntax (see the tests in recma-module-shims.test.mjs): in particular, dynamic imports (await import) are not transformed, so it is not possible to dynamically import other JSX and MDX files.

This is not technically impossible to fix, but I will most likely wait until custom module loaders are more stable/usable and release a new major version which uses that (cleaner) approach.

License

This is free software, released under the Zero Clause BSD License, as found in the LICENSE file of this repository. This license places no restrictions on your use, modification, or redistribution of the library: providing attribution is appreciated, but not required.