fphilipe / premailer-rails

CSS styled emails without the hassle.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any way to remove unused classes from <style>?

aguynamedben opened this issue · comments

Thanks for the time you spend working on this awesome gem. It's really helping me with a project right now. I had a question that I couldn't find an answer for in the docs.

I saw #184 and was curious if there is indeed a way to remove unused CSS classes that remain in the <style> element after premailer copies CSS into style elements. For reference, my email CSS lives at app/assets/stylesheets/emails/emails.less, and emails.less looks like this:

@import "emails/shared.less";
@import "emails/first_email.less";
@import "emails/second_email.less";

Shared CSS is in shared.less, and email-specific email is in the respective .less file and is scoped by a class on the body element (i.e. body.first_email {}).

Because premailer leaves unused classes lying around in the <style> element, that element remains way larger than it needs to be in my case. Is there an easy way to remove both used and unused classes from the resulting <style> element? Or to just remove the remaining <style> element altogether? It seems like if premailer is doing its job this should be safe to do.

Thanks for any feedback, and again, thanks for the time you spend working on this gem.

Glad this gem is of help 😊

Take a look at premailer's configs here (not premailer-rails). If that doesn't help, maybe ask directly on premailer.

Good luck.

Still didn't find how to remove <style> tag

This not work

Premailer::Rails.config.merge!(
  preserve_styles:    false,
  include_style_tags: false,
  remove_comments:    true,
  remove_classes:     true,
  remove_ids:         true
)

module Helpers
  module PremailMailAndReturnHtml
    extend self

    def premail_mail_and_return_html(mail)
      Premailer::Rails::Hook.perform(mail)
      mail.part.second.body.to_s
    end
  end
end

this work

module Helpers
  module PremailMailAndReturnHtml
    extend self

    def premail_mail_and_return_html(mail)
      Premailer
        .new(
          mail.body.to_s,
          with_html_string:   true,
          preserve_styles:    false,
          include_style_tags: false,
          remove_comments:    true,
          remove_classes:     true,
          remove_ids:         true
        ).to_inline_css
    end
  end
end

ok, it doesnt work only in tests