acid

package module
v0.0.0-...-3b37cd2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 15 Imported by: 0

README

acid

Acid is a library for doing #NoBuild frontends in Go projects.

Go Report Card

Features

  • #NoBuild strategy for frontend assets
  • Leans heavily into embed.FS: all your asset files are compiled into your binary so you only have to deploy a single file.
  • Generates importmaps for your JS dependencies. currently only supporting JsDelivr as a CDN
  • Automatically generates and maps digests for busting caches.

In my setup I use the tailwindcss binary to prerender some CSS because I'm rather bad at CSS. You don't have to do that. You can add your vanilla css and it will be picked up.

Usage

To install acid, run:

go get github.com/jeffrydegrande/acid

Include acid in your project. Here's an example of how I'm using it right now:

package assets

import (
	"embed"
	"github.com/jeffrydegrande/acid"
)

// Include tailwindcss, this will end up in the Assets FS
//go:generate tailwindcss build -o ./static/css/tailwind.css --minify

//go:embed static/*
var Assets embed.FS

func init() {
	acid.UseCDN(acid.JsDelivr)
	acid.Pin("htmx.org", "1.9.10")
	acid.Pin("flatpickr", "4.6.9")
	acid.Pin("@milkdown/core", "7.3.2")
	acid.Pin("@milkdown/ctx", "7.3.2")
	acid.Pin("@milkdown/plugin-listener", "7.3.2")
	acid.Pin("@milkdown/preset-commonmark", "7.3.2")
	acid.PinAllFrom(&Assets)
}

Three interesting things are going on here.

  1. I do use tailwindcss and I use go:generate to produce my output before go:embed pix up the result
  2. embed.FS pulls everything in the static/ directory into something I can later read from memory. This means everything, all my css, all my images, all my js will be compiled into my binary.
  3. I'm leaning on the init function to "load everything". This means I do need to import this module at an approriate time. I do it in the module that sets up my Echo server with "import _ "github.com/jeffrydegrande//assets" in this case.

Configuration

TODO:

License

acid is made available under the MIT License. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSS

func CSS(path string) string

func CalculateDigests

func CalculateDigests(fsys fs.FS, path string) error

func FS

func FS() http.FileSystem

func Image

func Image(path string) string

func ImportMap

func ImportMap() templ.Component

func Javascript

func Javascript(path string) string

func Pin

func Pin(name string, version string)

func PinAllFrom

func PinAllFrom(fs *embed.FS)

func UseCDN

func UseCDN(cdn CDN)

Types

type CDN

type CDN int
const (
	JsDelivr CDN = iota
)

type DigestFS

type DigestFS struct {
	FS         fs.FS
	Map        map[string]string
	ReverseMap map[string]string
}

func (*DigestFS) FindFile

func (c *DigestFS) FindFile(name string) string

func (*DigestFS) Open

func (c *DigestFS) Open(name string) (fs.File, error)

type Package

type Package struct {
	Name string `json:"name,omitempty"`
	URL  string `json:"url,omitempty"`
}

func Packages

func Packages() []Package

type Structure

type Structure struct {
	Imports map[string]string `json:"imports,omitempty"`
}

Directories

Path Synopsis
views
templ: version: 0.2.476
templ: version: 0.2.476

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL