Skip to content

Instantly share code, notes, and snippets.

@TeamDijon
Created June 29, 2024 10:55
Show Gist options
  • Save TeamDijon/8f85a3c30353991b92d6aa3da8860dd7 to your computer and use it in GitHub Desktop.
Save TeamDijon/8f85a3c30353991b92d6aa3da8860dd7 to your computer and use it in GitHub Desktop.
{%- comment -%}
Minifies and return CSS. Returns nothing if the CSS is empty.
Accepts:
- css {string} - CSS to minify
- section {Section object} - Section object (Optional)
Usage:
{% capture dynamic_style %}
{% render 'template-section-style' %}
{% endcapture %}
{% render 'css-minifier', css: dynamic_style, section: section %}
{%- endcomment -%}
{% liquid
if section != null
assign base_selector = '#shopify-section-' | append: section.id
assign dynamic_style = css | remove: base_selector | remove: ' {' | remove: '}' | strip
if dynamic_style == empty
continue
endif
elsif css == blank
continue
endif
assign css_chunk_list = css | strip_newlines | split: ' ' | join: ' ' | split: '*/'
assign minified_css = ''
for css_chunk in css_chunk_list
assign minified_chunk = css_chunk | split: '/*' | first | strip
assign minified_css = minified_css | append: minified_chunk
endfor
assign minified_css = minified_css | replace: '; ', ';' | replace: '} ', '}' | replace: '{ ', '{' | replace: ' {', '{' | replace: ': ', ':' | replace: ';}', '}'
style
echo minified_css
endstyle
%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment