I'm trying to debug why some assets are found and others not in the Asset Pipeline. i've tried a lot of obvious things (like typos, clearing tmp, clean/clobber).
Now I placed binding.pry right before where the image path is generated, and I'd like to view (print to the rails console / debugger) the name of every asset rails thinks is available.
How can I do that?
You can take a look at the manifest:
Rails.application.assets_manifest
# or just the files
# this is empty in some of my apps, no idea why, maybe cache or some
# lazy loading that I'm missing:
Rails.application.assets_manifest.assets
Or maybe loop through asset paths:
Rails.configuration.assets.paths.flat_map{ |path| Dir.glob("#{path}/*.{js,css}") }
Please note, this doesn't answer the question (how to view the available assets in the console/debugger), and therefore shall not be accepted as an answer, but it's a small start: showing how to view assets from the terminal:
rails webpacker:clean
rails webpacker:clobber
rails webpacker:compile
The last step will print all the available assets to the terminal.
Another way is to run bin/webpack from the terminal, and in the output will be each asset. Again, this doesn't provide the list from the console or debugger.
Related
My Rails app doesn't serve images at all.
image_url('picture.jpg')
# will result in url(http://localhost:3000/images/picture.jpg)
# but should be url(http://localhost:3000/assets/picture.jpg)
image_tag 'picture.jpg'
asset_url 'picture.jpg'
# will result in the same url / path as image_url()
Neither http://localhost:3000/images/picture.jpg nor http://localhost:3000/assets/picture.jpg exists, while http://localhost:3000/assets/images/picture.jpg does.
Here is a gist of my application.rb and development.rb: https://gist.github.com/maximski/1ccb75f6f89c02932239
I am in development environment and I don't want to precompile files manually. The app is pretty much much newly generated so the configuration is almost completely set on default.
This problem appear if images doesn't exists in app/assets/images directory. Check that app/assets/images/picture.jpg file is exists.
I've got somefile.js.coffee.erb file which is processed by Rails asset pipeline. My ERB code returns some string that cannot be parsed by Coffee which result in SyntaxError exception. I would like to peek into generated somefile.js.coffee file, or in general any intermediary file processed by asset pipeline.
I've tried to examine Sprockets with no luck:
environment = Sprockets::Environment.new
MyApplication::Application.config.assets.paths.each {|p| environment.append_path p}
rerb = environment['somefile.js.coffee.erb']
rerb.source #=> it's already preprocessed
Or to look into \tmp\cache\assets but there are also only preprocessed files, additionaly obscured by fingerprinted name.
Maybe there is a way to hook into asset-pipeline I have no idea how..
Why I need ERB? To generate client-side-model stubs with fields and validations matching Rails model using KnockoutJS (https://github.com/dnagir/knockout-rails extended -> https://github.com/KrzysztofMadejski/knockout-rails).
I am using Rails '~> 3.2.12', sprockets (2.2.2).
Edit: I've ended up injecting erb code in ### comments, to sneak-peak at generated code while coffeescript file is still compiling:
###
<%= somefun() %>
###
Altough I would suggest using #Semyon Perepelitsa answer as it produces coffee script file as it is seen by coffee compiler.
Just remove "coffee" from the file extension temporarily: somefile.js.erb. You will see its intermediate state at /assets/somefile.js as it won't be processed by CoffeeScript.
I wonder if you can put <% binding.pry %> just before the line and mess around till you get it right. Never tried during a compile and don't use coffeescript. In theory, it should work (or is worth a shot) so long as you put gem pry in your Gemfile and run bundle first.
The Rails Guide on the asset pipeline says you can use the yui-compressor on CSS with:
config.assets.css_compressor = :yui
However, I see no sign that it is actually using it. For one, thing, it makes no difference whether or not I have the yui-compressor gem installed or not. For another, compressed output is the same whether I have that line or not.
I put a little debug line into actionpack-3.2.3/lib/sprockets/compressors.rb in the registered_css_compressor method, and this is the result when the css is compiled: #<Sass::Rails::CssCompressor:0x007fdef9f9fee0>
So it appears that the config line is not being honored. Has anyone actually used this option?
Update
Looking in sass-rails shows that the selection is overridden:
if app.config.assets.compress
app.config.sass.style = :compressed
app.config.assets.css_compressor = CssCompressor.new
end
If I comment that out, then it actually attempts to start the yui compressor... I'm still checking the output to see if it is correct.
It truly is a bug in rails. I created a patch and pull request to fix it.
I recently migrated from Jammit to the Rails Asset Pipeline. Other than a few teething issues, everything has been working well.
However, I recently started getting some script errors in production, and realised that it's near on impossible for me to debug them. I had previously configured Jammit to retain linebreaks, but otherwise remove all white space in the javascript files. This was to ensure that should I see a runtime error, I would be able to locate the offending line and hopefully figure out what the problem is. With the Rails Asset Pipeline, and the default :uglifier compressor, it appears all whitespace is removed including line breaks, and as such my script errors do not tell me where in the code the problem was.
Does anyone know anyway to configure the Rails Asset Pipeline to retain line breaks so that code can be debugged?
Matt
Set in you production.rb:
config.assets.compress = false
and running rake assets:precompile won't uglify your assets.
UPD:
So-called compression means (among other stuff): remove line breaks and comments.
But if you want to obfuscate your variables and save some readability then use:
# in production.rb
config.assets.compress = true
config.assets.js_compressor = Uglifier.new(:beautify => true) if defined? Uglifier
Here see for more options: https://github.com/lautis/uglifier.
I am having an issue with the asset pipeline, I already have a resource / controller called assets. So i have changed assets.prefix option to "/externals".
config.assets.prefix = '/externals'
This simple dose not work unless i remove:
resources: assets
Then all works as expected.
I am not sure how to write a test to prove this but i have created a app to showcase it.
https://github.com/nodrog/asset-pipeline-issue
If you run the app, and visit '/products' all will work, then go to the routes file and change the variable create_bug to true.
We have looked into https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/helpers/rails_helper.rb, and then added a debugger to the asset_path method.
This method is not called, if you but a debugger in the javascript_include_tag method. And run method(:asset_path).source_location, it tells you it is calling the method from default routes not from the sprockets helper.
Any help would be greatly appreciated...
UPDATE:
I reported this to the rails people, and they fixed it. The fix is now in the master branch.
https://github.com/rails/rails/issues/3643#issuecomment-2775938