Rails Assets: Custom Application.css.scss - ruby-on-rails

I know that in order to precompile your assets you do the following
bundle exec rake assets:precompile
which will take your application.css.sass in app/assets/styesheets and generate application-xxxxx.css in your public/assets directory
My question, what if you have a custom application_custom.css.scss How to precompile it to be used by production?

You can include that file in the application.css.scss manifest file.
Add the following line in your app/assets/stylesheets/application.css.scss:
*= require application_custom
You can also specify additional assets to precompile by adding them to config.assets.precompile += %w( ... ) in your environment configuration, e.g. config/environments/production.rb

Related

precompile coffeescript files ( Rails 4 )

In my Rails 4 app I have a users.js.coffee file in app/assets/javascripts. It compiles and works fine in development. But when I deploy to heroku it's not working. According to Railsguides:
The matcher (and other members of the precompile array; see below) is applied to final compiled file names. This means anything that compiles to JS/CSS is excluded, as well as raw JS/CSS files; for example, .coffee and .scss files are not automatically included as they compile to JS/CSS.
So I added the following line to my config/environments/production.rb
config.assets.precompile += %w ( users.js.coffee )
It's still not forcing the file to be precompiled. Do you know how I may force Rails to precompile it (I use RAILS_ENV=production bundle exec rake assets:precompile to precompile)?
Thanks a lot.
Your config.assets.precompile should contain users.js, not users.js.coffee.

Rails Precompiling Custom Assets

I have a custom application_admin.css.scss under app/assets/stylesheets
and I have this line in my config/environments/production.rb file
config.assets.precompile += [%w(application_admin.css)]
When I run bundle exec rake assets:precompile the custom stylesheet doesn't compile
I came across this post but I already have what they suggested.
Rails Assets custom manifests are not precompiling
What else should I check?
First, you don't need to use both [] and the %w(). Try just:
config.assets.precompile += %w( application_admin.css )
which is the equivalent of:
config.assets.precompile += ['application_admin.css']
Second, since you are precompiling for your production environment you want to run:
RAILS_ENV=production bundle exec rake assets:precompile
Just:
bundle exec rake assets:precompile
runs it for your development environment by default. You will want to run this in each environment you want to precomplie your assests in.

rails asset pipeline production precompile

I have a directory projName/vendor/assets/bootstrap/css/
I am in production mode.
production.rb contains: config.assets.precompile << /(^[^_\/]|\/[^_])[^\/]*$/
when I run rake assets:precompile
I get
projName/public/assets/css/ but I want projName/public/assets/bootstrap/css/
I do not understand why the bootstrap directory is not there.
Actually all the top level directories under vendor/assets and app/assets are missing in public assets/
Compiled assets are written to the location specified in config.assets.prefix. By default, this is the public/assets directory.
To understand this you have to first understand what precompiling is and does. Let me explain
When you run (a rake task)
rake assets:precompile
it will create a public folder inside your application folder which will compile all your asset manifests (ie. your application.css and application.js)
How exactly does this happen ?? -> Rails comes bundled with a rake task which will compile all this. That rake task is what is shown above.
Compiled assets are written to the location specified in config.assets.prefix. By default, this is the public/assets directory.
The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files (this will include all image assets automatically) from app/assets folders including your gems.
And thats exactly what that regex means (to include everything in your app/assets folder), you can also explicitly provide it as shown in the answer above.
Hope this helped.
Here are few links for your reference
http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
http://dennisreimann.de/blog/precompiling-rails-assets-for-development/
What does that regex mean? If you want to precompile everything, try this.
config.assets.precompile = ['*.js', '*.css']
Actually, if you want to precompile everything, try this:
def precompile?(path)
%w(app lib vendor).each do |asset_root|
assets_path = Rails.root.join(asset_root, 'assets').to_path
return true if path.starts_with?(assets_path)
end
false
end
# Precompile all assets under app/assets (unless they start with _)
Rails.application.config.assets.precompile << proc do |name, path|
starts_with_underscore = name.split('/').last.starts_with?('_')
unless starts_with_underscore
path = Rails.application.assets.resolve(name).to_path unless path # Rails 4 passes path; Rails 3 doesn't
precompile?(path)
end
end
(Based on the code in the Rails Guide.)

Precompiling a .css file

I have a board controller and I have made a board.css file for it.
When I push to heroku I'm getting an error saying board.css is not precompiled. Based on this question I have added
config.assets.precompile += %w( *.css *.js )
to my production.rb file.
This still has not fixed it. What am I missing here?
Open up config/environments/production.rb and make sure the following option is set to true:
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
Then run:
rake assets:precompile --trace RAILS_ENV=production
Source
Do you have an assets manifest in your /assets folder?
Default manifest file are /assets/application.js and /assets/application.css.
A manifest file describes all the resources will be precompiled and looks like this:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree ./dir
*= require_tree .
*/
This CSS manifest precompiles itself, all css files in assets/dir and /assets folders.
So make sure you have a manifest file and it requires board.css with a matching path.
When you set config.assets.precompile += %w( *.css *.js )
in production.rb you are telling Rails that all files in /assets folder are manifest file.
IMHO this is not correct and you have to add only custom manifest files.
Default manifests are already included:
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
About others solutions there's something to say. If you set:
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
It means that you are not forced to have precompiled assets.
The trick works because your app will not raise an error, but you still get an app without precompiled assets and you'll pay this in page load time. For this reason this option should always be disabled in production environment.
So when you enable it, calling rake assets:precompile will not guarantee you are going to use a precompiled assets.
Finally remember that Heroku always run rake assets:precompile when you deploy your app, so unless you are running your app locally with production environment you don't need to manually precompile your assets.

How do I precompile assets in rails in development environment?

I want to manually precompile some assets in my dev environment (to check the content of the compiled js and css files) from the command line.
I am running
RAILS_ENV=development bundle exec rake assets:precompile
but my js and css are not compiled in the public/assets folder, only the images are. Any idea why that may be happening?
Try adding this to your config/environments/production.rb :
config.assets.precompile += %w( *.css *.js )

Resources