html-compose
v1.0.1
Published
Static HTML builder with component-style includes, props and watch mode.
Maintainers
Readme
html-compose
Static HTML builder with component-style includes, props and watch mode.
Build modular HTML pages using reusable components without a framework.
You can split HTML into components and compose pages with:
include()for componentspropsfor passing dataiff()for conditional rendering- watch mode for automatic rebuilding
Installation
Install in your project:
npm install html-composeUsage
Build a directory:
html-compose src distWatch mode:
html-compose src dist --watchExample package.json scripts:
{
"scripts": {
"build:html": "html-compose src dist",
"watch:html": "html-compose src dist --watch"
}
}Run with:
npm run watch:htmlExample Project Structure
src/
index.html
about.html
_components/
header.html
footer.html
card.htmlFiles and folders starting with _ are treated as private templates.
They are not rendered as standalone pages, but can be included inside other HTML files.
Include Components
${ await include('./_components/header.html') }Components can be nested and reused across multiple pages.
Passing Props
Props can be passed into components.
Page
${ await include('./_components/card.html', {
title: 'Hello',
text: 'Welcome to the site'
}) }Component
<div class="card">
<h2>${ props.title }</h2>
<p>${ props.text }</p>
</div>Conditional Rendering
Use iff() for conditional rendering.
${ iff(props.loggedIn, `
<p>Welcome back!</p>
`) }With an else case:
${ iff(props.loggedIn,
`<p>Welcome!</p>`,
`<p>Please log in</p>`
) }Watch Mode
Watch mode automatically rebuilds when HTML files change.
html-compose src dist --watchExample output:
[html-compose] built index.html
[html-compose] built about.html
[html-compose] watch modePrivate Templates
Any file or directory starting with _ is not rendered as a page.
Example:
src/
index.html
_components/
header.html
footer.htmlOutput:
dist/
index.htmlBut components can still be used via include().
Example Page
<body>
${ await include('./_components/header.html', {
title: "My Site"
}) }
<main>
<h1>Hello</h1>
</main>
${ await include('./_components/footer.html') }
</body>How It Works
The build pipeline:
HTML files
↓
include()
↓
props
↓
iff()
↓
render
↓
dist outputTemplates are rendered using a JavaScript-based template engine internally.
Roadmap
Possible future features:
each()loops- dependency graph for faster watch rebuilds
- HTML minification
- layouts
- plugin system
License
MIT
