micro-web: A 6KB Zero-Dependency Vanilla JS UI & CRUD Library

2026-12-05来源 dev.toEnglish

Sometimes React or Vue is more machinery than a project needs — internal admin dashboards, server-rendered pages, browser extensions, or quick prototypes — yet writing raw document.createElement and manual form extraction gets repetitive fast.

I built micro-web (mw.js) (~6.0 KB min+gzip, zero runtime dependencies) to give Vanilla JS a clean structure without a build step or virtual DOM.

What's in the box?

Four small modules designed to work together:

  1. mw.element — Create and patch HTML/SVG elements (create, h, patch) with built-in XSS guards.
  2. mw.event — Lightweight Pub/Sub (on, once, off, emit) with wildcard patterns (data:*, *) and automatic cleanup tied to DOM node ownership.
  3. mw.component — Reusable component factories with explicit onMount/onDestroy lifecycle hooks and batched list rendering (render).
  4. mw.data — Unified async CRUD (select, insert, update, delete, clear) backed by localStorage (with in-memory fallback), swappable per-key adapters (use), request hooks (intercept), and one-line form binding (data.bind).

Quick Example (Single <script>, No Build Step)

<script src="https://cdn.jsdelivr.net/gh/pariad84/micro-web@v0.2.0/mw.min.js"></script> <script> const { h } = mw.element; mw.component.define('todo-item', ({ item }) => h('li', h('span', item.data.title), h('button', { event: { click: () => mw.data.delete({ key: 'todos', id: item.id }) } }, 'Delete') ) ); const list = h('ul'); mw.component.mount(list, document.body); async function refresh() { const rows = await mw.data.select({ key: 'todos', query: { sort: '-id' } }); mw.component.render(list, rows, 'todo-item'); } mw.event.on({ name: 'data:todos', handler: refresh, owner: list }); mw.data.insert({ key: 'todos', data: { title: 'Try micro-web' } }); </script> 

Prototype with localStorage, Switch to REST Per Key

One pattern I wanted to make painless is starting with localStorage and swapping in a backend API one resource at a time:

// Log or attach auth headers across all CRUD calls mw.data.intercept(async (ctx, next) => { const start = Date.now(); const result = await next(); console.log(`[${ctx.action}] ${ctx.key} (${Date.now() - start}ms)`); return result; }); // Replace only 'users' with a REST adapter (omitted methods keep defaults) mw.data.use('users', { async select({ id }) { const res = await fetch(id != null ? `/api/users/${id}` : '/api/users'); return res.json(); } }); 

Form Binding Without Boilerplate

mw.data.bind.set(form, { title: 'Write docs', done: true }); const values = mw.data.bind.get(form); // { title: 'Write docs', done: true } mw.data.bind.reset(form); 

Nested [data-mw-component] inputs are automatically skipped so parent forms never accidentally grab child component fields.

If you're working on a lightweight app where a full framework feels heavy, give the live demo or GitHub repo a look — feedback and PRs are very welcome!

原文:https://www.a8q9.cn/en/article/micro-web-a-6kb-zero-dependency-vanilla-js-ui-crud-library-9cda0e.html

相关文章