rails asset pipeline production precompile - ruby-on-rails

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.)

Related

How use rails 5.1+ without yarn and webpack?

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

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 4 asset pipeline losing vendor assets

I can't seem to figure out how to make sprockets find assets in vendor/assets. I've ben been pushing off the problem by adding all of my assets to app/assets, but it's becoming way too cluttered.
I have read the documentation, and tried adding all of the following lines to my application.rb file.
config.assets.paths << "#{Rails.root}/vendor/assets/*"
config.assets.paths << "#{Rails.root}/vendor/assets/fonts"
config.assets.paths << "#{Rails.root}/vendor/assets/stylesheets"
config.assets.precompile << Proc.new { |path|
if path =~ /\.(eot|svg|ttf|woff)\z/
true
end
They work locally, but when I push them to the server, none of my vendor assets are there. I'm using capistrano for deployments, and I know that there were some issues with the upgrade. That could be the root of the problem, but I followed the documentation to get it deploying (almost) everything alright.
The problem turned out to be me being stupid, and quick to jump the gun on other problems. I dove too far down the rabbit hole, and lost sight of what was happening. I did not include the otf filetype in the regex, and it wasn't being included.
Facepalm
EDIT:
To clarify: all I had to do was change
if path =~ /\.(eot|svg|ttf|woff)\z/
to
if path =~ /\.(eot|svg|ttf|woff|otf)\z/
When you run rake assets:precompile are you manually setting the env to production?
The command should read:
RAILS_ENV=production rake assets:precompile

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.

Rails asset pipeline - JS and CoffeeScript

I have both .js and .coffee files in my /app/assets/javascripts/ folder. The .coffee files will not run unless I call rake assets:precompile, which is a pain because I have to do rake assets:clean and precompile them again whenever I make a change.
Also, the precompiled .js file is included in addition to the source files, which causes double event handlers and all that good stuff.
My understanding is that the coffeescript should be compiled to javascript upon each request if it's not precompiled, but it doesn't seem to be doing so. I can't find the compiled script loading in Firebug, and I don't see its behavior, at least.
My /config/application.rb has the following line configured:
# Enable the asset pipeline
config.assets.enabled = true
What else is there to check?
I am using Rails 3.2.3.
If you precompile on your local machine, then you can commit these generated assets into the repository and proceed with deployment. No need to compile them on production machine.
But it introduces a problem: now when you change source files (coffescript / scss), the app won't pick up the changes, because it will serve precompiled files instead. rake assets:clean deletes these precompiled files.
from https://stackoverflow.com/a/9335864/643500
What I usually do if I want the assets to precompile on the production server to pickup the new changes every build is just clean the assets - once of course unless you re-precompile them
rake assets:clean
When the changes are made and you don't want to precompile them every build do
rake assets:clean
rake assets:precompile

Resources