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

stylesheet_pack_tag should be consistent about whether it produces error (or not) about missing asset regardless of extract_css #2342

Closed
TylerRick opened this issue Oct 30, 2019 · 6 comments

Comments

@TylerRick
Copy link
Contributor

TylerRick commented Oct 30, 2019

Would it be okay if we either:

  1. make stylesheet_pack_tag always simply silently return nil if there is no stylesheet for the given pack name (this is currently the behavior only if extract_css: false), or

  2. make stylesheet_pack_tag always raise a
    Webpacker can't find {pack_name} in public/packs/manifest.json Webpacker::Manifest::MissingEntryError if there is no stylesheet for the given pack name (this is currently the behavior only if extract_css: true)

?

Documentation reference

My workaround

I ended up not running into an error from a stylesheet_pack_tag referencing something that wasn't in the manifest until it got to staging. It bugged me that development behaves differently from production. Enough that I changed the default: section of my config/webpacker.yml to:

  # This used to be `false` here and `true` only in production. But then we can't catch errors such
  # as stylesheet_pack_tag referencing something not in manifest until it gets to staging/production.
  extract_css: true

I'm not sure yet if there are any downsides to using extract_css: true in development (no HMR?), but at least this gives me the same behavior between dev and production...

Confusion in the community

#2059 (comment)

However, the problem I've observed with this is that if you have stylesheet_pack_tag in your layout as a default, but your javascript doesn't spit out any styles, then you will run into an error on live where rails complains about stylesheet not existing.

This should be consistent for dev and production environment. As in, if error is to be generated on production for no stylesheet generated, then development should also give the error.

#2059 (comment)

I'm using webpacker with react and, by importing the scss in my packs/*.js, the styles work...in development. When I deployed to production, I realized there are no styles available.

Do I need to add stylesheet_pack_tag for it to work in production?

I think it would be better if I'd receive any error at any point.

#2202:

This is alright, but [stylesheet_link_tag] doesn't raise an error for packs that don't exist ...

@jakeNiemiec
Copy link
Member

stylesheet_pack_tagshould be consistent about whether it produces error (or not) about missing asset regardless of extract_css

extract_css: false means that no stylesheet_packs will be created. This is why it will return nil...you don't want styles in the head and from your stylesheet_pack_tags.

I'm not sure yet if there are any downsides to using extract_css: true

For codebases with a moderate amount of styling, extract_css: true can add 2+ minutes to compile time. This is why it is not recommended for development.

Let's continue this in the other issues you opened.

@bbugh
Copy link
Contributor

bbugh commented Apr 29, 2020

I just spent a couple of hours trying to figure out why production was failing but development works fine. We added a pack and put in the stylesheet_pack_tag. Everything was fine, and then production broke when we launched.

This being a fatal error in production with no warning during development is very surprising. It is a hard to track down error, especially because webpack just reports the name of the pack without a file extension. We thought something was wrong with the JavaScript and just by trying everything possible found out that it was the stylesheet failing.

None of the linked issues from @TylerRick specifically address production having a fatal production error when it can't find a css file. :(

Can we reopen this and address it directly?

@SampsonCrowley
Copy link
Contributor

Seriously this is a big problem to think your app is running fine, to find out it's not a silent error in production

@SampsonCrowley
Copy link
Contributor

it would be nice if there was a stylesheet pack tag helper that included css if it exists, and failed silently if not

@SampsonCrowley
Copy link
Contributor

SampsonCrowley commented Jun 11, 2020

my workaround for anyone looking for a quick fix:

# app/helpers/webpacker_overrides.rb
module WebpackerOverrides
  private
    def rescued_pack_tags
      @rescued_pack_tags ||= {}
    end

    def sources_from_manifest_entries(names, type:)
      names.map do |name|
        unless (type == :stylesheet) && rescued_pack_tags[name]
          current_webpacker_instance.manifest.lookup!(name, type: type)
        end
      rescue
        raise unless type == :stylesheet
        rescued_pack_tags[name] = true
        nil
      end.flatten.select(&:present?)
    end
end

rbclark pushed a commit to mitre/vulcan that referenced this issue Aug 17, 2021
Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing.
rails/webpacker#2342
rails/webpacker#2059 (comment)
This adds an override which causes no errors in production but still allows us to leave the stylesheet_pack_tag for when it is needed.
rbclark pushed a commit to mitre/vulcan that referenced this issue Aug 17, 2021
Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing.
rails/webpacker#2342
rails/webpacker#2059 (comment)
This adds an override which causes no errors in production but still allows us to leave the stylesheet_pack_tag for when it is needed.
@rbclark
Copy link

rbclark commented Aug 18, 2021

This issue should really be reopened since this seems to be the only issue that was created by @TylerRick specifically about the inconsistent behavior of stylesheet_pack_tag. After trying and failing to use the workaround provided above I'm going to end up just using extract_css: true all the time since it provides completely inconsistent behavior.

rbclark pushed a commit to mitre/vulcan that referenced this issue Aug 18, 2021
Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing.
rails/webpacker#2342
rails/webpacker#2059 (comment)

Unfortunately using extract_css: true all the time apparently comes with some performance impacts, however the tradeoff is the application actually works correctly in development mode.
rbclark pushed a commit to mitre/vulcan that referenced this issue Aug 18, 2021
Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing.
rails/webpacker#2342
rails/webpacker#2059 (comment)

Unfortunately using extract_css: true all the time apparently comes with some performance impacts, however the tradeoff is the application actually works correctly in development mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants