An open API service indexing awesome lists of open source software.

https://github.com/elct9620/esbuild-fs

The esbuild plugin to support Go's http.FileServer
https://github.com/elct9620/esbuild-fs

esbuild-plugin golang

Last synced: 10 months ago
JSON representation

The esbuild plugin to support Go's http.FileServer

Awesome Lists containing this project

README

          

ESBuild FS
===
[![Test](https://github.com/elct9620/esbuild-fs/actions/workflows/test.yml/badge.svg)](https://github.com/elct9620/esbuild-fs/actions/workflows/test.yml)

This plugin creates a simple in-memory Key-Value filesystem to track compiled assets to provide `http.FileSystem` support.

> This is not designed for production, use it when development.

## Usage

Install with `go get`

```bash
go get -u github.com/elct9620/esbuild-fs
```

The `esbuildfs.Serve()` shortcut is created to make assets handler and SSE (Server Sent Event).

```go
assets, sse, err := esbuildfs.Server(
api.BuildOptions{
Outdir: "static/js", // required
// ...
},
esbuildfs.WithPrefix("assets"), // Or `http.StripPrefix`
)

// ...
mux.Handle("/assets/", assets)
mux.Handle("/esbuild", sse)
```

We can use `html/template` to add the Live Reload script.

```html
{{ if .LiveReload }}

new EventSource('/esbuild').addEventListener('change', () => location.reload())

{{ end }}
```