-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 not working in webpacker 4?? #2059
Comments
having the same issue. |
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 When you do You didn't mention what framework you're using, and how this works is dependent on that. As far as I remember, It is perfectly normal and expected to |
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. |
@bbugh And yeah, maybe because I missing a loader in the app, so it doesn't compile my scss file. BTW, thanks for your input. |
From #2062 (comment) The build chain works like this:
MiniCssExtractPlugin does not support Good news time: a PR was just merged for this feature webpack-contrib/mini-css-extract-plugin#334, but it won't come to webpacker unless somebody creates a PR here. |
still not working upd.
in my template i have:
|
For what it's worth, I've noticed that while My guess is that this is due to the |
@eronisko when you are in development (depending on settings), the styles are being streamed in via WebSocket and injected into the |
So what is the conclusion here? I'm using webpacker with react and, by importing the scss in my Do I need to add 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
I'm not sure if the scss file name matters. I think what matters is that |
You probably have |
Per rails/webpacker#2059 (comment) you need to include a stylesheet_pack_tag if webpacker.yml has its emit_css_file setting set to true.
What does this and/or how is this configured? |
See this updated comment where I spell out both ways it can go: #2059 (comment)
When webpack-dev-server is running (depending on HMR/dynamic loading settings), your chunks come over a socket allowing faster development. It can be sometimes difficult to set up.
This is handled via |
@jakeNiemiec Thank you very much for your comment! So if I understand it correctly, there's currently no way to have the CSS in the head without some client-side JS execution? |
It is possible, just do the following and it should work in a default install:
Here is the mechanism: webpacker/package/utils/get_style_rule.js Lines 40 to 44 in 41d79c9
|
@jakeNiemiec Sorry, I was unclear. That would include a |
<%= File.read(Webpacker.manifest.lookup('iliketomakeemailsandpdfs.css'))&.html_safe %>
|
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.
For both items, this requires that we have the 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! |
Thanks @HarrisonB, today is much better. Rendering emails and PDFs are definitely a
Most definitely! It is especially needed since rails@6 is out with webpacker as a default. |
It's shoddy, but this gets me by for the time being: module WebpackerHelper
def inline_stylesheet_pack_tag(name)
file_name = name + ".css"
content_tag(:style) do
if current_webpacker_instance.dev_server.running?
open(inline_asset_url(file_name)).read.html_safe
else
File.read(File.join(Rails.root, "public", asset_pack_path(file_name))).html_safe
end
end
end
private
def inline_asset_url(name)
dev_protocol + current_webpacker_instance.config.dev_server[:public] + asset_pack_path(name)
end
def dev_protocol
current_webpacker_instance.config.dev_server[:https] ? "https://" : "http://"
end
end It'd be really nice if there was a helper around this for the pdf/mailer use-cases. |
I think it could use a general way to resolve asset names. The equivalent of this bit: if current_webpacker_instance.dev_server.running?
open(inline_asset_url(file_name)).read.html_safe
else
File.read(File.join(Rails.root, "public", asset_pack_path(file_name))).html_safe
end |
I tried require "net/http"
require "openssl"
require "uri"
def inline_stylesheet_pack_tag(name)
file_name = name.end_with?(".css") ? name : name + ".css"
tag.style do
if current_webpacker_instance.dev_server.running?
uri = URI(inline_asset_url(file_name))
::Net::HTTP.start(
uri.hostname,
uri.port,
use_ssl: uri.scheme == "https",
# Will be self signed cert
verify_mode: OpenSSL::SSL::VERIFY_NONE,
) do |http|
http.request_get(uri.path)
end.body.force_encoding("UTF-8")
else
::File.read(
::File.join(
::Rails.public_path,
# Using `asset_pack_path` generates unnecessary info
# like host and protocol
current_webpacker_instance.manifest.lookup!(file_name),
),
)
end.html_safe
end
end Edit 1: Updated code for |
Is this still an issue? Feel free to reopen if needed |
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.
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.
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.
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.
based on the usage mention here, you can link them by using stylesheet_pack_tag which mean you add
<%= stylesheet_pack_tag 'application' %>
underviews>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'
intopacks/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?
The text was updated successfully, but these errors were encountered: