LiveStyle.UsageManifest (LiveStyle v0.14.0)

View Source

Tracks which LiveStyle classes are actually used in code.

This module provides a data structure for tracking class usage at compile time, enabling tree-shaking of unused styles (StyleX-style optimization).

Usage Tracking

When css/1 macros expand, they record which classes are referenced. This allows the CSS generator to emit only styles that are actually used.

Data Structure

Usage is stored as a MapSet of {module, class_name} tuples where:

  • module is the defining module (e.g., MyApp.Button)
  • class_name is the atom name (e.g., :primary)

Example

usage = UsageManifest.empty()
usage = UsageManifest.record_usage(usage, MyApp.Button, :primary)
UsageManifest.used?(usage, MyApp.Button, :primary)  #=> true

Summary

Functions

Returns an empty usage manifest.

Checks if a class is used by its manifest key.

Marks all classes from a manifest as used.

Merges two usage manifests.

Records that a class is used.

Returns the number of recorded usages.

Converts usage manifest to a list of {module, class_name} tuples.

Checks if a class is used.

Types

t()

@type t() :: MapSet.t({module(), atom()})

Functions

empty()

@spec empty() :: t()

Returns an empty usage manifest.

key_used?(usage, key)

@spec key_used?(t(), String.t()) :: boolean()

Checks if a class is used by its manifest key.

Manifest keys have the format "Elixir.Module.class_name".

Parameters

  • usage - The usage manifest
  • key - The manifest key (e.g., "Elixir.MyApp.Button.primary")

Returns

true if the class has been recorded as used, false otherwise.

mark_all_used(usage, manifest)

@spec mark_all_used(t(), map()) :: t()

Marks all classes from a manifest as used.

This is useful for testing where you want to verify CSS output without needing to explicitly call css/1 for each class.

Similar to StyleX's treeshakeCompensation option.

Parameters

  • usage - The current usage manifest
  • manifest - The LiveStyle manifest containing class definitions

Returns

Updated usage manifest with all classes marked as used.

merge(usage1, usage2)

@spec merge(t(), t()) :: t()

Merges two usage manifests.

Parameters

  • usage1 - First usage manifest
  • usage2 - Second usage manifest

Returns

A new usage manifest containing all entries from both.

record_usage(usage, module, class_name)

@spec record_usage(t(), module(), atom()) :: t()

Records that a class is used.

Parameters

  • usage - The current usage manifest
  • module - The module defining the class
  • class_name - The atom name of the class

Returns

Updated usage manifest with the class recorded.

size(usage)

@spec size(t()) :: non_neg_integer()

Returns the number of recorded usages.

to_list(usage)

@spec to_list(t()) :: [{module(), atom()}]

Converts usage manifest to a list of {module, class_name} tuples.

used?(usage, module, class_name)

@spec used?(t(), module(), atom()) :: boolean()

Checks if a class is used.

Parameters

  • usage - The usage manifest
  • module - The module defining the class
  • class_name - The atom name of the class

Returns

true if the class has been recorded as used, false otherwise.