Detalhes do pacote

jsx-async-runtime

jeasx1.8kMIT2.0.1

An asynchronous JSX runtime without dependencies to be used as html template engine.

jsx, runtime, template, engine

readme (leia-me)

jsx-async-runtime

An asynchronous JSX runtime without dependencies to be used as html template engine for server or browser.

Breaking change

With jsx-async-runtime >= v2.x.x HTML entities are escaped per default. Read more...

Introduction

This runtime was initially developed for Jeasx, but has a value of its own. Its main focus is to keep things simple, reliable, secure and fast.

You can find more information about using this runtime as template engine in the Jeasx documentation.

Installation

npm i jsx-async-runtime

To make use of the jsx-async-runtime, you need to configure your transpiler to utilize this package for transforming the JSX syntax. If you are using TypeScript or esbuild for transpiling your code base, simply add the following options in your tsconfig.json:

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "jsx-async-runtime"
  }
}

The example project provides a complete tsconfig.json with all required options for a proper project setup.

Now you can create a simple test file (e.g. src/HelloWorld.jsx) and execute it via npx tsx src/HelloWorld.jsx:

import { jsxToString } from "jsx-async-runtime";

export default function HelloWorld({ greeting }) {
  return (
    <>
      {{ html: `<!DOCTYPE html>` }}
      <html>
        <head>
          <meta charset="utf-8" />
          <title>{greeting}</title>
        </head>
        <body>
          <h1>{greeting}</h1>
        </body>
      </html>
    </>
  );
}

// Use jsxToString#call with {} to create a 'this' context
console.log(await jsxToString.call({}, <HelloWorld greeting="Hello World" />));

Usage

If you're using jsx-async-runtime as template engine, you might want to include data from an asynchronous operation in the resulting markup. To simplify this process, you can make your components asynchronous and send async requests from within them.

export default async function Todos() {
  const { todos } = await (await fetch("https://dummyjson.com/todos")).json();

  return (
    <ul>
      {todos.map(({ todo, completed }) => (
        <li>
          {todo} ({completed ? "yes" : "no"})
        </li>
      ))}
    </ul>
  );
}

jsx-async-runtime >= v2.x.x escapes all HTML entities for texts per default to prevent cross site scripting. If you want or need to opt out this security feature to include literal HTML snippets in your template (e.g. WYSIWYG content from a CMS), you can provide an object with a single key called html containing the code snippet as a string in your JSX template:

<div>{{ html: "<p>Some <b>HTML</b> from a CMS</p>"}}</div>

If you want to disable the automatic escaping of HTML completely to restore the behaviour of jsx-async-runtime < v2.x.x, you can turn off text escaping with a compatibilty switch via the this context:

export default function () {
  this.jsxEscapeHTML = false;
  return <div>{"<p><b>HTML</b> from a trusted CMS</p>"}</div>
}

If you opt out of automatic escaping, you can use a built-in utility function to escape markup from untrusted external sources:

import { escapeEntities } from "jsx-async-runtime";

export default function () {
  this.jsxEscapeHTML = false;
  return <div>{escapeEntities("<p><b>HTML</b> from user input</p>")}</div>
}

Example project

You can study the example project to learn more about all existing features. Here is a shortened version to give you the idea:

import { jsxToString } from "jsx-async-runtime";

export default function App() {
  return (
    <>
      {{ html: `<!DOCTYPE html>`}}
      <html>
        <head>
          <meta charset="utf-8" />
          <title>Todos</title>
        </head>
        <body>
          <Header label="<Todos>" />
          <TodoList quantity={3} />
        </body>
      </html>
    </>
  );
}

function Header({ label }) {
  return (
    <section
      style={{
        "background-color": "red",
        "padding-bottom": "1rem",
      }}
    >
      <h1 style="color: white; text-align: center">{label}</h1>
    </section>
  );
}

