Feature: Change extract_css default to true in all environments #2608
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Dealing with CSS with Webpacker is a common issue. See the lengthy background in #2605 as one example for getting a local development running from rails new with webpacker. Another common issue is deployment; developers don't catch missing stylesheets locally as a result of
extract_css: false
until first deployment whereextract_css: true
, e.g., #2071.Solution
This changeset adds two key changes:
extract_css: true
by default for all environmentsapp/javascript/packs/application.css
file on install. This ensures that addingstylesheet_pack_tag "application"
to the layout does not raise an error if no styles have been otherwise imported from js.I expect there to be some discussion around these changes as they are fairly impactful. The most important argument I can make is that these changes together improve the out-of-the-box experience with Webpacker. I believe this would prevent issues like #2605 and #2071. This sets up an experience that is more Sprockets-friendly to start. Developers can remove this file if not needed.
Fixes #2592
Fixes #2605
Potential concerns
application.css
, add to the confusion? My change takes advantage of a new feature, Feature: Add support for multiple files per entry #2476: as of Webpacker 5, files of the same canonical name and different extensions act as a single multi-file entrypoint. I think, is more "Sprockets-like". I'm not sure that this convention is well-known yet however and could be surprising to folks with more experience.stylesheet_pack_tag "application"
tag in the application layout. I expected that would be a more drastic change, also deferring to the Rails default preference for Sprockets to compile CSS. Generally, developers are confused about how the Rails default straddles the fence between Sprockets and Webpacker, especially around CSS. My intent with this change is to ease the transition for developers who are new to webpack. I could see that it may add to confusion as well. I'm open to feedback on this.extract_css: false
is to enable HMR for CSS in development. My expectation is that developers would prefer a setup that is less surprising out-of-the-box than one that is optimized for HMR. This PR means one extra file change to get HMR working. I believe this is an acceptable tradeoff. To make that easier, I added an explicit entry forextract_css
in thedevelopment
section ofconfig/webpacker.yml
.