Sprockets require all files from a gem's folder - ruby-on-rails

So I'm using the jqPlot gem in Rails, and in order to use plugins, I'm required to do a
//=require jqplot/plugins/plugin-name
for each plugin that I want to use. Given that there are currently ~30 plugins in jqPlot, as I use more and more plugins, my application.js manifest will get bloated up with all the plugin requires. As a result, I was wondering if there was a way to require the entire plugin folder with a single require, as that would be significantly cleaner.
I've tried //=require_tree ./jqplot/plugins and //= require_tree ./jqplot/plugins/, and replacing require_tree with require_directory and all I get is an ArgumentError that require_tree/require_directory argument must be a directory.

Related

Rails Assets Precompile in production mode error

When I run rake assets:prcompile RAILS_ENV=production, I am getting below error.But if i run in development mode it runs fine.
rake assets:precompile RAILS_ENV=production
rake aborted!
Uglifier::Error: fs redeclared
Environment
Rails 5.0.7
Ruby 2.4.1
Could someone helps to resolve this one
Got the issue.In my application.js file have below codes
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
As per functionality of require_tree, it will include current path's all js files recursively. Refer the link https://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives
In JavaScript files, Sprockets directives begin with //=. In the above case, the file is using the require and the require_tree directives. The require directive is used to tell Sprockets the files you wish to require. Here, you are requiring the files rails-ujs.js and turbolinks.js that are available somewhere in the search path for Sprockets. You need not supply the extensions explicitly. Sprockets assumes you are requiring a .js file when done from within a .js file.
The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file. You can also use the require_directory directive which includes all JavaScript files only in the directory specified, without recursion.
In my application , Gruntfile.js file was declared in two js folders.So, when I was doing the precompile,as the file was declared in two js , it throws error when it tried to compress that file

Rails Asset Pipeline not compiling assets

In my application.js file, I have the following:
//= require jquery
//= require vendor
When I load up a Rails server with rails s the application.js file that is served still has the require statements at the top. I expect it to remove these lines and load the jquery file separately.
I'm using Rails 3.2.19. My RAILS_ENV is not set.
Though I'm not entirely sure that what you're doing doesn't work, I typically use //= require [name of js file]. e.g. //= require bootstrap.js when bootstrap.js is inside the vendors/javascript folder.
Are you using an old version of Ruby? Maybe something from before 1.9.3? I had similar before problems when upgrading Rails without upgrading Ruby.
Bug with sprockets 2.2.1
Updated to sprockets 2.2.2 (via bundle update sprockets) and it now works

Rails asset pipeline require

In rails to require a file in the vendor directory I know that I can do this in my application.j file.
//=require vendor/assets/javascripts/me.js
Am wondering how does one require the entire javascript folder in the vendor directory
//= require_tree ../../../vendor/assets/javascripts/include/.
See http://guides.rubyonrails.org/asset_pipeline.html#asset-organization
Try
//=require me
Vendor should be loaded by default
Assets are not namespaced
You might need to restart your dev server after adding to vendor

Rails and codekit - how to disable rails coffescript compilation

I'm using rails 3.2, but I'm compiling my coffee files with CodeKit.
I still want my coffee files to live inside 'assets', but each time I restart rails, it finds them in there and tries to compile them itself.
My files live in /assets/cafe/myscript.coffee, and codekit compiles them into /assets/javascripts/myscript.js
The coffee-rails gem is already commented out in my gemfile (when rails tries to (re) compile it it gives "throw Error("LoadError: no such file to load -- coffee_script)" - though I really dont want it to even try compiling.
Setting "config.assets.compile = false" in application.rb results in "application.js isn't precompiled"
you should probably just configure your sprockets manifest to not require the whole tree.
edit app/assets/javascripts/application.js
typically it looks like this:
//= require jquery
//= require jquery_ujs
//= require_tree .
change that to
//= require jquery
//= require jquery_ujs
//= require myscript
An (inferior) workaround is to put my coffeescripts inside "App" rather than "assets" (so one more branch up the tree.)
This sucks because it's not where they should go, but at least it does put it outside rails' stalker-tendencies to find coffeescript files anywhere in assets and try to compile them.

How do I require subdirectories of /app/assets/javascripts in Rails 3.1

I want to separate out my javascripts into separate subdirectories in my Rails 3.1 app.
For instance I have a /modules directory inside /app/assets/javascripts
A way to either require all the contents of the directory or each file individually would be helpful.
Edit: To clarify, I want to do this from my application.js coffeescript file.
I believe the way to do this in Sprockets is
#= require_tree modules
or
#= require_tree ./modules
if you want to select a subdirectory relative to the CoffeeScript file, rather than relative to app/assets/javascripts (see this issue).

Resources