Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused CSS #36

Closed
stevenvachon opened this issue Dec 6, 2016 · 3 comments
Closed

Remove unused CSS #36

stevenvachon opened this issue Dec 6, 2016 · 3 comments

Comments

@stevenvachon
Copy link

with email-remove-unused-css.

It's built with posthtml, but it currently cannot be used as a plugin and forces a reparse of the HTML. This should get fixed, though.

@revelt
Copy link

revelt commented Dec 7, 2016

I agree, the unused CSS removal is a perfect feature for minifier.

And there can be issues when any removal of CSS is done after the minification because, for example, mentioned email-remove-unused-css parses and renders back the code. The CSS renderer (stringifier) won't output minified code, what means minifying would need to be performed again, OR unused CSS removed first and only then whole thing minified. When minification does the CSS removal it's faster for user.

email-remove-unused-css is for email code and was not designed to accept external stylesheets. That will be an issue for htmlnano which is web pages oriented. Unless we're happy with removal only what's contained. In a sense that's fine since it's quite quick procedure, it's not unCSS.

There is a PostHTML plugin to remove unused CSS, https://www.npmjs.com/package/postcss-remove-unused and looks like it accepts external stylesheets, although probably not id attributes.

@fstanis
Copy link
Contributor

fstanis commented Dec 27, 2018

How about using uncss?

It seems that, at least theoretically, we could add uncss to the minifyCss module with the help of posthtml-render (since uncss requires the html):

const uncss = require('uncss');
const render = require('posthtml-render');

[...]

function processStyleNode(tree, styleNode, cssnanoOptions) {
  const css = Array.isArray(styleNode.content) ? styleNode.content.join(' ') : styleNode.content;
  [...]
  const html = render(tree, options);
  uncss(html, { stylesheets: [], raw: css } (error, output) => { ... });
}

Thoughts on this?

@maltsev
Copy link
Member

maltsev commented Jan 3, 2019

Thanks, @fstanis for implementing that! I released this module and your other fixes as version 0.2.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants