Trên GitHub, developit/htm đã đạt 9.1k sao, ngôn ngữ JavaScript. Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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 bytehtm/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: <div ...${props}> instead of <div {...props}>
Self-closing tags: <div />
Components: <${Foo}> instead of <Foo>(where Foo is a component reference)
Boolean attributes: <div draggable />
Improvements over JSX
htm actually takes the JSX-style syntax a couple steps further!
Here's some ergonomic features you get for free that aren't present in JSX:
No transpiler necessary
HTML's optional quotes: <div class=foo>
Component end-tags: <${Footer}>footer content<//>
Syntax highlighting and language support via the lit-html VSCode extension and vim-jsx-pretty plugin.
Multiple root element (fragments): <div /><div />
Support for HTML-style comments: <div><!-- comment --></div>
Installation
htm is published to npm, and accessible via the unpkg.com CDN:
via npm:
npm i htm
hotlinking from unpkg:(no build tool needed!)
import htm from 'https://unpkg.com/htm?module'
const html = htm.bind(React.createElement);
// 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.
import { render } from 'preact';
import { html } from 'htm/preact';
render(html`<a href="/">Hello!</a>`, document.body);
Similarly, for React:
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:
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:
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.
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.
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.
developit/htm có 9.1k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/developit/htm. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
developit/htm có phải mã nguồn mở không?
Có — developit/htm phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/developit/htm.
developit/htm còn đang phát triển không?
Commit gần nhất trên developit/htm là 2.8 năm trước (theo timestamp GitHub). Repo có 181 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
developit/htm là gì?
developit/htm (developit/htm) là dự án JavaScript trên GitHub. Theo mô tả gốc: Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Đọc thêm về developit/htm ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/developit/htm là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về htm?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về htm.