javascript 如何获取 Rails 资产管道以生成源映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14594717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:03:04  来源:igfitidea点击:

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

javascriptruby-on-railsherokuasset-pipelinesprockets

提问by Vincent Woo

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?

我想让 Rails 与编译后的 coffeescript/minified JS 一起生成源映射,以便更好地记录错误。不过,网上似乎还没有关于如何执行此操作的全面文档。有没有人做过这个?

I'm on Rails 3.2 and Heroku.

我在 Rails 3.2 和 Heroku 上。

回答by jsanders

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

Rails 支持缩小 JavaScript 的源映射!Rails 依赖Sprockets进行资产编译,并且在这个 pull request 中向 Sprockets 添加了源地图支持。

回答by whitehat101

If you don't really want source-maps, but instead just want line numbers in coffee-script compile exceptionstry 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-source1.5.x gave line numbers in the compilation exceptions, while coffee-script-source1.6.x did not. I believe is a bug, and I wouldn't be surprised if this was "fixed" in the future.

过去,在您的 Gemfile 中仅使用 coffee-rails 会在原始咖啡脚本源代码中产生带有行号的异常。然后,它们以无行号的异常消失了。我做了一些挖掘,我发现coffee-script-source1.5.x 在编译异常中给出了行号,而coffee-script-source1.6.x 没有。我相信这是一个错误,如果将来“修复”它,我不会感到惊讶。

# 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')

然后你会得到像 ('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')

而不是 ('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)

回答by cryo28

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

测试了这个。有用。https://github.com/markbates/coffee-rails-source-maps。但是,它会使您的资产渲染速度变慢。

回答by Alex Ghiculescu

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

这看起来应该有效: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.

重要提示:这个相当残酷的 hack 通过炮击 CoffeeScriptRedux 编译器来替换普通的 coffeescript 编译器,这实际上还没有完成。这只是一个概念证明,您可能不应该使用它。

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.

所以我不建议在生产中运行它,但如果你有一个临时环境(也在 Heroku 上,也有缩小的 Javascript),它可能会有用。