Rails 4 Asset Pipeline With Lib and Vendor Folders - ruby-on-rails

I was reading the documentation on Rails 4 and the Asset Pipeline when I got tripped up by this line:
If you are upgrading from Rails 3, please take into account that assets under lib/assets or vendor/assets are available for inclusion via the application manifests but no longer part of the precompile array
http://edgeguides.rubyonrails.org/asset_pipeline.html#asset-organization
What is the difference between being available for inclusion via the application manifest and being part of the precompile array? Does this mean that if I were to have a javascript file lib/vendor/cool_plugin.js I would get a 404 when I went to production even if I had //require cool_plugin in application.js?

From http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html:
In Rails 4.0, precompiling assets no longer automatically copies
non-JS/CSS assets from vendor/assets and lib/assets. Rails application
and engine developers should put these assets in app/assets or
configure config.assets.precompile.
so I believe you need to explicitly reference your vendor assets for them to get picked up during precompilation on production:
# production.rb
config.assets.precompile += %w( cool_plugin.js )

Related

Rails 3.2 Asset Precompile is skipping files

Stack
Ruby 1.9.3
Rails 3.2.12
Issue
We have precompilation issues with our application.
We're trying to remove config.server_static_files = true and config.assets.compile = true from our production configs, but are constantly missing assets whenever we precompiled. Even though these assets live in app/assets, while other files in the same directory are seen and compiled.
Ex:
app/assets/stylesheets/application.css
app/assets/stylesheets/some_slider_lib.css
app/assets/stylesheets/bootstrap.min.css
After precompiling
public/assets/application-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.css
public/assets/some_slider_lib-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.css
And the manifest.yml is also missing the bootstrap file among other assets.
Same thing with our vendor folder.
Attempts
The following is the only way that I've managed to precompiles everything.
config.assets.precompile += ["*.css", "*.scss", "*.js", "*.coffee"]
However, this feels like overkill. I thought all files within the asset pipeline would be compiled. Is this not what is suppose to happen?
Also tried rake assets:precompile --trace, but this was unhelpful.
If anyone could provide a little guidance, suggestions or more it would be deeply appreciated.
Thanks!

Rails assets not precompiling in production

I'm using Datetimepicker and Slider. I include them in my Gemfile
gem 'datetimepicker-rails', github: 'zpaulovics/datetimepicker-rails', branch: 'master', submodules: true
source 'https://rails-assets.org' do
# gem 'rails-assets-select2-bootstrap-css'
gem 'rails-assets-seiyria-bootstrap-slider'
end
In my application.js
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
//= require seiyria-bootstrap-slider
This works great in development, but when I run RAILS_ENV=production rake assets:precompile on the server (capistrano deploy or by hand) these, and others don't seem to get pulled in. Chrome complains specifically about these two first.
I know I could put the line Rails.application.config.assets.precompile += %w( *.js ) and then do a =javascript_include_tag :XXXX, but this defeats the purpose of sprockets/manifest right?
My understanding with sprockets/manifest is when I require it in my application.js it'll be included with the deploy so the client hits the server less.
Is there something I'm missing?
EDIT
Traced the problem to the uglifier gem. When I remove/comment out config.assets.js_compressor = :uglifier and recompile the JS starts working again.
Any thoughts?
this is because things work differently in development as compared to production.
few thing to note:-
No CSS or JS files will be available to your app through the asset pipeline unless they are included in other files OR listed in the config.precompile directive.Only application.css and application.js are available by default of all the CSS and JS files.
Every file that is not a Javascript file or CSS file that is in the app/assets folder will be copied by Rails into the public/assets folder when you compile your assets.So if you want to add some web fonts, you could make an app/assets/fonts/ folder and put your fonts in there, these will then be copied to public/assets/fonts folder when you compile your assets. Note that your app/assets/stylesheets/fonts.css.scss file that references those fonts will NOT be copied over unless you either added it to the config.assets.precompile directive or required it from your application.css
for config.assets.compile...If it is set to "true" (which it is by default in development) then Rails will try to find a Javascript or CSS file by first looking in the public/assets directory and if it can't find it, will hunt through your app/assets folder looking for the file. If it finds it in app/assets it will go ahead and compile on the fly and then serve this asset up.
The problem with this is that you don't notice it happening in development, then you commit everything and push to production and BOOM, everything is broken with 500 errors because production has config.assets.compile set to "false".This prevents the app from "falling back" and trying to load the file directly instead of using the asset pipeline.
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
Why don't you just have this set to "true" in every environment? Well, because it is sloooooow. And you don't want slow in production
Run RAILS_ENV=production rake assets:clean assets:precompile
check public/assets directory and verify that assets are compiled..if its not empty...that means asset pipeline is working but path is not correct.use asset_helpers to set the path of assets in css files.

