css

package
v0.4.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bundle

func Bundle(assets []*Asset) string

func EscapeClass added in v0.4.0

func EscapeClass(s string) string

EscapeClass converts a Tailwind class name into a CSS-selector-safe identifier per the CSSOM `CSS.escape` algorithm, so selectors like `w-1/2`, `w-[100px]`, `bg-red-500/50`, `content-['a.b']` are valid.

Rules (https://drafts.csswg.org/cssom/#serialize-an-identifier):

  • U+0000 → U+FFFD
  • control chars (U+0001–U+001F, U+007F) → \HEX
  • a leading digit → \HEX
  • a digit in the second position when the first char is '-' → \HEX
  • a lone "-" → \-
  • '-' '_' and ASCII alphanumerics are safe elsewhere
  • everything else is backslash-escaped (with a trailing space when the escaped char is a hex digit, to disambiguate)

func ExtractClassNames

func ExtractClassNames(css string) []string

func GenerateMapping

func GenerateMapping(path string, classNames []string) map[string]string

func GenerateTailwind

func GenerateTailwind(root string, cfg *TailwindConfig) (string, error)

GenerateTailwind runs the full Tailwind pipeline: scan classes → generate CSS.

func GenerateTailwindWithOptions added in v0.4.0

func GenerateTailwindWithOptions(root string, cfg *TailwindConfig, opts TailwindOptions) (string, error)

GenerateTailwindWithOptions is GenerateTailwind with explicit options.

func InlineImports

func InlineImports(css, basePath string) string

InlineImports resolves and inlines @import directives in CSS content. basePath is the directory containing the CSS file, used to resolve relative paths. All resolved imports must remain inside basePath (the project root) — traversal above it is treated as invalid and left un-inlined. It handles circular imports via a visited set and has a max depth of 10.

func Minify

func Minify(css string) string

func ProcessModule

func ProcessModule(path, content string) (scopedCSS string, mapping map[string]string, err error)

func ScopeCSS

func ScopeCSS(content string, mapping map[string]string) string

func StripTailwindDirectives

func StripTailwindDirectives(css string) string

StripTailwindDirectives removes @tailwind and @apply directives from CSS.

func TailwindPreflight added in v0.4.0

func TailwindPreflight(theme TailwindTheme) string

TailwindPreflight returns Tailwind's Preflight base reset (opt-in via `tailwind.preflight`). It normalizes box-sizing, borders, margins, headings, lists, media, and form controls, and sets the default transform/space custom properties so composed utilities behave correctly.

func VerifyMapping

func VerifyMapping(mapping map[string]string) error

Types

type Asset

type Asset struct {
	Path     string
	Content  string
	IsModule bool
}

func Collect

func Collect(dir string) ([]*Asset, error)

type ModuleMapping

type ModuleMapping struct {
	LocalVar string
	Mappings map[string]string
}

type TailwindConfig

type TailwindConfig struct {
	Theme    TailwindTheme `json:"theme"`
	DarkMode string        `json:"darkMode"`
}

TailwindConfig represents the tailwind.config.ts/js parsed values.

func LoadTailwindConfig

func LoadTailwindConfig(root string) *TailwindConfig

LoadTailwindConfig loads a tailwind.config file. It prefers a static parse (no Node required); when `execute` is true or the static parse yields no theme, it falls back to executing the config via `npx tsx`. On any failure it returns defaults (a diagnostic is surfaced by the caller).

func LoadTailwindConfigWithOptions added in v0.4.0

func LoadTailwindConfigWithOptions(root string, opts TailwindOptions) *TailwindConfig

LoadTailwindConfigWithOptions is LoadTailwindConfig with explicit options.

func ParseTailwindConfigStatic added in v0.4.0

func ParseTailwindConfigStatic(path string) (*TailwindConfig, bool)

ParseTailwindConfigStatic parses a tailwind.config.{ts,js,mjs} file without executing it, extracting the common `theme` / `extend` / `darkMode` shapes. It deliberately handles a useful subset (object/array/string literals) so the Go-native build needs no Node; anything it cannot parse returns ok=false and the caller may fall back to `npx tsx`.

type TailwindGenerator

type TailwindGenerator struct {
	Theme TailwindTheme
	// Strict, when true, collects classes that produced no rule.
	Strict bool
	// Unknown collects class names that yielded no rule (populated when Strict).
	Unknown []string
}

TailwindGenerator converts class names to CSS rules.

func NewTailwindGenerator

func NewTailwindGenerator() *TailwindGenerator

NewTailwindGenerator creates a generator with default theme.

func (*TailwindGenerator) Generate

func (g *TailwindGenerator) Generate(classes map[string]bool) string

Generate produces CSS for the given set of class names. Output is deterministic: rules are sorted by a stable key so identical inputs always produce byte-identical stylesheets (stable content hashing).

func (*TailwindGenerator) MergeConfig

func (g *TailwindGenerator) MergeConfig(cfg *TailwindConfig)

MergeConfig merges a TailwindConfig into the generator's theme.

type TailwindOptions added in v0.4.0

type TailwindOptions struct {
	// ScanDirs are directories (relative to root) to scan for classes.
	ScanDirs []string
	// ContentOverrides, when non-empty, replaces ScanDirs (Tailwind `content`).
	ContentOverrides []string
	// Preflight enables the base reset.
	Preflight bool
	// Strict collects unrecognized classes.
	Strict bool
	// DarkMode overrides the config's dark-mode strategy.
	DarkMode string
	// ExecuteConfig forces `npx tsx` config execution instead of static parse.
	ExecuteConfig bool
}

TailwindOptions carries site config that influences generation (scan roots, preflight, strict diagnostics).

type TailwindScanner

type TailwindScanner struct {
	Root string
}

TailwindScanner extracts Tailwind utility class names from source files.

func NewTailwindScanner

func NewTailwindScanner(root string) *TailwindScanner

NewTailwindScanner creates a scanner for the given project root.

func (*TailwindScanner) ScanClasses

func (s *TailwindScanner) ScanClasses(dirs []string) map[string]bool

ScanClasses walks the project and extracts every Tailwind class candidate used in the scanned files. It is a broad candidate extractor (matching real Tailwind's approach): every string and template literal is scanned and its tokens filtered by shape, so classes inside `class={cond ? "a" : "b"}`, `clsx()/cn()/cva()` arguments, and arrays are all found.

type TailwindTheme

type TailwindTheme struct {
	Spacing     map[string]string            `json:"spacing"`
	Sizing      map[string]string            `json:"sizing"`
	MaxWidth    map[string]string            `json:"maxWidth"`
	MinWidth    map[string]string            `json:"minWidth"`
	LineHeight  map[string]string            `json:"lineHeight"`
	Opacity     map[string]string            `json:"opacity"`
	Screens     map[string]string            `json:"screens"`
	Colors      map[string]map[string]string `json:"colors"`
	TextSizes   map[string]string            `json:"textSizes"`
	FontWeights map[string]string            `json:"fontWeights"`
	FontFamily  map[string]string            `json:"fontFamily"`
	Radii       map[string]string            `json:"radii"`
	Shadows     map[string]string            `json:"shadows"`
	BgColors    map[string]string            `json:"bgColors"`
	// DarkMode is "media" (default), "class", or "selector".
	DarkMode string `json:"darkMode"`
}

TailwindTheme holds user-customizable theme values for the Tailwind generator.

func DefaultTailwindTheme

func DefaultTailwindTheme() TailwindTheme

DefaultTailwindTheme returns the default Tailwind theme configuration.

Jump to

Keyboard shortcuts

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