Rails 3.2 Dev环境sourceMaps支持JavaScript

Raj*_*jat 7 javascript ruby-on-rails ruby-on-rails-3 source-maps

我正在使用Asset管道开发Rails应用程序.development.rb具有以下内容:

  config.assets.compress = false
  config.assets.compile = true
  config.assets.debug = true
Run Code Online (Sandbox Code Playgroud)

在Dev环境中,资产没有捆绑在一起,每个资源都由Rails单独提供.此时,单独获得服务的资产数量超过50.因此,整页重新加载速度极慢.

我想在几个资产中将它们连接起来以便在开发环境中加快加载时间但是这样做,我放弃了在Chrome开发工具中单独调试/查看它们的能力.示例:http://d.pr/i/ZFge

在你知道后,有两种方法可以解决这个问题:

  config.assets.debug = false
Run Code Online (Sandbox Code Playgroud)

并开始作为连锁资产提供服务.

  1. Old Hacky Way:@sourceUrl技巧.
  2. 新方式:sourceMaps.

是否有关于如何在rails应用程序上启用它们的指南?我不使用CoffeeScript,所以https://github.com/markbates/coffee-rails-source-maps没有帮助.大多数Google搜索都会导致这种情况.

我正在寻找原生JS的解决方案.

Jos*_*hin 13

我还没有看到这个问题的现有解决方案.但建立一个将是非常直接的.

以下假设gem uglifier是使用的js压缩器.

uglifier的第2版有一个创建源图的机制.它具有以下语法

uglified, source_map = Uglifier.new.compile_with_map(source)
Run Code Online (Sandbox Code Playgroud)

Rails资产管道允许compress使用以下语法指定自定义JS Compressor(使用方法)

config.assets.js_compressor = Transformer.new
Run Code Online (Sandbox Code Playgroud)

在这里阅读

一个简单的Transformer类如下所示

class Transformer
  def compress(string)
    if Rails.env.development?
      output, sourcemap = Uglifier.new.compile_with_map(string)

      # write the sourcemap to a file somewhere under public

      sourcemap_comment = "//@ sourceMappingURL=#{sourcemap_path}}\n"

      return output + sourcemap_comment
    else
      Uglifier.compile(string)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

注意:这不是解释概念的完整解决方案.您可以在此基础上进行构建,并添加使其更加健壮的方法.