Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
H

htm

> 编程语言
Open source

Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.

9.0K stars0 likes0 views
WebsiteGitHub

About

Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.

HTM (Hyperscript Tagged Markup)

htm is JSX-like syntax in plain JavaScript - no transpiler necessary.

Develop with React/Preact directly in the browser, then compile htm away for production.

It uses standard JavaScript Tagged Templates and works in all modern browsers.

htm by the numbers:

< 600 bytes when used directly in the browser

⚛️ < 500 bytes when used with Preact (thanks gzip )

< 450 byte htm/mini version

0 bytes if compiled using babel-plugin-htm

Syntax: like JSX but also lit

The syntax you write when using HTM is as close as possible to JSX:

  • Spread props:

Installation

htm is published to npm, and accessible via the unpkg.com CDN:

via npm:

javascript
npm i htm

hotlinking from unpkg: (no build tool needed!)

javascript
import htm from 'https://unpkg.com/htm?module'
const html = htm.bind(React.createElement);
javascript
// just want htm + preact in a single file? there's a highly-optimized version of that:
import { html, render } from 'https://unpkg.com/htm/preact/standalone.module.js'

Usage

If you're using Preact or React, we've included off-the-shelf bindings to make your life easier. They also have the added benefit of sharing a template cache across all modules.

javascript
import { render } from 'preact';
import { html } from 'htm/preact';
render(html`<a href="/">Hello!</a>`, document.body);

Similarly, for React:

javascript
import ReactDOM from 'react-dom';
import { html } from 'htm/react';
ReactDOM.render(html`<a href="/">Hello!</a>`, document.body);

Advanced Usage

Since htm is a generic library, we need to tell it what to "compile" our templates to. You can bind htm to any function of the form h(type, props, ...children) (hyperscript). This function can return anything - htm never looks at the return value.

Here's an example h() function that returns tree nodes:

javascript
function h(type, props, ...children) {
  return { type, props, children };
}

To use our custom h() function, we need to create our own html tag function by binding htm to our h() function:

javascript
import htm from 'htm';

const html = htm.bind(h);

Now we have an html() template tag that can be used to produce objects in the format we created above.

Here's the whole thing for clarity:

javascript
import htm from 'htm';

function h(type, props, ...children) {
  return { type, props, children };
}

const html = htm.bind(h);

console.log( html`<h1 id=hello>Hello world!</h1>` );
// {
//   type: 'h1',
//   props: { id: 'hello' },
//   children: ['Hello world!']
// }

If the template has multiple element at the root level the output is an array of h results:

javascript
console.log(html`
  <h1 id=hello>Hello</h1>
  

`);
// [
//   {
//     type: 'h1',
//     props: { id: 'hello' },
//     children: ['Hello']
//   },
//   {
//     type: 'div',
//     props: { class: 'world' },
//     children: ['world!']
//   }
// ]

Caching

The default build of htm caches template strings, which means that it can return the same Javascript object at multiple points in the tree. If you don't want this behaviour, you have three options:

  • Change your h function to copy nodes when needed.
  • Add the code this[0] = 3; at the beginning of your h function, which disables caching of created elements.
  • Use htm/mini, which disables caching by default.

Example

Curious to see what it all looks like? Here's a working app!

It's a single HTML file, and there's no build or tooling. You can edit it with nano.

…

⚡️ See live version ▶

⚡️ Try this on CodeSandbox ▶

How nifty is that?

Notice there's only one import - here we're using the prebuilt Preact integration since it's easier to import and a bit smaller.

The same example works fine without the prebuilt version, just using two imports:

javascript
import { h, Component, render } from 'preact';
import htm from 'htm';

const html = htm.bind(h);

render(html`<${App} page="All" />`, document.body);

Other Uses

Since htm is designed to meet the same need as JSX, you can use it anywhere you'd use JSX.

Generate HTML using vhtml:

javascript
import htm from 'htm';
import vhtml from 'vhtml';

const html = htm.bind(vhtml);

console.log( html`<h1 id=hello>Hello world!</h1>` );
// '<h1 id="hello">Hello world!</h1>'

Webpack configuration via jsxobj: (details here) (never do this)

javascript
import htm from 'htm';
import jsxobj from 'jsxobj';

const html = htm.bind(jsxobj);

console.log(html`
  <webpack watch mode=production>
    <entry path="src/index.js" />
  </webpack>
`);
// {
//   watch: true,
//   mode: 'production',
//   entry: {
//     path: 'src/index.js'
//   }
// }

Demos & Examples

  • Canadian Holidays: A full app using HTM and Server-Side Rendering
  • HTM SSR Example: Shows how to do SSR with HTM
  • HTM + Preact SSR Demo
  • HTM + vhtml SSR Demo

Project Status

The original goal for htm was to create a wrapper around Preact that felt natural for use untranspiled in the browser. I wanted to use Virtual DOM, but I wanted to eschew build tooling and use ES Modules directly.

This meant giving up JSX, and the closest alternative was Tagged Templates. So, I wrote this library to patch up the differences between the two as much as possible. The technique turns out to be framework-agnostic, so it should work great with any library or renderer that works with JSX.

htm is stable, fast, well-tested and ready for production use.

Issues· 49 open

View all issuesOpen on GitHub
  • #268

    Sarthak

    Updated Oct 31, 2025
  • #239

    just a small note: standalone bundle of htm with preact and preact/signals

    Updated Jun 7, 2025
  • #266

    Tugas

    Updated May 28, 2025
  • #265

    Tugas

    Updated May 28, 2025
  • #264

    type checking templates

    Updated Feb 17, 2025
  • #262

    Missing dependency on preact / react (ghost dependency)

    Updated Feb 4, 2025
  • #229

    Include Preact signal

    Updated Oct 29, 2024
  • #240

    different attribute handling in div/span etc. and svg

    Updated Jul 3, 2024
  • #257

    Can we say htm as internal dsl

    Updated Mar 7, 2024
  • #255

    Add support iterator

    Updated Jan 31, 2024

> Tags

JavaScriptbabelbabel-pluginjsxtagged-template

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言