Webpacker throws application.css not found in manifest.json in *Rails 6* application
Asked Answered
Q

4

6

Currently we use Rails 6 for our application. It's working fine in production but in development mode it throws some errors. Please assist me.

This is how my application.js and application.css looks like

packs/application.js

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")

import '../stylesheets/application'
import './bootstrap_custom.js'
import './side_menu.js.erb'
//import './sweetalert.js'
import './business_hours.js'
import './admin.js'
import './services.js'
import './user_service.js' 

Packs/stylesheets/application.scss

@import './bootstrap_custom.scss';

$fa-font-path: '~@fortawesome/fontawesome-free/webfonts'; 
@import '~@fortawesome/fontawesome-free/scss/fontawesome';
@import '~@fortawesome/fontawesome-free/scss/solid';
@import './admin_dashboard.scss';
@import './side_menu.css';
@import './fonts.scss'; 
@import './login.css';

enter image description here

Quantize answered 22/10, 2019 at 14:19 Comment(0)
Q
5

After a long struggle I found a solution for this. Thanks @ahmed kamal.

Multiple packs with the same name but a different extension.

i.e: application.scss and application.js only the last one will make it to the webpack entry path configuration.

Solution:

 1. Rename stylesheets/application.scss into stylesheets/style.scss
 2. Import style.scss in application.js like this import '../stylesheets/style'
 3. config/webpacker.yml make below changes
      compile: true
      cache_manifest: true
      extract_css: true

If this solution not working for you, try th

rake assets:precompile

Thats all its started working fine for me

Quantize answered 22/10, 2019 at 15:22 Comment(1)
application.js and application.css is not a name conflict and cache_manifest: true is not a part of the fix.Energetic
D
3

I think you should import './stylesheets/application' with just one dot because the stylesheets directory is on the same level as your application.js.

Plus, I think it's recommended to only includes packs in the pack directory, and the rest will be one level before it, like this:

app/javascript
├── stylesheets
|   └── application.css
├── channels
└── packs
    └── application.js

and if you did that you should use two dots again '../stylesheets/application'

and make sure you configure the webpacker to extract CSS, in webpacker.yml :

extract_css: true
Distortion answered 22/10, 2019 at 15:4 Comment(4)
still facing the same issue. Please give me some alternative solution.Quantize
make sure that it recompiled the packs, try to restart your dev-serverDistortion
yeah I restart the server. I'm facing this issue in local machine tooQuantize
Did you configure the webpacker to extract CSS?, extract_css: true in webpacker.ymlDistortion
M
0

Check the directory names to make sure they aren't conflicting with Webpacker's output. In my case, Rails was unable to find application.css even though Webpacker had successfully compiled it.

// Webpacker log output
Asset                         Size       Chunks       Chunk Names
application.js                3.62 MiB   application  [emitted]              application
css/application-631a168a.css  332 KiB    application  [emitted] [immutable]  application

My relevant directory structure:

app/javascript
|——packs
|  └——application.js
└——src
   └——assets
      |——css
      └——scss
         └——application.scss

In my case, the problem was that I already had a directory named css, so when Rails went to look for the compiled css/application.css, it went to this directory instead of the one generated by Webpacker and wasn't able to find application.css.

Deleting the conflicting directory fixed it:

$ rm -rf app/javascript/src/css // change path appropriately
Mopboard answered 18/5, 2020 at 14:35 Comment(0)
T
0

As per recently commit in Rails, the official fix is:

  1. create an empty file in app/javascript/packs/application.css

    $ touch rebloom/app/javascript/packs/application.css
    
  2. change all environments from extract_css: false to extract_css: true in config/webpacker.yml

    $ ruby -pi -e "sub(/extract_css: false/, 'extract_css: true')" config/webpacker.yml
    
  3. change hmr and inline to true for the dev_server in development in config/webpacker.yml (NOTE: the following commands work assuming that you don't have custom settings. In case, please update the file manually)

    $ ruby -pi -e "sub(/hmr: false/, 'hmr: true')" config/webpacker.yml
    $ ruby -pi -e "sub(/inline: false/, 'inline: true')" config/webpacker.yml`
    

IMPORTANT

In case you followed other instructions, be sure you didn't change compile to true for the production mode in config/webpacker.yml This would negatively affect the performance of your application in production mode and it's not necessary. You just need to precompile the assets using

$ bundle exec rake assets:precompile
Travax answered 7/6, 2020 at 1:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.