6.1.2 • Published 2 months ago

html-bundle v6.1.2

Weekly downloads
160
License
MIT
Repository
github
Last release
2 months ago

html-bundle

A (primarily) zero-config bundler for HTML files. The idea is to use HTML as Single File Components, because HTML can already include <style> and <script> elements.

Features

  • 🦾 TypeScript (reference it as .js or write inline TS)
  • 📦 Automatic Package Installation
  • 💨 HMR and automatic reconnect
  • ESBuild
  • 🦔 Critical CSS
  • 🚋 Watcher on PostCSS and Tailwind CSS and TS Config
  • 🛡️ Almost no need to restart

Demo

Demo

Installation and Usage

$ npm install -D html-bundle

Add an entry to script in package.json (see flags below).

{
  "scripts": {
    "build": "html-bundle"
  }
}

Add a postcss.config.js file and run the build command. If you do not create this config file, a minimal in-memory config file will be created with cssnano as plugin.

$ npm run build

CLI

--hmr: boots up a static server and enables Hot Module Replacement. This generates a development build and works best when not triggered from the main index.html --secure: creates a secure HTTP2 over HTTPS instance. This requires the files localhost.pem and localhost-key.pem in the root folder. You can generate them with mkcert for instance. --isCritical: uses critical to extract and inline critical-path CSS to HTML. --handler: path to your custom handler. Here, you can handle all non-supported files. You can get the filename via process.argv[2].

Optional Config

The CLI flags can also be set by the config. Flags set by the CLI will override the config. Generate the config in the root and call it "bundle.config.js"

src: input path. Default to "src" build: output path. Defaults to "build" port: For the HMR Server. Defaults to 5000 deletePrev: Whether to delelte the build folder. Defaults to true esbuild: Your additional config html-minifier-terser: Your additional config critical: Your additional config

Example:

/** @type {import('html-bundle').Config} */
export default {
  secure: true,
  handler: "utils/staticFiles.js",
  esbuild: {
    external: ["images"],
  },
};

Concept

The bundler always globs all HTML, CSS and TS/JS files from the src (config) directory and processes them to the build (config) directory. PostCSS is being used for CSS files and inline styles, html-minifier-terser for HTML and esbuild to bundle, minify, etc. for inline and referenced TS/JS. Server-sent events and hydro-js are used for HMR. In order to trigger SPA Routers, the popstate event is being triggered after HMR Operations.

Example hydro-js

Get the idea from hydro-starter. Set "jsxFactory": "h" in tsconfig.json for JSX.

Input

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Example</title>
    <meta name="Description" content="Example for html-bundle" />
    <script type="module">
      import { render, h, reactive } from "hydro-js";

      function Example({ name }) {
        return <main id="app">Hi {name}</main>;
      }

      const name = reactive("Tester");
      render(<Example name={name} />, "#app");
    </script>
    <style>
      body {
        background-color: whitesmoke;
      }
    </style>
  </head>
  <body>
    <main id="app"></main>
  </body>
</html>

Example Vue.js

Set "jsxFactory": "h" in tsconfig.json.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue Example</title>
  </head>
  <script type="module">
    import { createApp, h } from "vue";

    const App = {
      data() {
        return {
          name: "Fabian",
        };
      },
      render() {
        return <p>{this.name}</p>;
      },
    };

    createApp(App).mount("#app");
  </script>
  <body>
    <div id="app"></div>
  </body>
</html>

Example React

Set "jsxFactory": "React.createElement" in tsconfig.json.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React Example</title>
  </head>
  <script type="module">
    import React, { useState } from "react";
    import { createRoot } from "react-dom/client";

    function Example() {
      const [count, setCount] = useState(0);

      return (
        <div>
          <p>You clicked {count} times</p>
          <button onClick={() => setCount(count + 1)}>Click me</button>
        </div>
      );
    }

    createRoot(document.getElementById("app")).render(<Example />);
  </script>
  <body>
    <div id="app"></div>
  </body>
</html>
6.1.2

2 months ago

6.1.0

3 months ago

6.1.1

3 months ago

6.0.22

3 months ago

6.0.21

4 months ago

6.0.20

4 months ago

6.0.19

4 months ago

6.0.18

12 months ago

6.0.17

12 months ago

6.0.16

1 year ago

6.0.15

1 year ago

6.0.9

2 years ago

6.0.14

2 years ago

6.0.13

2 years ago

6.0.12

2 years ago

6.0.11

2 years ago

6.0.10

2 years ago

6.0.8

2 years ago

5.5.1

2 years ago

5.5.0

2 years ago

6.0.1-beta

2 years ago

6.0.0-beta

2 years ago

6.0.7

2 years ago

6.0.6

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

6.0.5

2 years ago

6.0.4

2 years ago

5.5.2

2 years ago

5.4.18

3 years ago

5.4.17

3 years ago

5.4.16

3 years ago

5.4.15

3 years ago

5.4.14

3 years ago

5.4.12

3 years ago

5.4.13

3 years ago

5.4.10

3 years ago

5.4.11

3 years ago

5.4.9

3 years ago

5.4.8

3 years ago

5.4.7

3 years ago

5.4.6

3 years ago

5.4.5

3 years ago

5.4.4

3 years ago

5.4.3

3 years ago

5.4.2

3 years ago

5.4.1

3 years ago

5.4.0

3 years ago

5.3.8

3 years ago

5.3.7

3 years ago

5.3.6

3 years ago

5.3.5

3 years ago

5.3.4

3 years ago

5.3.3

3 years ago

5.3.2

3 years ago

5.3.1

3 years ago

5.2.4

3 years ago

5.2.3

3 years ago

5.3.0

3 years ago

5.2.2

3 years ago

5.2.1

3 years ago

5.2.0

3 years ago

5.1.1

3 years ago

5.1.0

3 years ago

5.0.0

3 years ago

4.0.1

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.0

3 years ago

3.0.0

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago