webpacker: stylesheet_pack_tag not working in webpacker 4??
based on the usage mention here, you can link them by using stylesheet_pack_tag which mean you add <%= stylesheet_pack_tag 'application' %>
under views>layout>application.html.erb
but then when I view-source the web, there is no css loaded inside the <head>
.
then to fixed this issue, i need to import import '../src/application.css'
into packs/application.js
to make the css file to work and load.
my question, is this how it should work? if so, what is the purpose to have <%= stylesheet_pack_tag 'application' %>
? if you commented out this line, it is still work.
I have create the sample app for better picture about this
or maybe i miss some point somewhere else?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 25
- Comments: 22 (11 by maintainers)
Commits related to this issue
- Include Pages Stylesheet Pack Tag Per https://github.com/rails/webpacker/issues/2059#issuecomment-518485523 you need to include a stylesheet_pack_tag if webpacker.yml has its emit_css_file setting se... — committed to MAPC/trailmap3 by mzagaja 5 years ago
- Disable webpacker stylesheet errors in production Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing. https://github.c... — committed to mitre/vulcan by deleted user 3 years ago
- Disable webpacker stylesheet errors in production Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing. https://github.c... — committed to mitre/vulcan by deleted user 3 years ago
- Use webpack extract_css: true all of the time Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing. https://github.com/r... — committed to mitre/vulcan by deleted user 3 years ago
- Use webpack extract_css: true all of the time (#204) Webpacker has a nasty habit of not compiling css files for components and then causing an error in production when they are missing. https://gith... — committed to mitre/vulcan by deleted user 3 years ago
This sounds to me like misunderstanding how webpack works (which is reasonable, because it’s incredibly confusing).
Webpack is used for building many things, and the way we tell webpack what to load are
import
orrequire
statements. This includes css files, images, and everything else. When you doimport '../src/application.css'
, you’re telling webpack include application.css in the build. This does not mean it’s going to be compiled into your javascript, only that webpack now knows that you want to compile this file. How that file compilation is handled is depending on how your loaders (css-loader
,sass-loader
,file-loader
, etc… ) are configured.When you do
<%= stylesheet_pack_tag 'application' %>
, that’s a run-time inclusion from Rails, that has nothing to do with whether webpack has built the stylesheets or not. All that is doing is saying “if you have a pack namedapplication-*.css
, include it here”. If webpack didn’t build a separate pack of css, then this statement will have nothing to load because no stylesheets were compiled.You didn’t mention what framework you’re using, and how this works is dependent on that. As far as I remember,
stylesheet_pack_tag
is only used for Vue becausevue-loader
automatically collects all of the stylesheets into one file. I am not sure if any other frameworks require this (I don’t think React does, for example).It is perfectly normal and expected to
import
orrequire
your stylesheets like you’re doing in your entry point file.For what it’s worth, I’ve noticed that while
stylesheet_pack_tag
does not render anything in development, it does render a css file in production.My guess is that this is due to the
extract_css
setting inconfig/webpacker.yml
.I saw that its covered in the document as
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.
So what is the conclusion here?
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.
I’ve also checked https://github.com/rails/webpacker/blob/master/docs/css.md and many other issues. Maybe this should be clarified more?
EDIT: I got it working in production by doing
stylesheet_pack_tag 'index'
, the same as the js file that includes the scss file. Here’s the full solution:I’m not sure if the scss file name matters. I think what matters is that
stylesheet_pack_tag
includes the same name like the js file nameindex.js
.I would totally agree, but there are at least two cases we’ve encountered where reading the CSS outside of precompilation is necessary, and they both involve feeding CSS into a separate “packager”. It’s also a bit frustrating as it was, in a sense, within the scope of Sprockets, from which we’ve had to move away because of waning community support.
We use
wicked_pdf
to generate PDFs from HTML documents, and having the CSS be external to the fed-in HTML creates issues.wicked_pdf
bundles helpers that do effectively what you’re describing in item (2) or queries thewebpack-dev-server
if one’s available.We also need to feed CSS into
premailer
manually as it converts CSS into inline email-safe styling. To grab that CSS, much likewicked_pdf
, we branch on the current environment and grabFor both items, this requires that we have the
webpack-dev-server
running during tests, which we haven’t been doing up to now, but might need to.I think it would be nice to have a centralized way to grab CSS for a given manifest entry for cases like these. Do you think a feature request issue or PR would be welcome?
Anyway, thanks for your help @jakeNiemiec 🙂 Hope you are having a wonderful day!
From https://github.com/rails/webpacker/issues/2062#issuecomment-484942469
The build chain works like this:
(if extract_css == true) -> application stylesheet -> MiniCssExtractPlugin -> application.css -> stylesheet_pack_tag 'application'
(if extract_css == false) -> application stylesheet -> style-loader -> application.js (loads css in head)
MiniCssExtractPlugin does not support
webpack-dev-server
orHMR
. statement from dev: https://github.com/webpack-contrib/mini-css-extract-plugin/issues/296#issuecomment-430679174Good news time: a PR was just merged for this feature https://github.com/webpack-contrib/mini-css-extract-plugin/pull/334, but it won’t come to webpacker unless somebody creates a PR here.
It’s shoddy, but this gets me by for the time being:
It’d be really nice if there was a helper around this for the pdf/mailer use-cases.
You probably have
extract_css: true
in yourwebpacker.yml
. This removesimport '../scss/index';
and generates the equivalent style sheet. You do need to addstylesheet_pack_tag
yourself.@eronisko when you are in development (depending on settings), the styles are being streamed in via WebSocket and injected into the
<head>
.I tried
open
but it would complain about self signed cert invalid So I used net/httpEdit 1: Updated code for
current_webpacker_instance.dev_server.running? == false
after some testing It does skip the conditional insideasset_pack_path
Thanks @HarrisonB, today is much better.
Rendering emails and PDFs are definitely a
webpack-dev-server
use-case I hadn’t considered. There are serious hurdles that would need to be cleared before it could work sincewebpack-dev-server
uses JS to juggle the styles. I am not sure how it could work in the context of PDFs & emails. You would probably need to run./bin/webpack
withextract_css: true
to get what you need. (I’m more from the webpack side of the equation, can you tell? 🙂)Most definitely! It is especially needed since rails@6 is out with webpacker as a default.
@jakeNiemiec Sorry, I was unclear. That would include a
<link>
tag in the<head>
referencing a separate CSS file. However, if I were to want a<style>
tag with the CSS as a child text node, I cannot achieve that via Webpacker (without client-side JS execution)?It is possible, just do the following and it should work in a default install:
<%= stylesheet_pack_tag 'myStylePack' %>
in<head>
Here is the mechanism:
https://github.com/rails/webpacker/blob/41d79c96187154b5485289e8c3c42428dd819bfc/package/utils/get_style_rule.js#L40-L44