How can I get the Rails asset pipeline to produce source maps?

…衆ロ難τιáo~ 提交于 2019-12-04 07:57:27

问题


I'd like to have Rails produce source maps alongside the compiled coffeescript/minified JS, for better error logging. There doesn't seem to be comprehensive documentation on the net on how to do this, yet, though. Has anyone done this?

I'm on Rails 3.2 and Heroku.


回答1:


Rails supports source maps for minified JavaScript! Rails relies on Sprockets for asset compilation, and source maps support was added to Sprockets in this pull request.




回答2:


If you don't really want source-maps, but instead just want line numbers in coffee-script compile exceptions try this:

It used to be that just having coffee-rails in your Gemfile would produce exceptions with line numbers in the original coffeescript source. Then, they disappeared with a line-number-less exception. I did some digging, and I found that coffee-script-source 1.5.x gave line numbers in the compilation exceptions, while coffee-script-source 1.6.x did not. I believe is a bug, and I wouldn't be surprised if this was "fixed" in the future.

# Gemfile
gem 'coffee-rails', '~> 4.0.0'
  gem 'coffee-script-source', '~> 1.5.0' # 1.6 doesn't include line numbers in exceptions

Then you'll get exceptions like ('coffee-script-source', '~> 1.5.0')

Showing /Users/.../app/views/layouts/application.html.erb where line #12 raised:

SyntaxError: missing } on line 15
  (in /Users/.../app/assets/javascripts/app.js.coffee)

Instead of ('coffee-script-source', '~> 1.6.3')

Showing /Users/.../app/views/layouts/application.html.erb where line #12 raised:

SyntaxError: missing }
  (in /Users/.../app/assets/javascripts/app.js.coffee)



回答3:


Tested this. It works. https://github.com/markbates/coffee-rails-source-maps. However it makes your asset rendering much slower.




回答4:


This looks like it should work: http://alexspeller.com/2012/09/15/Source_maps_for_coffeescript_in_rails.html

Though, keep in mind the warning at the end:

Important Note: this rather brutal hack replaces the normal coffeescript compiler by shelling out to the CoffeeScriptRedux compiler, which is not in fact finished. This is just a proof of concept, you probably shouldn’t use it.

So I wouldn't recommend running this in production, but if you have a staging environment (also on Heroku, also with minified Javascript) it might be useful.



来源:https://stackoverflow.com/questions/14594717/how-can-i-get-the-rails-asset-pipeline-to-produce-source-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!