asyncLiz / minify-html-literals

Minify HTML template literal strings
MIT License
68 stars 14 forks source link

Investigate CSS replacements in style attribute #23

Open lukapopijac opened 4 years ago

lukapopijac commented 4 years ago

where did the px go?

in.js:

html`<div style="width: ${a}px;"></div>`;

out.js:

html`<div style="width:${a}"></div>`;

Breaks with this message:

(!) Plugin minify-html-literals: splitHTMLByPlaceholder() must return same number of strings as template parts

input.js:

html`
    <div style="--xyz: ${a};">
    </div>
`;
asyncLiz commented 4 years ago

I'm not sure if template literal replacements in the HTML style attribute is something the minifier will support properly. I'll move this to minify-html-literals as a feature request to investigate.

This may also be an issue with general CSS value unit replacements.

My first recommendation as a workaround would be to avoid using the style attribute, since the minifier supports minifying <style> tags and css template literals. If the style attribute must be used, my second recommendation in the meantime would be to replace the entire style attribute instead of part of it (<div style="#{divStyle}">).

lukapopijac commented 4 years ago

Good. Thanks for the recommendations! I will avoid using js placeholders inside <style> as that is an antipattern, as mentioned in lit-html documentation (https://lit-html.polymer-project.org/guide/styling-templates, section "Bindings in style sheets"). I will consider the other recommendation you mentioned. Thanks!