liveBook · Manning

HTML module

As explained in Web Components in Action

An HTML module is a proposed module type that packages HTML content for import through a module system.

An HTML module is a proposed module type for importing HTML through a module system. It aims to let teams keep component structure separate from JavaScript logic, much as CSS modules separate styling. section 15.1

Until HTML modules become available, a JavaScript module can hold template functions that return HTML and CSS. A Web Component then imports the module and renders the result. section 6.3

The intended benefits are practical:

  • Move markup outside a large component class. section 15.1
  • Let people focus on HTML, CSS, or JavaScript according to their strengths. section 15.1
  • Support smaller templates and modular design-system styles, especially alongside Shadow DOM features. section 15.1

Template module workaround

A template module can export an object with render, html, and css methods. The component imports it and calls Template.render({ first_name: 'Emmett', ... }) inside connectedCallback(), keeping the component class focused on registration and lifecycle behavior. section 6.3

How Web Components in Action covers it

How deep it goes
Defines the proposal and demonstrates a JavaScript substitute, but stops short of specifying HTML-module syntax or browser implementation details.
Examples
Uses a business-card component, a JavaScript template module, parameterized rendering, and separate HTML and CSS methods.
What you can do after
Apply it via JavaScript modules to move templates outside Web Component classes while HTML-module support remains unavailable.
How it connects
Connects HTML modules with Web Components, CSS modules, Shadow DOM, design systems, and JavaScript modules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.