1

I have the following pack file:

// app/javascript/packs/styles.js 
import 'bootstrap/dist/css/bootstrap'

Unless I set extract_css: true in config/webpack.yml I do not see how to get these styles to load.

If I set extract_css: true and include <%= stylesheet_pack_tag 'styles' %> in the layout everything seems to work.

But then what is the point of extract_css: false why would you ever NOT want this? Because its the default webpacker configuration I feel as if I am missing something important.

2 Answers 2

3

With extract_css: false, the styles are provided by the webpack js runtime as blob urls and injected into the document <head> inline (assuming you have a corresponding javascript_pack_tag).

One use case for this approach in development is to better support hot module reloading for CSS.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you the clue here was that you have to have the corresponding javascript_pack_tag. so If I understand you have both a javascript_pack_tag AND a stylesheet_pack_tag, BUT the stylesheet_pack_tag is ignored by the system in development.
Yep and you can verify the stylesheet_pack_tag behavior here: github.com/rails/webpacker/blob/…
0

I'm not 100% sure, but I think if you extract the css, webpack will generate that standalone css file so you can link it. If you don't extract it, the CSS is bundled insde the js file and webpack will process it when the page loads and inserts the CSS in a style tag in the head tag of the document.

To extract the CSS from the js file it uses the mini-css-extract plugin.

https://webpack.js.org/guides/asset-management/#loading-css

In some cases you may want to keep the css on the bundle if you want to have all the js and css with only one request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.