Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to override single files from gem assets for assets:precompile?
    primarykey
    data
    text
    <h2>Situation</h2> <p>I use a gem that brings its own JavaScript and stylesheet assets. This gem uses a standard application.js and application.css manifest to require all its assets:</p> <pre><code>[gem]/app/assets/javascripts/gem_name/application.js require_tree . [gem]/app/assets/javascripts/gem_name/backoffice/menu.js … some JavaScript code … [gem]/app/assets/javascripts/gem_name/backoffice/forms.js … some JavaScript code … </code></pre> <p>In development mode I can then override single asset files by placing them in <code>[my_app]/app/assets</code> folder, e.g.:</p> <pre><code>[my_app]/app/assets/javascripts/gem_name/backoffice/menu.js </code></pre> <p>The load paths configured in Rails then ensure that files in <code>[my_app]/app/assets</code> override files in <code>[gem]/app/assets</code>.</p> <p>But <strong>when I precompile the assets using <code>rake assets:precompile</code>, the overriding file <code>menu.js</code> doesn't replace the original file from the gem</strong>. The compiled <code>application.js</code> contains the code from the original <code>menu.js</code>.</p> <h2>Demo</h2> <p>See the behavior in action with this test app: <a href="https://github.com/widescape/AssetCompileTest" rel="noreferrer">https://github.com/widescape/AssetCompileTest</a></p> <h2>Considerations</h2> <p>So far, I found out that <code>require_tree</code> looks up files <em>only</em> in the directory of the current file and does <em>not</em> look across multiple load paths (e.g. app/assets or vendor/assets). So if the current file is <code>[gem]/app/assets/javascripts/gem_name/application.js</code>, <code>Sprockets#require_tree</code> will only look in <code>[gem]/app/assets/javascripts/gem_name/</code> for more files and will <em>not</em> use the other load paths like <code>[my_app]/app/assets/…</code>.</p> <p>I am reluctant to copy all asset files from the gem into my app/assets only to override one single file. And forking the gem to customize a single file doesn't have a great appeal, too.</p> <p>Is there any other way?</p> <p><strong>Note:</strong> I am aware that I could create a custom JavaScript file <code>menu_patched.js</code> that overrides the objects and methods defined in the original file and place it so that its definitions will be applied before the original objects and methods are called (monkey patching JavaScript style). But I am looking for a cleaner solution here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload