JSX

Create pages and layouts with JSX (React).

Installation

Import this plugin in your _config.ts file to use it:

import lume from "lume/mod.ts";
import jsx from "lume/plugins/jsx.ts";

const site = lume();

site.use(jsx({/* your config here */}));

export default site;

See all available options in Deno Doc.

Configuration

You might want to add the following fields to deno.json and import_map.json in order to tell Deno that Lume exposes React in global scope:

{
  "importMap": "import_map.json",
  "tasks": {
    "lume": "deno eval \"import 'lume/task.ts'\" --",
    "build": "deno task lume",
    "serve": "deno task lume -s"
  },
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react"
  }
}

Go to Using TypeScript for more info about using TypeScript with Lume.

Description

JSX (or the equivalent TSX for TypeScript) is a template language to create and render HTML code, very popular in some frameworks, like React. This plugin adds support for using JSX / TSX to create pages and layouts and using React for rendering.

Creating pages

To create a page with this format, just add a file with .jsx or .tsx extension to your site. This format works exactly the same as JavaScript/TypeScript files, but with the addition of the ability to export JSX code in the default export:

export const title = "Welcome to my page";
export const layout = "layouts/main.njk";

export default (data) => (
  <>
    <h1>{data.title}</h1>
    <p>This is my first post using lume. I hope you like it!</p>
  </>
);

Note that this page uses the layouts/main.njk layout to wrap the content (you can mix different template languages like Nunjucks and JSX)

Creating layouts

To create layouts in JSX, just add .jsx or .tsx files to the _includes directory. Note that we need to use the variable children to render the page content instead of content. The difference is that content is a string and cannot be easily used in JSX because it's escaped, and children is the JSX object un-rendered.

export default ({ title, children }) => (
  <html>
    <head>
      <title>{title}</title>
    </head>
    <body>
      {children}
    </body>
  </html>
);

Lume will automatically add the missing <!DOCTYPE html> to the generated .html pages.