通过链轮在 Ruby on Rails 中进行源映射 [英] Source maps in Ruby on Rails through sprockets

查看:40
本文介绍了通过链轮在 Ruby on Rails 中进行源映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我正在开发的 rails 3.2 应用程序上添加源映射支持.据我所知,Sprockets 不支持生成源映射,从其 github 页面看来,该功能计划用于 4.0.我正在使用链轮 2.2,我认为猴子补丁是唯一的出路.主链板模块下的模块 Processing 提供对 js_compressor 函数的访问,该函数可以被修补以生成单个文件的源映射.但是,我不知道如何在 JS 文件组合时添加它.我使用 Uglifier 2.4 作为压缩器.

I'd like to add source map support on a rails 3.2 application I am working on. As far as I know, generating source maps is not supported by Sprockets and from its github page it looks like the feature is planned for 4.0. I am working with Sprockets 2.2 and I think monkey patching is the only way to go. The module Processing under the main Sprockets module gives access to the js_compressor function which can be patched to generate source map for a single file. But, I don't know how to add this when the JS files combine. I am using Uglifier 2.4 as the compressor.

该项目混合了 CoffeeScript、JS 和 EJS 文件.所以,我认为这就是链轮将它们编译在一起的方式.首先,它将 Coffeescript 和 EJS 转换为 JS,然后使用 js_compressor 压缩单个文件,然后将它们连接成组.现在,由于多个文件的源映射组合到同一个文件是一个文件.因此,一旦连接完成,我将需要稍微更改编译过程并使 js_compressor 运行文件.那么,有人可以帮忙解决这个问题吗?甚至解释 sprockets 编译过程以及使用的模块和涉及的功能也会有很大帮助.目前我不关心为 CoffeeScript 代码制作源映射文件.甚至映射到他们转换后的 JS 文件也可以.

The project has a mixture of CoffeeScript, JS and EJS files. So, I think this is how sprockets would be compiling them together. First, it would convert Coffeescript and EJS to JS, then use the js_compressor to compress individual files and later concatenate them in groups. Now, as the source map for multiple files combined to the same file is a single file. So, I will need to change the compilation process somewhat and make the js_compressor run over the files, once the concatenation is finished. So, can anyone help out with this? Even explaining the sprockets compilation process and the modules used and functions involved would be of great help. I don't care about making source map files for the CoffeeScript code at present. Even mapping to their converted JS files would do.

另外,如果有一些宝石可以帮助解决这个问题,我想补充一下.

Also, would like to add if there is some gem which can help with this it would be most welcome.

推荐答案

Rails 4 也没有源映射.

Rails 4 does not have source maps either.

据我所知,截至今天,这只是 Rails 5 的一部分.

As far as I know, and as of today, this will only be part of rails 5.

@SamSaffron 在话语中实施了一种非常好的解决方法,并在此处进行了解释:https://github.com/discourse/discourse/blob/master/lib/tasks/assets.rake

A really nice approach to solve this right now is implemented in discourse by @SamSaffron and explained here: https://github.com/discourse/discourse/blob/master/lib/tasks/assets.rake

要点,在链轮预编译过程中添加一个before"任务,并侵入编译过程以生成源映射文件和指令.

The gist, add a "before" task to the sprockets precompile process, and hack into the compilation process to generate the sourcemapped files and directives.

这种方法的好处是您不会丢失 js 和 erb (*.js.erb) 文件中的内容,这在 Rails 中很常见.

The nice thing in this approach is that you don't lose stuff from files that are both js and erb (*.js.erb) which is something quite common in rails.

我认为修补整个链轮管道有点滥用和冒险.

I think that patching the whole sprockets pipeline is a bit of an abuse and risky.

这篇关于通过链轮在 Ruby on Rails 中进行源映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