Skip to content

Latest commit

 

History

History

webcomponents

README.md

Web Components + esbuild Example

This example demonstrates how to integrate a TypeScript Web Component bundle into a Datapages project using esbuild through a custom watcher, so edits to .ts, .templ, and .go all hot-reload through datapages watch.

The example is a minimal "flash sale" landing page with 2 Web Components:

wcexample.mp4

How It Works

The datapages.yaml defines a custom watcher that runs esbuild whenever any TypeScript source in js/src/ changes:

custom-watchers:
  - name: "Bundle web components (esbuild)"
    include:
      - "js/src/**/*.ts"
    cmd: "cd js && node build.mjs"
    fail-on-error: true
    requires: reload

js/build.mjs bundles js/src/index.ts into app/static/bundle.js. The page references it via assets.Path("bundle.js") so the URL is consistent in dev and production.

In dev mode, static files are served directly from ./app/static/ on disk, so a fresh bundle is picked up on browser reload without needing to rebuild and restart the server. In production, app/static/ is embedded into the binary.

Prerequisites

  • Go (see go.mod for the required version)
  • Node.js (for esbuild and Lit)
# Install JS dependencies (esbuild, TypeScript, Lit).
make npm-install

Run in Dev Mode

make dev

The Datapages watcher proxy starts at http://localhost:7331. The app itself listens on http://localhost:8080. To change that, set HOST and PORT in the shell that runs make dev, and watch.app-host in datapages.yaml to match.

Edit any file under js/src/ — esbuild rebuilds the bundle and the browser reloads.