HTML Bundle
html-bundle is a primarily zero-config bundler designed for using HTML as Single File Components (SFCs), allowing inline `<style>` and `<script>` elements. It processes HTML, CSS, and TypeScript/JavaScript files from a source directory to a build directory, leveraging tools like ESBuild for bundling and minification, PostCSS for CSS processing, and html-minifier-terser for HTML. The package, currently at version 6.2.3, focuses on developer experience with features like automatic package installation, Hot Module Replacement (HMR) powered by Server-Sent Events and hydro-js, and critical CSS extraction via `beasties`. Its key differentiators include its HTML-first approach to componentization and its integrated tooling for a streamlined development workflow. The project appears actively maintained, though a clear release cadence isn't specified in the provided excerpt.
Common errors
-
Error: Missing certificate file 'localhost.pem' or 'localhost-key.pem'
cause Attempting to run `html-bundle --secure` without the required TLS certificate and key files in the project root.fixGenerate `localhost.pem` and `localhost-key.pem` using `mkcert` (`mkcert -install && mkcert localhost`) and place them in your project's root directory. -
TypeError: 'createElement' is not a function or 'h' is not defined
cause JSX syntax is used in TypeScript files or inline scripts, but the TypeScript compiler or runtime is not configured to handle it correctly, often missing the `jsxFactory` setting.fixAdd `"compilerOptions": { "jsxFactory": "h" }` to your `tsconfig.json` to inform TypeScript how to transpile JSX into function calls. -
ERR_REQUIRE_ESM: require() of ES Module html-bundle/dist/index.js from ... not supported.
cause Attempting to `require('html-bundle')` in a CommonJS context when `html-bundle`'s main entry point is an ES Module.fixRefactor your consuming code to use ES Module `import` syntax. Ensure your environment supports ESM, or use dynamic `import('html-bundle')` if you must remain in a CommonJS file. -
Error: ELOOP: too many symbolic links, stat 'src'
cause Misconfiguration of `src` or `build` paths in `bundle.config.js` or via CLI, potentially creating a recursive loop where the source directory includes the build directory, or vice-versa, leading to an infinite file globbing/processing loop.fixVerify that your `src` and `build` directories are distinct and do not contain each other. For example, if `src` is `./src`, then `build` should not be `./src/build`.
Warnings
- gotcha When using the `--hmr` flag, `html-bundle` generates a development build. This mode is best suited for local development and might not function optimally if triggered from the main `index.html` due to specific HMR injection mechanisms.
- gotcha Enabling secure HTTP2 over HTTPS with the `--secure` flag requires `localhost.pem` and `localhost-key.pem` files to be present in the project's root folder. Without these, the server will fail to start in secure mode.
- gotcha Configuration options provided via CLI flags will always override equivalent settings defined in a `bundle.config.js` file. Be aware of this precedence when debugging unexpected build behaviors.
- gotcha For TypeScript projects using JSX directly in `<script type="module">` tags or imported components, you must configure `"jsxFactory": "h"` in your `tsconfig.json` to correctly transpile JSX syntax, especially when integrating with libraries like `hydro-js`.
- breaking Version 6.x of `html-bundle` is primarily designed for ES Modules (ESM) in its programmatic API (e.g., `import router from 'html-bundle';`). Attempting to use CommonJS `require()` syntax for the main programmatic exports will result in errors.
Install
-
npm install html-bundle -
yarn add html-bundle -
pnpm add html-bundle
Imports
- router
const router = require('html-bundle');import router from 'html-bundle';
- Config
import type { Config } from 'html-bundle'; - cli
node node_modules/html-bundle/dist/cli.js --flag
html-bundle --flag
Quickstart
{
"name": "my-html-project",
"version": "1.0.0",
"scripts": {
"build": "html-bundle",
"dev": "html-bundle --hmr --port 3000"
},
"devDependencies": {
"html-bundle": "^6.0.0"
}
}
// src/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My HTML SFC</title>
<style>
body { font-family: sans-serif; background-color: #f0f0f0; }
h1 { color: #333; }
</style>
</head>
<body>
<main id="app">
<h1>Hello, HTML Bundle!</h1>
<script type="module">
import { reactive } from 'hydro-js'; // Optional: for reactivity
const message = reactive('World');
document.querySelector('h1').textContent = `Hello, ${message}!`;
console.log('App loaded and script executed.');
</script>
</main>
</body>
</html>
# Terminal commands to run the project
npm install
npm run build
npm run dev