async function TodoList({ quantity }) {
  const { todos } = await (await fetch("https://dummyjson.com/todos")).json();

  return (
    <table class="table">
      <thead class={{ thead: true, striped: false, sticky: true }}>
        <tr>
          <th>Label</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody class="tbody striped">
        {todos.slice(0, quantity).map(({ todo, completed }) => (
          <tr>
            <td>
              <label for="todo">{todo}</label>
            </td>
            <td>
              <label for="status">{completed ? "yes" : "no"}</label>
            </td>
          </tr>
        ))}
      </tbody>
    </table>
  );
}

// Use jsxToString#call with {} to create a 'this' context
console.log(await jsxToString.call({}, <App />));

changelog (log de mudanças)

Changelog

Version Date Release Notes
2.0.1 2025/11/10 updated tsconfig.json (moduleResolution=bundler); esbuild@0.27.0
2.0.0 2025/10/12 Breaking change: escape all HTML entities per default. Opt out of escaping with { html: "<b>Text</b>" } in JSX or via global context configuration with this.jsxEscapeHTML=false. Use faster & more robust escapeHTML from fast-escape-html internally and for exported utility function escapeEntities.
1.0.4 2025/08/13 dep. update to typescript@5.9.2 => proper fix for typing error (previously surpressed with ts-expect-error). esbuild@0.25.9
1.0.3 2025/07/09 added "selectedcontent" element to jsx.types. esbuild@0.25.6
1.0.2 2025/05/28 esbuild@0.25.5
1.0.1 2025/05/03 Consistent naming of internal variables. Replaced nodemon with esbuild watcher. esbuild@0.25.3. typescript@5.8.3
1.0.0 2025/03/15 Added missing return types. esbuild@0.25.1, typescript@5.8.2
0.8.1 2025/02/28 Use interfaces instead of types for attribute & elements typings, so types can be extended in userland
0.8.0 2025/02/26 consistent use of types instead of interfaces. added typings for svg elements & attributes
0.7.1 2025/01/17 feat: build class attribute from array of strings
0.7.0 2025/01/13 Improved HTML typings. Multiple rel attributes; allow numbers in object styles. Use (string & {}) to allow arbitrary attribute values while preserving code completions. Removed any-types for SVG & MathML
0.6.2 2024/12/03 Updated HTML typings with MDN; added deprecated tags & attributes.
0.6.1 2024/12/01 fix(escapeEntities): don't escape existing & sequence
0.6.0 2024/11/23 removed deprecated alias "renderToString"
0.5.2 2024/10/04 dep. updates
0.5.1 2024/09/18 deps updated
0.5.0 2024/07/25 Use "this" to provide a custom jsxToString function which allows to modify/replace existing JSX components, implement helmet like head and more. Removed Object.freeze for children / props to prepare component interception.
0.4.2 2024/07/12 Ship non-minified version
0.4.1 2024/07/11 Fixed: rendering in Vitest results in undefined
0.4.0 2024/07/10 Added option to bind "this" as context to avoid prop drilling
0.3.0 2024/07/05 deprecated unnecessary renderToString() wrapper in favor of jsxToString()
0.2.2 2024/07/01 esbuild@0.22.0, nodemon@3.1.4, typescript@5.5.2
0.2.1 2024/04/24 fix: empty class object should omit rendering of class attribute
0.2.0 2024/04/22 change: removed indent option from renderToString. No paddings / newlines are created anymore by renderer to improve consistency with tools like prettier.
0.1.8 2024/03/23 typescript@5.4.3
0.1.7 2024/03/15 esbuild@0.20.2
0.1.6 2024/02/28 build:esm with platform=neutral
0.1.5 2024/02/26 nodemon@3.1.0
0.1.4 2024/01/03 dep. updates
0.1.3 2024/01/03 Organize import in jsx-runtime;added more tests.
0.1.2 2023/12/28 default indent = 0
0.1.1 2023/12/28 Fixed bug / revert: boolean elements must be rendered as empty string, otherwise conditionals will render 'false'.
0.1.0 2023/12/28 Some performance optimizations for jsx-to-string; using <> doesn't increase indent level anymore.
0.0.3 2023/12/24 Provide utility function to escape entities.
0.0.2 2023/12/23 Removed render-error; cleanups & refactorings; updated docs
0.0.1 2023/12/22 Initial release