Vize.CSS (Vize v0.11.0)

Copy Markdown View Source

CSS parsing, printing, and AST traversal helpers.

The AST is produced by LightningCSS and represented as Elixir maps, lists, strings, numbers, booleans, and nil. Use parse_ast/2 to parse CSS, transform the returned AST with prewalk/2 or postwalk/2, and then use print_ast/2 to serialize it back to CSS.

Summary

Functions

Bundle a CSS file and all its @import dependencies into a single stylesheet.

Like bundle/2 but raises on errors.

Collect values from map nodes that match fun.

Compile CSS using LightningCSS.

Like compile/2 but raises on errors.

Parse CSS into a LightningCSS-backed AST represented as Elixir maps and lists.

Like parse_ast/2 but raises on errors.

Depth-first post-order traversal, like Macro.postwalk/2.

Depth-first post-order traversal with accumulator, like Macro.postwalk/3.

Depth-first pre-order traversal, like Macro.prewalk/2.

Depth-first pre-order traversal with accumulator, like Macro.prewalk/3.

Print CSS from an AST returned by parse_ast/2.

Like print_ast/2 but raises on errors.

Traverse the AST in pre-order and call fun for every map node.

Types

ast()

@type ast() :: map() | list() | String.t() | number() | boolean() | nil

ast_result()

@type ast_result() :: %{
  ast: map() | nil,
  errors: [String.t()],
  warnings: [String.t()]
}

css_result()

@type css_result() :: %{
  optional(:exports) => %{optional(String.t()) => String.t()} | nil,
  code: String.t(),
  css_vars: [String.t()],
  errors: [String.t()],
  warnings: [String.t()]
}

Functions

bundle(entry_path, opts \\ [])

@spec bundle(
  String.t(),
  keyword()
) :: {:ok, css_result()}

Bundle a CSS file and all its @import dependencies into a single stylesheet.

Reads the entry file and all imported files from disk, resolving @import rules recursively. The result is a single merged stylesheet with all imports inlined, wrapped in the appropriate @media, @supports, and @layer rules.

Options

  • :minify — minify the output (default: false)
  • :css_modules — enable CSS Modules scoping (default: false)
  • :targets — browser targets for autoprefixing

bundle!(entry_path, opts \\ [])

@spec bundle!(
  String.t(),
  keyword()
) :: css_result()

Like bundle/2 but raises on errors.

collect(value, fun)

@spec collect(ast(), (map() -> {:keep, term()} | :skip)) :: [term()]

Collect values from map nodes that match fun.

fun should return {:keep, value} to include a value, or :skip to ignore the node.

compile(source, opts \\ [])

@spec compile(
  String.t(),
  keyword()
) :: {:ok, css_result()}

Compile CSS using LightningCSS.

Parses, autoprefixes, and optionally minifies CSS. Also handles Vue scoped CSS transformation, v-bind() extraction, and CSS Modules.

Options

  • :minify — minify the output (default: false)
  • :scoped — apply Vue scoped CSS transformation (default: false)
  • :scope_id — scope ID for scoped CSS (e.g. "data-v-abc123")
  • :filename — filename for error reporting
  • :css_modules — enable CSS Modules scoping (default: false)
  • :targets — browser targets for autoprefixing, map with optional :chrome, :firefox, :safari keys as major version integers

Examples

iex> {:ok, result} = Vize.CSS.compile(".foo { color: red }")
iex> result.code =~ "color"
true
iex> result.errors
[]

compile!(source, opts \\ [])

@spec compile!(
  String.t(),
  keyword()
) :: css_result()

Like compile/2 but raises on errors.

parse_ast(source, opts \\ [])

@spec parse_ast(
  String.t(),
  keyword()
) :: {:ok, ast_result()}

Parse CSS into a LightningCSS-backed AST represented as Elixir maps and lists.

Options

  • :filename — filename for parser locations and error reporting
  • :css_modules — enable CSS Modules parsing (default: false)
  • :custom_media — enable custom media parsing (default: false)

Examples

iex> {:ok, result} = Vize.CSS.parse_ast(".foo { background: url('./logo.svg') }")
iex> is_map(result.ast)
true
iex> result.errors
[]

parse_ast!(source, opts \\ [])

@spec parse_ast!(
  String.t(),
  keyword()
) :: ast_result()

Like parse_ast/2 but raises on errors.

postwalk(value, fun)

@spec postwalk(ast(), (map() -> map())) :: ast()

Depth-first post-order traversal, like Macro.postwalk/2.

The callback receives every map node after its children and must return the transformed node.

postwalk(value, acc, fun)

@spec postwalk(ast(), acc, (map(), acc -> {map(), acc})) :: {ast(), acc}
when acc: term()

Depth-first post-order traversal with accumulator, like Macro.postwalk/3.

prewalk(value, fun)

@spec prewalk(ast(), (map() -> map())) :: ast()

Depth-first pre-order traversal, like Macro.prewalk/2.

The callback receives every map node before its children and must return the node to continue traversing.

prewalk(value, acc, fun)

@spec prewalk(ast(), acc, (map(), acc -> {map(), acc})) :: {ast(), acc}
when acc: term()

Depth-first pre-order traversal with accumulator, like Macro.prewalk/3.

walk(value, fun)

@spec walk(ast(), (map() -> any())) :: :ok

Traverse the AST in pre-order and call fun for every map node.

Returns :ok. Use prewalk/2 or postwalk/2 when you need to transform the AST.