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

我试图从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 . 

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

 Rails.application.config.assets.precompile += %w(other_manifest.js) 

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

容易

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

然后在你的布局layouts/application.html.erb (可能是一个不同的布局):

 <% if condition %> <%= javascript_include_tag 'other_manifest' %> <% else %> <%= javascript_include_tag 'application' %> <% end %> 

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

 Rails.application.config.assets.precompile += %w(other_manifest.js) 

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