13

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?

Philip7899
  • 4,599
  • 4
  • 55
  • 114

1 Answers1

4

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)

Ruslan
  • 1,919
  • 21
  • 42
  • I am trying to do this but still need to include the require_tree . and that appears to be breaking this with the following error: Sprockets::CircularDependencyError in Index#index .. I need the require_tree . to work because there are about 100+ angular files in the assets/javascripts folder and the differences I need have been added via bower_components and live in a separate directory.. thoughts? – Starfs Oct 29 '18 at 21:08
  • 1
    @Starfs `require_tree . ` will require the other JS manifest file. Put all the shared JS into a shared directory and then do `require_tree ./shared` or something of that nature – Ruslan Oct 29 '18 at 21:41
  • @Rusian as a side note I still had trouble making this work, but eventually just imported scripts in /views/layout/application.html.erb <% if mobile_device == "mobile" %> – Starfs Mar 19 '19 at 18:29
  • @Starfs Just be careful when you deploy to production with asset pipeline. Because `application.js` will turn to `application-2sadf324gsx234dfasdf.......js` – Ruslan Mar 19 '19 at 18:42
  • It seems like you're describing the very thing the OP tried, which didn't work for them. This approach does not seem to work. – Marc Apr 21 '19 at 08:32