Created
June 29, 2024 10:55
-
-
Save TeamDijon/8f85a3c30353991b92d6aa3da8860dd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%- 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