minify-html-literals icon indicating copy to clipboard operation
minify-html-literals copied to clipboard

Investigate CSS replacements in style attribute

Open lukapopijac opened this issue 5 years ago • 2 comments

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>
`;

lukapopijac avatar Sep 05 '20 06:09 lukapopijac

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}">).

asyncliz avatar Sep 05 '20 06:09 asyncliz

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!

lukapopijac avatar Sep 05 '20 06:09 lukapopijac