Rails 3.2 Dev environment sourceMaps support for JavaScript

Rails 3.2 Dev environment sourceMaps support for JavaScript

本文关键字:support for JavaScript sourceMaps environment Dev Rails      更新时间:2023-09-26

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

  config.assets.compress = false
  config.assets.compile = true
  config.assets.debug = true

在开发环境中,资产不是捆绑在一起的,每个资源都由Rails单独提供。此时,单独提供的资产数量超过 50 个。因此,整页重新加载速度非常慢。

我想至少将它们连接在一些资产中,以便在开发环境中更快地加载时间,但这样做,我失去了在 Chrome 开发工具中单独调试/查看它们的能力。示例:http://d.pr/i/ZFge

据我所知,在您这样做之后,有两种方法可以解决此问题:

  config.assets.debug = false

并开始将它们作为串联资产提供。

  1. 老哈基方式:@sourceUrl把戏。
  2. 新方式:源地图。

有没有关于如何在轨道应用程序上启用它们的指南?我不使用CoffeeScript,所以 https://github.com/markbates/coffee-rails-source-maps 没有帮助。大多数谷歌搜索都会导致这一点。

我正在寻找本机JS的解决方案。

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

下面假设gem uglifier是正在使用的 js 压缩器。

丑陋程序的版本 2 具有创建源映射的机制。它具有以下语法

uglified, source_map = Uglifier.new.compile_with_map(source)

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

config.assets.js_compressor = Transformer.new

在此处阅读相关内容

一个简单的转换器类如下所示

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

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