Remove /assets directory completely from Rails 6 app - ruby-on-rails

I'm setting up a new Rails 6 app and moved all of my assets over to webpack. I now want to delete my empty app/assets directory, but doing so means Rails won't boot because Sprockets can't find my missing manifest.js file:
> Expected to find a manifest file in `app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError)
But did not, please create this file and use it to link any assets that need
to be rendered by your app:
Example:
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
and restart your server
I haven't found any good solutions for completely removing Sprockets. Creating an empty assets directory just to hold a manifest file I'm no longer using doesn't seem like the optimal solution here. Yes, it will work, but what's the point of moving to webpack if Rails still forces us to use Sprockets and have a JS manifest file?
Has anyone successfully removed their assets directory completely? Thanks for your help!

I just removed app/assets from my apps, getting similar error. Solve by:
Change this line in config/application.rb
from
require 'rails/all'
to
require 'rails'
# Pick only the frameworks we want:
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_mailbox/engine'
require 'action_text/engine'
require 'action_view/railtie'
require 'action_cable/engine'
# require "sprockets/railtie"
require 'rails/test_unit/railtie'
Remove all config.assets.* in config/environments/*
Delete config/initializers/assets.rb

Related

Sprockets processing image assets from a gem

I'm migrating from sprockets 3 to sprockets 4 and v4 seems to ignore assets from gems. Those were automatically handled by v3, but I can't seem to find anything about porting this behaviour.
For example there's gem mediaelement_rails which includes mediaelement_rails-0.8.2/app/assets/images/mediaelement_rails/skipback.png
In sprockets v3 it was automatically included during asset compilation, but now, even with manifest:
//= require mediaelement_rails
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
The skipback.png is not included.
I could do this very explicitly: query the gem path and add it to the assets paths. But is there a way to do this automatically similar to v3?
Sprockets v4 no longer automatically includes gem resources.
To include gem resources in your assets, you must explicitly register the gem paths that contain the resources. This can be done using the configuration: config.assets.paths in your config/application.rb file.
For example, to include the mediaelement_rails gem image resources in your assets, you could add the following to your config/application.rb file:
config.assets.paths << Rails.root.join('vendor', 'assets', 'mediaelement_rails', 'images')
This will add the vendor/assets/mediaelement_rails/images directory to the assets path.
If the gem you're using isn't following the standard naming conventions for its assets (i.e. putting your assets in subdirectories named after javascripts, stylesheets, or images), you'll need to explicitly register those directories. using a config.assets.precompile setting in your config/application.rb file.
Here they talk a little about inserting paths that don't follow a pattern
Here, the rails documentation also already shows something about this

Rails Assets Precompile in production mode error

When I run rake assets:prcompile RAILS_ENV=production, I am getting below error.But if i run in development mode it runs fine.
rake assets:precompile RAILS_ENV=production
rake aborted!
Uglifier::Error: fs redeclared
Environment
Rails 5.0.7
Ruby 2.4.1
Could someone helps to resolve this one
Got the issue.In my application.js file have below codes
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
As per functionality of require_tree, it will include current path's all js files recursively. Refer the link https://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives
In JavaScript files, Sprockets directives begin with //=. In the above case, the file is using the require and the require_tree directives. The require directive is used to tell Sprockets the files you wish to require. Here, you are requiring the files rails-ujs.js and turbolinks.js that are available somewhere in the search path for Sprockets. You need not supply the extensions explicitly. Sprockets assumes you are requiring a .js file when done from within a .js file.
The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file. You can also use the require_directory directive which includes all JavaScript files only in the directory specified, without recursion.
In my application , Gruntfile.js file was declared in two js folders.So, when I was doing the precompile,as the file was declared in two js , it throws error when it tried to compress that file

rails assets in production not served (yet another assets issue)

I'm using
apache 2.2.15
passenger 5.1.2
rails 4.2.6
ruby 2.3.3
In the rails log, I get
(No route matches [GET] application-xxxxx.js
as well as
(No route matches [GET] application-xxxxx.css
I can find the assets in the public/assets directory.
My manifests:
app/assets/stylesheets/application.css.less:
/*
*= require jquery-ui
*= require font-awesome
*= require bootstrap_and_overrides
*= require awesome-bootstrap-checkbox
*= require lists
*= require mailgroups
*= require general
*= require colors
*= require print
*= require loader
*= require tooltips
*= require_self
*= require_tree .
*/
app/assets/javascripts/application.js
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery-ui
//= require twitter/bootstrap
//= require bootstrap
//= require turbolinks
//= require twitter/bootstrap/rails/confirm
//= require_self
//= require_tree .
and I set (following other answers here on SO) in config/production.rb
config.serve_static_files = true
in app/views/layouts/application.html.erb
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => "reload" %>
<%= javascript_include_tag "application", "data-turbolinks-track" => "reload" %>
I'm lost, thanks for your help.
UPDATE: I don't use Capistrano, just deploying manually.
When running Rails.application.config.assets.paths in the rails console, I get the following:
=> ["/var/www/myappname/app/assets/fonts",
"/var/www/myappname/app/assets/images",
"/var/www/myappname/app/assets/javascripts",
"/var/www/myappname/app/assets/stylesheets",
"/var/www/myappname/vendor/assets/fonts",
"/var/www/myappname/vendor/assets/images",
"/var/www/myappname/vendor/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-confirm-1.0.6/vendor/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/local_time-1.0.3/app/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/font-awesome-rails-4.7.0.1/app/assets/fonts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/font-awesome-rails-4.7.0.1/app/assets/stylesheets",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-fontawesome-0.5.1/vendor/assets/fonts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-fontawesome-0.5.1/vendor/assets/stylesheets",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-bootstrap-3.3.5.0/app/assets/fonts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-bootstrap-3.3.5.0/app/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/less-rails-bootstrap-3.3.5.0/app/assets/stylesheets",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/fonts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/images",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/app/assets/stylesheets",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/twitter-bootstrap-rails-3.2.0/vendor/assets/stylesheets",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-turbolinks-2.1.0/vendor/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-ui-rails-6.0.1/app/assets/images",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-ui-rails-6.0.1/app/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-ui-rails-6.0.1/app/assets/stylesheets",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/jquery-rails-4.2.2/vendor/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/coffee-rails-4.0.1/lib/assets/javascripts",
"/var/www/myappname/vendor/bundle/ruby/2.2.0/gems/turbolinks-source-5.0.0/lib/assets/javascripts",
#<Pathname:/var/www/myappname/app/assets/fonts>,
#<Pathname:/var/www/myappname/app/views>,
#<Pathname:/var/www/myappname/app/assets/stylesheets>,
#<Pathname:/var/www/myappname/app/assets/javascripts>]
my config/initializers/assets.rb already contains this:
Rails.application.config.assets.precompile += ['.svg', '*.eot', '*.woff', '*.woff2', '*.ttf', '*.otf', '*.js', '*.css']
this is my advise.
I add the following resources that may help in finding a solution.
https://launchschool.com/blog/rails-asset-pipeline-best-practices
https://railsapps.github.io/rails-javascript-include-external.html
https://reinteractive.com/posts/116-12-tips-for-the-rails-asset-pipeline
I read all the guides that can be found by searching rails asset pipeline, but I will not link them all
My advice is to check the fingerprint of the application.css and application.js both with the Chrome Developer Tools (by going in any screen element, opening the developer toolbox with f12 and checking any style of any div or any js file). You will need to check which fingerprint version of the file is used, then open that version with the text editor and check what is loaded in the fingerprint version of the file.
If your file (for ex. user.js) is loaded at the bottom of the manifest, you should easily find it at the bottom of your fingerprinted application.js.
If you want to update this file you can run
rake assets:precompile
This will precompile assets only in development, for production you need to specify the environment. It will change that fingerprinted file application.css and application.js with the editing you have done.
RAILS_ENV=production bundle exec rake assets:precompile
You can also check the file at the following address localhost:3000/assets/yourjsfile.js or check the fingerprint version by following localhost:3000/assets/application-yourfingerprint.js
As you can see, many files are included in that public/assets folder, you can delete them with rake assets:clean or -rf public/assets and then run rake assets:precompile, it will precompile again assets.
If nothing is included, then your application.js is not being loaded. You may try the following as I read on different posts:
Include the file in the config/initializers/assets.rb with the following line of code:
Rails.application.config.assets.precompile += %w( user.js )
Start the rails c and run Rails.application.config.assets.paths
This command will show in red the paths that are automatically included, for example those inside App, Lib and Vendor, in green those that you included by adding inside config/application.rb the follwing code to add new path
config.assets.paths << Rails.root.join("vendor","assets", "fonts")
Another error i made in the past was not using the correct sprockets syntax, you can check it at the following link ( i was using require_user instead of require user):
https://github.com/rails/sprockets#sprockets-directives
Another problem was that I had under App/assets/javascript two files named user (user.coffee and user.js). The first to be loaded was user.coffee which was empty.

Rails asset pipeline require

In rails to require a file in the vendor directory I know that I can do this in my application.j file.
//=require vendor/assets/javascripts/me.js
Am wondering how does one require the entire javascript folder in the vendor directory
//= require_tree ../../../vendor/assets/javascripts/include/.
See http://guides.rubyonrails.org/asset_pipeline.html#asset-organization
Try
//=require me
Vendor should be loaded by default
Assets are not namespaced
You might need to restart your dev server after adding to vendor

Rails and codekit - how to disable rails coffescript compilation

I'm using rails 3.2, but I'm compiling my coffee files with CodeKit.
I still want my coffee files to live inside 'assets', but each time I restart rails, it finds them in there and tries to compile them itself.
My files live in /assets/cafe/myscript.coffee, and codekit compiles them into /assets/javascripts/myscript.js
The coffee-rails gem is already commented out in my gemfile (when rails tries to (re) compile it it gives "throw Error("LoadError: no such file to load -- coffee_script)" - though I really dont want it to even try compiling.
Setting "config.assets.compile = false" in application.rb results in "application.js isn't precompiled"
you should probably just configure your sprockets manifest to not require the whole tree.
edit app/assets/javascripts/application.js
typically it looks like this:
//= require jquery
//= require jquery_ujs
//= require_tree .
change that to
//= require jquery
//= require jquery_ujs
//= require myscript
An (inferior) workaround is to put my coffeescripts inside "App" rather than "assets" (so one more branch up the tree.)
This sucks because it's not where they should go, but at least it does put it outside rails' stalker-tendencies to find coffeescript files anywhere in assets and try to compile them.

Resources