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

Feature: Change extract_css default to true in all environments #2608

Merged
merged 3 commits into from
May 30, 2020

Conversation

rossta
Copy link
Member

@rossta rossta commented May 28, 2020

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 where extract_css: true, e.g., #2071.

Solution

This changeset adds two key changes:

  • Set extract_css: true by default for all environments
  • Add an empty app/javascript/packs/application.css file on install. This ensures that adding stylesheet_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

  1. Does adding a separate file, 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.
  2. One change I did not make is inserting the corresponding 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.
  3. I believe that the main reason for 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 for extract_css in the development section of config/webpacker.yml.

rossta added 2 commits May 28, 2020 16:48
The common use case, for hot module reloading, should be opt-in. The
default production use case, extract_css: true, should be presented by
default in development and test to be less surprising to developers who
encounter issues during deployment.
Doing so ensures that stylesheet_pack_tag won't fail out of the box when
no styles are imported in JS. Support for "matching-name"CSS pack was
added in Webpacker 5.
@gauravtiwari gauravtiwari merged commit 68d643a into rails:master May 30, 2020
@justin808
Copy link
Contributor

@gauravtiwari @rossta can we get some more details on

  1. How will this interact when the application.js contains CSS? and this application.css also exists?
  2. Does splitting chunks with application.js matter?

Maybe this all "just works" thanks to Webpack's magic and support of this feature?

Per the docs from https://webpack.js.org/guides/entry-advanced/#multiple-file-types-per-entry:

It is possible to provide different types of files when using an array of values for entry to achieve separate bundles for CSS and JavaScript (and other) files in applications that are not using import for styles in JavaScript (pre Single Page Applications or different reasons).

What's not clear about these webpack docs is how this interacts with https://webpack.js.org/plugins/mini-css-extract-plugin/#root.

Maybe this doc in the generated file could offer some guidance on this?

I do say, it sounds quite nice simple imports in this application.css file, although maybe it should be called a with a Sass extension, as the docs list imports?

/* 
Any CSS added to this file or imported from this file, e.g. `@import '../stylesheets/my-css.css'`,
will be included in the "application" pack. Any CSS imported from application.js or as part of the 
application.js dependency graph, e.g. `import '../stylesheets/my-css.css'` will also be included 
in the "application" pack. 
*/

@justinperkins
Copy link

justinperkins commented Nov 14, 2020

Firing up a fresh Rails 6 (6.0.3.4) app, running into webpacker issues as described extensively elsewhere. Hello world version of rails (no controllers, models) ran fine, but once I created a single controller, webpacker immediately stepped in with complaints about manifest issues. I attempted all the usual antics (webpacker:install, upgrading webpacker to v5), nothing is helping at the moment. I've just commented out the javascript_pack_tag include to get it out of my way for the time being.

Really getting in the way of easily prototyping new apps, that's for sure. The state of webpacker+rails for new users is severely subpar.

@rossta
Copy link
Member Author

rossta commented Nov 15, 2020

@justinperkins Could you file an issue that describes the issue you’re seeing from Webpacker in more detail?, e.g., what flags you used to generate the app, error messages, Webpacker config, etc.

I understand the frustration but filing a bug report may be a more effective way to both get help and help improve the experience.

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

Successfully merging this pull request may close these issues.

'Webpacker can't find application' in a brand new project with extract_css: true extract_css default value should be true for test environment too
4 participants