### Command Line Installation for gotailwindcss Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md Instructions to install the `gotailwindcss` command-line tool using `go get`. This command is used for processing CSS files and other build tasks. ```bash go get github.com/gotailwindcss/tailwind/cmd/gotailwindcss ``` -------------------------------- ### Example Tailwind CSS with Directives Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md An example CSS file demonstrating the use of Tailwind CSS directives like `@tailwind` for base, components, and utilities, and `@apply` for composing utility classes into a custom class (`.button`). ```css @tailwind base; @tailwind components; .button { @apply inline-block m-2 p-2 rounded-md bg-green-400; } @tailwind utilities; ``` -------------------------------- ### Bash: GoTailwindCSS Command Line Build Tool Source: https://context7.com/gotailwindcss/tailwind/llms.txt Demonstrates the usage of the GoTailwindCSS command-line interface for building CSS files. Examples cover basic builds, builds with purging enabled (scanning HTML, Vue, JSX, Vugu files), custom file extensions for purging, processing multiple input files, and outputting to standard output. ```bash # Basic build gotailwindcss build -o output.css input.css # Build with purging (scans HTML/Vue/JSX/Vugu files) gotailwindcss build -o output.css --purgescan ./static input.css # Build with custom file extensions for purging gotailwindcss build -o output.css --purgescan ./views --purgeext html,php,twig input.css # Multiple input files gotailwindcss build -o output.css styles/base.css styles/components.css styles/utilities.css # Output to stdout gotailwindcss build input.css ``` -------------------------------- ### Development HTTP Handler Setup in Go Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md Sets up an HTTP server that uses the `twhandler` to process Tailwind CSS directives on the fly. It serves static files and handles CSS requests through the custom handler. Dependencies include `http`, `log`, `twhandler`, and `twembed`. ```go import "net/http" import "log" import "github.com/gotailwindcss/tailwind/twembed" import "github.com/gotailwindcss/tailwind/twhandler" func main() { mux := http.NewServeMux() mux.Handle("/", http.FileServer(http.Dir("static"))) mux.Handle("/css/", twhandler.New(http.Dir("css"), "/css", twembed.New())) s := &http.Server{Addr: ":8182", Handler: mux} log.Fatal(s.ListenAndServe()) } ``` -------------------------------- ### Example Generated Go Purge Key File Source: https://context7.com/gotailwindcss/tailwind/llms.txt An example of a Go source file generated by `gotailwindcss purgescan`. This file contains a map of CSS class names that should not be purged, intended to be used with the `go generate` command. It includes a warning not to edit manually. ```go package main // WARNING: DO NOT EDIT, THIS IS A GENERATED FILE //go:generate gotailwindcss -o purgekeys.go ./templates // PurgeKeyMap is a list of keys which should not be purged from CSS output. var PurgeKeyMap = map[string]struct{}{ "text-blue-500": {}, "bg-green-400": {}, "rounded-md": {}, "p-2": {}, "m-2": {}, } ``` -------------------------------- ### Generate Purge Key Lists (Command Line) Source: https://context7.com/gotailwindcss/tailwind/llms.txt Provides command-line examples for generating lists of used CSS class names. These lists can be in Go source files, JSON, or plain text format, aiding in CSS optimization by identifying classes to keep. The `purgescan` command analyzes specified template directories. ```bash # Generate Go source with purge keys gotailwindcss purgescan -o purgekeys.go --ext html,vue ./templates # Generate JSON list gotailwindcss purgescan -o purgekeys.json ./templates # Generate plain text list gotailwindcss purgescan -o purgekeys.txt ./templates # Output to stdout gotailwindcss purgescan ./templates ``` -------------------------------- ### Processing CSS Files with gotailwindcss Build Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md Example of using the `build` subcommand of `gotailwindcss` to process one or more CSS input files and output the result to a single CSS file (`out.css`). ```bash gotailwindcss build -o out.css in1.css in2.css ``` -------------------------------- ### Go: Minification with Post-Processing Source: https://context7.com/gotailwindcss/tailwind/llms.txt Applies CSS minification as a post-processing step in GoTailwindCSS. This function demonstrates setting up a post-processor that uses the tdewolff/minify library to minify the generated CSS. The example shows minifying a simple CSS rule with Tailwind apply directives. ```go package main import ( "bytes" "io" "strings" "github.com/gotailwindcss/tailwind" "github.com/gotailwindcss/tailwind/twembed" "github.com/tdewolff/minify/v2" "github.com/tdewolff/minify/v2/css" ) func main() { var output bytes.Buffer conv := tailwind.New(&output, twembed.New()) // Set up minification post-processor conv.SetPostProcFunc(func(out io.Writer, in io.Reader) error { m := minify.New() m.AddFunc("text/css", css.Minify) return m.Minify("text/css", out, in) }) conv.AddReader("input.css", strings.NewReader(`.test { @apply font-bold text-2xl; }`), false) if err := conv.Run(); err != nil { panic(err) } // Output: .test{font-weight:700;font-size:1.5rem;line-height:2rem} } ``` -------------------------------- ### Implement Custom Purge Checker (Go) Source: https://context7.com/gotailwindcss/tailwind/llms.txt Shows how to implement a custom purge checker by creating a struct that satisfies the `PurgeChecker` interface. This allows for custom logic to determine which CSS classes to keep or purge, integrating with the Tailwind CSS conversion process. The example defines allowed classes and uses them in the `ShouldPurgeKey` method. ```go package main import ( "bytes" "strings" "github.com/gotailwindcss/tailwind" "github.com/gotailwindcss/tailwind/twembed" ) type CustomPurger struct { allowedClasses map[string]bool } func (cp *CustomPurger) ShouldPurgeKey(k string) bool { // Return true to purge (remove), false to keep return !cp.allowedClasses[k] } func main() { var output bytes.Buffer conv := tailwind.New(&output, twembed.New()) purger := &CustomPurger{ allowedClasses: map[string]bool{ "text-red-500": true, "bg-blue-600": true, "p-4": true, }, } conv.SetPurgeChecker(purger) conv.AddReader("main.css", strings.NewReader(`@tailwind utilities;`), false) if err := conv.Run(); err != nil { panic(err) } } ``` -------------------------------- ### Load Tailwind CSS from Filesystem (Go) Source: https://context7.com/gotailwindcss/tailwind/llms.txt Demonstrates how to load Tailwind CSS files from a local directory using the `twfiles` package. This is useful for integrating a locally built Tailwind distribution into your Go project. Ensure the specified directory contains the necessary Tailwind CSS files. ```go package main import ( "bytes" "strings" "github.com/gotailwindcss/tailwind" "github.com/gotailwindcss/tailwind/twfiles" ) func main() { // Directory should contain base.css, utilities.css, components.css dist := twfiles.New("/path/to/tailwind-dist") var output bytes.Buffer conv := tailwind.New(&output, dist) conv.AddReader("main.css", strings.NewReader(`@tailwind base;`), false) if err := conv.Run(); err != nil { panic(err) } } ``` -------------------------------- ### Command Line Help for gotailwindcss Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md Command to display the help message for the `gotailwindcss` tool, outlining its available subcommands and options. ```bash gotailwindcss --help ``` -------------------------------- ### Configure HTTP Handler with Caching and Compression (Go) Source: https://context7.com/gotailwindcss/tailwind/llms.txt Sets up an HTTP handler for serving Tailwind CSS with browser caching (1 year) and Brotli/Gzip compression. This handler is useful for development environments with hot-reloading. It uses the `twhandler` and `twembed` packages. ```go package main import ( "log" "net/http" "github.com/andybalholm/brotli" "github.com/gotailwindcss/tailwind/twembed" "github.com/gotailwindcss/tailwind/twhandler" ) func main() { handler := twhandler.New(http.Dir("css"), "/css", twembed.New()) // Enable Brotli/Gzip compression handler.SetWriteCloserFunc(brotli.HTTPCompressor) // Cache for 1 year (use with cache-busting URLs) handler.SetMaxAge(31536000) // Or disable internal caching for debugging // handler.SetCache(false) mux := http.NewServeMux() mux.Handle("/css/", handler) mux.Handle("/", http.FileServer(http.Dir("static"))) server := &http.Server{Addr: ":8080", Handler: mux} log.Fatal(server.ListenAndServe()) } ``` -------------------------------- ### Creating an HTTP Handler with twhandler in Go Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md Shows how to instantiate the `twhandler.New` function to create an HTTP handler for processing CSS files on the fly. It requires the directory of CSS files, the HTTP path prefix, and a Tailwind distribution. ```go import "net/http" import "github.com/gotailwindcss/tailwind/twembed" import "github.com/gotailwindcss/tailwind/twhandler" h := twhandler.New( http.Dir("/path/to/css"), // directory from which to read input CSS files "/css", // HTTP path prefix to expect twembed.New(), // Tailwind distribution ) ``` -------------------------------- ### Go: HTTP Handler for Live Tailwind Processing Source: https://context7.com/gotailwindcss/tailwind/llms.txt Sets up an HTTP handler that serves CSS files with live Tailwind processing on each request. It uses a Go HTTP multiplexer to serve static files and a dedicated handler for CSS processing, including a Tailwind distribution. The server listens on port 8080. ```go package main import ( "log" "net/http" "github.com/gotailwindcss/tailwind/twembed" "github.com/gotailwindcss/tailwind/twhandler" ) func main() { mux := http.NewServeMux() // Serve static HTML files mux.Handle("/", http.FileServer(http.Dir("static"))) // Serve CSS with Tailwind processing mux.Handle("/css/", twhandler.New( http.Dir("css"), // source directory "/css", // URL path prefix twembed.New(), // Tailwind distribution )) server := &http.Server{ Addr: ":8080", Handler: mux, } log.Printf("Server starting on http://localhost:8080") log.Fatal(server.ListenAndServe()) } ``` -------------------------------- ### CSS Conversion with tailwind.Convert in Go Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md Demonstrates how to use the `tailwind.Convert` type to process CSS, including directives like `@tailwind` and `@apply`. It involves creating a `Convert` instance, adding input readers, running the conversion, and capturing the output. Dependencies include `bytes`, `strings`, `tailwind`, and `twembed`. ```go import "bytes" import "strings" import "github.com/gotailwindcss/tailwind" import "github.com/gotailwindcss/tailwind/twembed" var w bytes.Buffer conv := tailwind.New(&w, twembed.New()) conv.AddReader("base.css", strings.NewReader(`@tailwind base;`), false) err := conv.Run() // w now has the processed CSS output ``` -------------------------------- ### Go: Purge Scanner for Production Optimization Source: https://context7.com/gotailwindcss/tailwind/llms.txt Implements a purge scanner for production optimization in GoTailwindCSS. It initializes a scanner from a Tailwind distribution, walks directory trees to find used CSS classes in specified file types (.html, .vue, .jsx), and then converts CSS with purging enabled, logging the purged CSS size. ```go package main import ( "bytes" "log" "path/filepath" "strings" "github.com/gotailwindcss/tailwind" "github.com/gotailwindcss/tailwind/twembed" "github.com/gotailwindcss/tailwind/twpurge" ) func main() { dist := twembed.New() // Create scanner with all possible Tailwind class names scanner, err := twpurge.NewScannerFromDist(dist) if err != nil { log.Fatal(err) } // Walk directory tree and scan for used classes err = filepath.Walk("static", scanner.WalkFunc(func(fn string) bool { ext := filepath.Ext(fn) return ext == ".html" || ext == ".vue" || ext == ".jsx" })) if err != nil { log.Fatal(err) } // Convert CSS with purging enabled var output bytes.Buffer conv := tailwind.New(&output, dist) conv.SetPurgeChecker(scanner.Map()) conv.AddReader("main.css", strings.NewReader(`@tailwind utilities;`), false) if err := conv.Run(); err != nil { log.Fatal(err) } log.Printf("Purged CSS size: %d bytes", output.Len()) } ``` -------------------------------- ### Standalone Go HTTP Server with Tailwind CSS Purging Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md This Go program sets up an HTTP server that serves static files and dynamically compiles/purges Tailwind CSS. It uses `twpurge` to scan CSS files for unused classes, reducing the output size. The handler `twhandler.NewFromFunc` is used for dynamic CSS generation. ```go package main import ( "io" "log" "net/http" "path/filepath" "github.com/gotailwindcss/tailwind" "github.com/gotailwindcss/tailwind/twembed" "github.com/gotailwindcss/tailwind/twhandler" "github.com/gotailwindcss/tailwind/twpurge" ) func main() { staticDir := http.Dir("static") indexH := http.FileServer(staticDir) pscanner, err := twpurge.NewScannerFromDist(twembed.New()) if err != nil { panic(err) } err = filepath.Walk("static", pscanner.WalkFunc(twpurge.MatchDefault)) if err != nil { panic(err) } tailwindH := twhandler.NewFromFunc(http.Dir("css"), "/css", func(w io.Writer) *tailwind.Converter { ret := tailwind.New(w, twembed.New()) ret.SetPurgeChecker(pscanner.Map()) return ret }) mux := http.NewServeMux() mux.Handle("/", indexH) mux.Handle("/css/", tailwindH) s := &http.Server{Addr: ":8182", Handler: mux} log.Fatal(s.ListenAndServe()) } ``` -------------------------------- ### Basic HTML Structure with Tailwind CSS Link Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md A simple HTML file that links to a CSS file (`/css/main.css`) which is expected to be processed by a Tailwind CSS handler. It demonstrates basic HTML structure with a head and body, including an element styled with a Tailwind class. ```html Test Button ``` -------------------------------- ### Enabling Compression for twhandler in Go Source: https://github.com/gotailwindcss/tailwind/blob/master/README.md Demonstrates how to configure the `twhandler` to use compression (Brotli and Gzip) by utilizing `SetWriteCloserFunc` with `brotli.HTTPCompressor`. This improves performance by reducing transfer sizes. ```go import "net/http" import "github.com/andybalholm/brotli" import "github.com/gotailwindcss/tailwind/twembed" import "github.com/gotailwindcss/tailwind/twhandler" h := twhandler.New(http.Dir("/path/to/css"), "/css", twembed.New()) h.SetWriteCloserFunc(brotli.HTTPCompressor) // ... ``` -------------------------------- ### Go: Convert CSS with Tailwind Directives Source: https://context7.com/gotailwindcss/tailwind/llms.txt Converts CSS files containing Tailwind directives (@tailwind, @apply) into processed output using Go. It takes a writer for the output and an embedded Tailwind distribution as input. The processed CSS length is printed upon completion. ```go package main import ( "bytes" "fmt" "strings" "github.com/gotailwindcss/tailwind" "github.com/gotailwindcss/tailwind/twembed" ) func main() { var output bytes.Buffer conv := tailwind.New(&output, twembed.New()) input := ` @tailwind base; @tailwind components; .button { @apply inline-block m-2 p-2 rounded-md bg-green-400; } @tailwind utilities; ` conv.AddReader("main.css", strings.NewReader(input), false) if err := conv.Run(); err != nil { panic(err) } fmt.Printf("Processed CSS length: %d bytes\n", output.Len()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.