tailwind

package module
v0.0.0-...-96fb28b Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: MIT Imports: 5 Imported by: 0

README

Tailwind.go

Use Tailwind in Go.

Unlike other options, this package embeds tailwind directly and doesn't spawn a subprocess, so it's faster.

Example Usage

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/livebud/js"
	v8 "github.com/livebud/js/v8"
	"github.com/livebud/tailwind"
)

func main() {
	// Initialize a V8 isolate
	vm, _ := v8.Load(&js.Console{
		Log:   os.Stdout,
		Error: os.Stderr,
	})
	defer vm.Close()
	// Generate the CSS for this index.html file
	processor := tailwind.New(vm)
	ctx := context.Background()
	css, _ := processor.Process(ctx, "index.html", `
	  <html>
	    <head>
	      <link rel="stylesheet" href="index.min.css" />
	    </head>
	    <body>
	      <h1 class="bg-[url(/img/grid.svg)] text-sky-500 hover:text-sky-600">Hello Tailwind!</h1>
	    </body>
	  </html>
	`)
	fmt.Println(css)
}

This results in a CSS file:

/*! tailwindcss v3.2.4 | MIT License | https://tailwindcss.com*/*,:after,:before{border:0 solid #e5e7eb;box-sizing:border-box}:after,:before{--tw-content:""}html{-webkit-text-size-adjust:100%;font-feature-settings:normal;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4}
/* ... */

For production, I recommend building this CSS file and embedding it alongside the processed HTML file.

For development, you may want to run this processor dynamically. Keep in mind that this processor currently depends on CGO.

I originally built this project for Bud, which handles the environment differences for you.

CLI Usage

We've also included a rudimentary CLI for quickly testing. If you already have the official CLI installed, stick with that.

go install github.com/livebud/tailwind/cmd/tailwind@latest
tailwind example/play.html

Development

You can install the dependencies with make install:

make install

This command expects that you have go and npm in your toolchain. After installing the dependencies, you can run tests with:

make test

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

func New

func New(vm js.VM, enablePreflight bool) *Processor

func (*Processor) Process

func (p *Processor) Process(ctx context.Context, name, src string) (css string, err error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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