Rails资产管道:如何创建自定义清单文件

Phi*_*899 11 ruby-on-rails sprockets asset-pipeline ruby-on-rails-4 ruby-on-rails-4.1

我试图从application.js单独创建一个自定义清单javascript文件.我从application.js获取代码并将其粘贴到我称为"other_manifest.js"的新文件中,并放在assets/javascrips目录中.这是代码:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .
Run Code Online (Sandbox Code Playgroud)

在我的assets.rb文件中,我已经包含了这一行:

Rails.application.config.assets.precompile += %w(other_manifest.js)
Run Code Online (Sandbox Code Playgroud)

我在本地预编译和清理资产,然后当我运行页面时,我得到的只是清单文件中的确切文本.它没有引入任何文件.如何创建自定义清单文件?

Rus*_*lan 4

容易地

你有application.js。创建第二个文件:other_manifest.js

然后在您的布局中layouts/application.html.erb(可能是完全不同的布局):

<% if condition %>
  <%= javascript_include_tag 'other_manifest' %>
<% else %>
  <%= javascript_include_tag 'application' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

是的,您需要添加config/initializers/assets.rb(然后重新启动):

Rails.application.config.assets.precompile += %w(other_manifest.js)
Run Code Online (Sandbox Code Playgroud)

另外,请确保从清单中删除//= require_tree .。因为它将包含清单中的所有 javascript(使得拥有不同的清单毫无意义)