Components
Cook components are reusable HTML templates stored in src/components/. They expand at build time — zero JavaScript runtime cost.
Creating a component
Create an HTML file in src/components/:
<article class="product-card"> <h3>${name}</h3> <p>${description}</p> ${slot}</article>
Using a component
Use the filename as a custom element tag. Attributes become template variables:
<product-card name="Trail Runner Pack" description="32L ultralight backpack"> <a href="/products/trail-runner-pack/">View Details</a></product-card>
The output replaces the custom element with the template, resolving all variables:
Slots
Default slot — ${slot} receives all child content:
<div class="box">${slot}</div>
Named slots — ${slot:name} receives children with slot="name":
<section> <header>${slot:header}</header> <main>${slot}</main></section>
<page-section> <h2 slot="header">Features</h2> <p>Default slot content here.</p></page-section>
Component styles
Components can include <style> blocks. Styles are extracted, deduplicated per component name, and consolidated into a single <style> in <head>:
<style> @scope (.alert) { :scope { padding: 1rem; border-radius: 0.25rem; } .alert-error { background: #fee; border: 1px solid #c00; } }</style><div class="alert alert-${type}"> ${slot}</div>
Use native @scope and @layer for CSS isolation — no framework-specific scoping needed.
Semantic HTML pattern
For semantic markup, use data-component on any element:
<header data-component="site-header" data-title="Welcome"></header>
This looks up src/components/site-header.html and passes data-* attributes as template variables.
DSD Components
Components can use Declarative Shadow DOM for true encapsulation. Include <template shadowrootmode="open"> in the component file:
<template shadowrootmode="open"> <style> :host { display: block; } .card { border: 1px solid var(--border, #ccc); padding: 1rem; } </style> <div class="card"> <h3>${title}</h3> <slot></slot> </div></template>
DSD components preserve the custom element in the output and use native <slot> for content projection. See Declarative Shadow DOM for details.
File resolution
Cook looks for component templates in two locations:
src/components/card.html(direct file)src/components/card/index.html(directory with index)
Configuration
export default { components: { path: 'components', // Directory in src/ prefix: 'site-', // Strip prefix: site-card uses card.html mapping: { // Explicit mappings 'my-widget': 'special-widget.html', }, },};