Rails 3.2.8 - not loading assets (development)

I have searched around and can't rectify the situation based on the responses to other similar questions. I seem to have broken the asset pipeline somehow but can't seem to figure out how.
None of my assets are being loaded at all; rails seems to just be ignoring my manifest files. When I inspect my page in firebug, only the 'non-compiled' text inside my manifest files (both js and css) is being displayed - almost as if the asset pipeline wasn't even enabled.
I deleted the contents of public/assets since I was adding a new file to the manifest which seemed to start this behavior.
Current configuration:
environments/development.rb
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
application.rb
# Enable the asset pipeline
config.assets.enabled = true
config.assets.manifest = config.root
# Add extra assets for precompiling
config.assets.precompile += ['admin.js', 'admin.css']
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
I had the same issue. You can still use Ruby 2.1.2, just upgrade rails to latest 3.2.x version - it's 3.2.22
Gemfile:
gem 'rails', '3.2.22'
And run:
$ bundle update rails
The issue was caused by using an incompatible version of ruby. I was using version 2.1.2 which lead to unusual behavior from the sprockets gem (which powers the asset pipeline). This was fixed by downgrading to ruby 1.9.3. I haven't done any experimentation for fear of breaking it again but maybe this has been addressed in later versions of sprockets. I am using sprockets 2.1.3.
See: Rails 3.2.8 Application.js and Application.css are not working as expcted
Always remember two things when you want to handle Rails asset pipleline:-
if you want all you newly created js/css to autoinclude in application.js/css,pls add them in...
ELSE
IF you dont wont to add in manifest file(application.js/css) then use precompile directive in yuur environment file.
config.assets.precompile=%w(custom.css,custom2.js...etc)
so make sure you have either of these...
===========for example=-=============
suppose you have new css/js file:-
custom.css inside
app/assets/stylesheets/
so you can include in
application.css
// = require 'custom'
OR
use precompile directive:-
config.assets.precompile += %w( custom.css )
and then reference it like you always do
stylesheet_link_tag "custom"
same applies for js also
I just spent a few hours troubleshooting this issue (in 2017!) and it turned out I just needed to remove the gem active_reload. Updating rails and ruby was having no effect for me. The thread where I found that gold is here:
https://github.com/rails/rails/issues/2715

Why is Rails not precompiling javascripts and stylesheets inside the vendor directory?

In my project I have third party JavaScripts and Css files inside the vendor/assets/javascripts and vendor/assets/stylesheets directories. In development mode I can refer to the assets within those directories using the javascript_include_tag and stylesheet_include_tag methods.
However those scripts do not get precompiled when I run the rake assets:precompile command and subsequently can't be accessed in production.
Could someone please say why are assets inside the vendor directory get ignored by the rake task?
I was able to answer my own question.
This wasn't obvious to me but it does make sense. Except for the application.css and application.js file all other .js/.css files get ignored by the precompile rake task. Therefore as long as your scripts and stylesheets are referenced using //= require name_of_script convention inside the application.js/.css files they will get appended to the relavant file fine.
However if you don't want certain scripts to be placed inside the application.js/.css files, then you need to explicitly tell Rails that you still want them to be precompiled using this line inside the config/environment/production.rb or other relavant runtime environment config file:
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w( morris.min.js raphael.min.js )

Why rails respond with public/assets/application.js but not app/assets/javascripts/application.js in development mode?

I was testing turbo-sprockets gem and precompiled assets locally. Since then localhost:3000/assets/application.js always respond with public/assets/application.js file (not app/assets/javascripts/application.js).
Solution is to remove public/assets folder. Is it normal behaviour?
When you compile your assets with the production env, the generated assets are placed into public/assets. This is the first place Rails will search for assets : if you restart your app with development env, assets are already there and Rails won't search them in your app/assets directory anymore.
As stated in the documentation : "Any assets under public will be served as static files by the application or web server."
You're not supposed to precompile your assets on your development machine.

Resources