Previous: , Up: API  


1.22 (olive-css tree-shaking)

Content-aware pruning of the generated stylesheet. The pruner scans a directory of built HTML for the utility classes that are actually used, parses the compiled stylesheet into nested blocks, and keeps only the rules whose selectors match a used class.

Every path and search option is a Guile parameter, so a build can be customized with parameterize instead of editing this module:

(parameterize ((artifact-path "_site") (base-stylesheet-path "resources/css/olive.min.css") (output-css-target "app.min.css")) (tree-shake))

The command-line front-end lives in scripts/olive-css-tree-shaking.scm.

1.22.1 Parameters

Parameter: artifact-path

Default value:

"public"
Parameter: base-stylesheet-path

Default value:

"resources/css/olive.min.css"
Parameter: output-css-target

Default value:

"olive.min.css"
Parameter: scanned-file-types

Default value:

(".html" ".htm")
Parameter: skipped-directories

Default value:

(".git")

1.22.2 Procedures

Procedure: escape-class-name name

Escape NAME the way a CSS selector escapes it, e.g. "hover:bg-white" becomes "hover\:bg-white".

Procedure: extract-classes-from-html file-path

Return the (escaped) class names referenced by the ‘class’ attributes of the HTML file at FILE-PATH.

Procedure: parse-css-blocks s

Parse the sequence of blocks at one nesting level of S.

Procedure: scan-used-classes base-dir

Recursively traverse BASE-DIR to find every unique CSS class name used in the files matching ‘scanned-file-types’. Returns a list of escaped class names, e.g. ’bg-apple-green-100\/50’ or ’hover\:bg-white’.

Procedure: serialize-css-blocks nodes

Serialize a list of parsed blocks back into minified CSS text.

Procedure: tree-shake KEY: #:stylesheet #:directory #:target

Prune the stylesheet at STYLESHEET (default: ‘base-stylesheet-path’) down to the classes used under DIRECTORY (default: ‘artifact-path’), writing the result to TARGET (default: ‘output-css-target’) inside that directory.

Returns an association list describing the run: the input stylesheet, the destination written, the number of used classes and the byte sizes before and after pruning.

Procedure: tree-shake-css css used

Return a minified stylesheet containing only the rules relevant to USED.


Previous: (olive-css sizes), Up: API