ScopedCss.compile constructor

ScopedCss.compile(
  1. String rawCss, {
  2. String? name,
})

Compiles rawCss into a scoped stylesheet with class name mappings.

Derives a deterministic 32-bit FNV-1a hash from rawCss and name, rewrites all local class selectors, creates the immutable classes map, and wraps the output in a StyleNode node.

final styles = ScopedCss.compile('''
  .badge {
    display: inline-flex;
    padding: 2px 8px;
    border-radius: 9999px;
  }
''', name: 'badge');

Implementation

factory ScopedCss.compile(String rawCss, {String? name}) {
  final seed = name != null && name.isNotEmpty ? '$name:$rawCss' : rawCss;
  final hashInt = fnv1a32(seed);
  final hashStr = hashInt.toRadixString(16).padLeft(8, '0').substring(0, 7);

  final scoper = _CssScoper(rawCss, hashStr, name);
  final transformedCss = scoper.process();
  final immutableClasses = Map<String, String>.unmodifiable(scoper.classes);

  return ScopedCss._(
    rawCss: rawCss,
    css: transformedCss,
    hash: hashStr,
    name: name,
    classes: immutableClasses,
    node: StyleNode(transformedCss),
  );
}