stylesheet_pack_tag not finding app/javascript/src/application.css in rails 5.1 with webpacker gem

主宰稳场 提交于 2019-12-04 22:30:51

In your application.js :

import "path to application.css"

I had this exact problem, on Rails 5.2 with Webpack's current version as of Dec 5, 2018 (4.2.x?) and just resolved it.

The fix for me was renaming application.css to anything else. I think there was a naming collision with Webpack.

So now my pack tags look like:

<%= stylesheet_pack_tag 'styles', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

I did NOT need to import the stylesheet inside application.js. This appears to make no difference for me.

OPTIONAL BONUS: I also renamed the app/javascript to app/webpack because I also thought it was confusing to run styles out of a Javascript directory (and learned from this youtube video that it could be done: https://www.youtube.com/watch?v=I_GGYIWbmg0&feature=youtu.be&t=32m35s). It was as simple as

  1. Renaming the directory to app/webpack
  2. Adjusting webpack.yml like so: source_path: app/webpack

Hope that helps.

When HMR is set to false the styles are inlined within the bundle so you won't get any css bundle. In your webpacker.yml file, setting hmr to true should fix your problem. I realize this is an old question. Relevant, though.

In "config/environments/production.rb":

config.public_file_server.enabled = true

Delete "public/assets" and "public/pack"

And run:

RAILS_ENV=production bundle exec rake assets:precompile

I had the same problem and solved it with running yarn add postcss-smart-import in console.

upd: rails 5.2, webpacker 3.5, default config.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!