Rails asset pipeline: how to create custom manifest file

丶灬走出姿态 提交于 2019-12-10 12:35:51

问题


I am trying to make a custom manifest javascript file seperate from application.js. I've take the code from application.js and pasted it into a new file I've called "other_manifest.js" and placed in the assets/javascrips directory. Here is the code:

// 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 .

In my assets.rb file, I've included the line:

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

I precompile and clean the assets locally, and then when I run the page, all i get is the exact text from the manifest file. It is not bringing in any of the files. How do I create a custom manifest file?


回答1:


Easily

You have application.js. Create a second file: other_manifest.js

Then in your layout layouts/application.html.erb (could be a different layout altogether):

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

And yes, you need to add in your config/initializers/assets.rb (followed by reboot):

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

Also, make sure to remove the //= require_tree . from manifests. Because it will include ALL the javascript in the manifest (making having a different manifest pointless)



来源:https://stackoverflow.com/questions/29990074/rails-asset-pipeline-how-to-create-custom-manifest-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!