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.
Related
I update my rails app to pass at 5.1.X to day,
but surprise, my config to minify JS and CSS dosn't work anymore...
config.assets.js_compressor = Uglifier.new(output: {comments: /^!/})
** Invoke yarn:install (first_time)
** Execute yarn:install Yarn executable was not detected in the system. Download Yarn at https://yarnpkg.com/en/docs/install
** Execute assets:precompile rake aborted! Uglifier::Error: Invalid assignment
If I comment out the line, it works, but JS is not minified and there is inaccessible on site... like all others assets, but there are present in cache in /public folder... I don't understand why asset pipeline doesn't work now?!
So how can I stay with classic asset pipeline and disabled yarn call ?
I have been looking into this and as it is a very new issue, I would think about posting also an issue on Uglifier github page. I do not understand why Uglifier is causing problem to yarn as asset pipeline files are in app while yarn will keep them in node_modules.
I believe the issue here is that Uglifier already works on production files (in fact that is a production setting) and the asset pipeline will be working with yarn. You can require files in your application.js and .scss from the yarn node_modules folder, they will be included in your assets.
in this guide you can find out how rails requires the different components
active_record/railtie
action_controller/railtie
action_view/railtie
action_mailer/railtie
active_job/railtie
action_cable/engine
rails/test_unit/railtie
sprockets/railtie
I gave a very quick look at this, could not find anything relevant to yarn in railties/lib/rails/all.rb so I decided to search the rails repository for yarn keyword and find out this file
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path.
# Rails.application.config.assets.paths << Emoji.images_path
<%- unless options[:skip_yarn] -%>
# Add Yarn node_modules folder to the asset load path.
Rails.application.config.assets.paths << Rails.root.join('node_modules')
<%- end -%>
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
so maybe you should search for this option\[:skip_yarn\]?
I wish you good luck
Fabrizio
The bootstrap and CSS work perfectly in development for our rails 4.2 app.
Here is the login page in development:
After deploying (ubuntu 14.1), assets are precompiled with:
bundle exec rake assets:precompile RAILS_ENV=production
However the bootstrap and css are completely not showing any effect on production server. Here is the same login page in production:
The assets precompile seems not having any effect on production. What's could cause bootstrap and css not showing in production? Is there way to verify that the assets precompile is successful?
UPDATE: files under app/assets
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
You might have to uncomment the above lines and add the js and css files you are compiling. Above line should be present in /config/environments/production.rb
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
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.)
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 )