@arcmantle/vite-plugin-html-module
    Preparing search index...

    @arcmantle/vite-plugin-html-module

    @arcmantle/vite-plugin-html-module

    A Vite plugin that enables importing HTML files as modules, supporting the HTML Modules proposal. Transform HTML files into JavaScript modules that export DOM nodes for use in your applications.

    • 🏗️ HTML Module Support: Import HTML files directly as JavaScript modules
    • 🔍 Element ID Export: Optionally export elements with IDs as named exports
    • 📦 TypeScript Support: Includes TypeScript declarations for seamless integration
    • Vite Integration: Built specifically for Vite with hot reload support
    • 🎯 Standards-Based: Follows the emerging HTML Modules web standard
    npm install @arcmantle/vite-plugin-html-module
    # or
    pnpm add @arcmantle/vite-plugin-html-module
    # or
    yarn add @arcmantle/vite-plugin-html-module

    Add the plugin to your vite.config.ts:

    import { htmlModules } from '@arcmantle/vite-plugin-html-module';
    import { defineConfig } from 'vite';

    export default defineConfig({
    plugins: [
    htmlModules()
    ]
    });

    Include the client types in your tsconfig.json:

    {
    "compilerOptions": {
    "types": ["@arcmantle/vite-plugin-html-module/client"]
    }
    }

    Import HTML files using the with { type: 'html' } syntax:

    import template from './template.html' with { type: 'html' };

    // The default export is a Document object
    console.log(template); // Document

    // Use the document as needed
    document.body.appendChild(template.body.firstElementChild);

    Enable element ID exports in your configuration:

    import { htmlModules } from '@arcmantle/vite-plugin-html-module';

    export default defineConfig({
    plugins: [
    htmlModules({ exportIds: true })
    ]
    });

    Given an HTML file like this:

    <!-- template.html -->
    <div id="container">
    <h1 id="title">Hello World</h1>
    <button id="my-button">Click me</button>
    </div>

    You can import both the document and individual elements:

    import template, { container, title } from './template.html' with { type: 'html' };

    // Access the full document
    console.log(template); // Document

    // Access individual elements by their IDs
    console.log(container); // <div id="container">...</div>
    console.log(title); // <h1 id="title">Hello World</h1>

    // Elements with invalid JavaScript identifiers are exported with quotes
    import template, { 'my-button': myButton } from './template.html' with { type: 'html' };
    console.log(myButton); // <button id="my-button">Click me</button>
    • Type: boolean
    • Default: false
    • Description: When enabled, elements with id attributes are exported as named exports. Elements with valid JavaScript identifiers are exported directly, while others are exported with quoted names.
    htmlModules({
    exportIds: true // Enable ID-based exports
    })
    1. Detection: The plugin detects imports of .html files that use the with { type: 'html' } import assertion
    2. Parsing: HTML content is parsed using parse5 to extract element information
    3. Transformation: The HTML is transformed into a JavaScript module that:
      • Creates a DOMParser instance
      • Parses the HTML string into a Document
      • Exports the document as the default export
      • Optionally exports individual elements by their IDs
    4. Type Safety: TypeScript declarations ensure proper typing for imported HTML modules

    This plugin transforms HTML modules at build time, so the generated code runs in any environment that supports:

    • DOMParser API (all modern browsers)
    • ES modules
    • Node.js >= 22
    • Vite >= 7.0.0
    • parse5: HTML parser for processing HTML files
    • @parse5/tools: Additional tools for HTML parsing and manipulation

    Apache-2.0

    This package is part of the Arcmantle Weave monorepo. Contributions are welcome!


    Note: This plugin implements support for the HTML Modules proposal, which is still evolving. The syntax and behavior may change as the specification develops.