IgniterCss.Transform (igniter_css v1.0.0)

Copy Markdown View Source

Whole-file transforms. These are not codemods.

Everything in IgniterCss is diff-minimal by construction. The functions here deliberately are not: minifying and beautifying rewrite every byte, and minifying discards comments, because that is what minifying is.

Use them for build-time output and reporting. Do not use them to patch a user's stylesheet — IgniterCss.set_declaration/5 and friends exist for that, and they never route their output through here.

Summary

Functions

Re-print the stylesheet with one declaration per line and consistent indentation, keeping every comment.

Concatenate stylesheets, then drop rules a later copy makes redundant.

Strip comments and collapse whitespace.

Types

opts()

@type opts() :: keyword()

Functions

beautify(source, opts \\ [])

@spec beautify(String.t(), opts()) :: {:ok, String.t()} | {:error, String.t()}

Re-print the stylesheet with one declaration per line and consistent indentation, keeping every comment.

iex> IgniterCss.Transform.beautify(".a{color:red;margin:0}")
{:ok, ".a {\n  color: red;\n  margin: 0;\n}\n"}

merge_stylesheets(sources, opts \\ [])

@spec merge_stylesheets([String.t()], opts()) ::
  {:ok, String.t()} | {:error, String.t()}

Concatenate stylesheets, then drop rules a later copy makes redundant.

A rule is dropped only when a later one has the same selector and a byte-identical body, so an intentional override survives.

iex> IgniterCss.Transform.merge_stylesheets([".a { color: red; }", ".a { color: red; }"])
{:ok, ".a { color: red; }\n"}

minify(source, opts \\ [])

@spec minify(String.t(), opts()) :: {:ok, String.t()} | {:error, String.t()}

Strip comments and collapse whitespace.

Driven by the token stream rather than by text matching, so a ; inside url(...) or a /* inside a string is never mistaken for syntax. Whitespace is kept only where removing it would change how the result tokenises, so @media screen and (min-width: 40em) keeps the space after and.

iex> IgniterCss.Transform.minify(".a {\n  color: red;\n}\n")
{:ok, ".a{color:red}"}