Sprockets require file from parent directory - ruby-on-rails

I am trying to use the rails 3.1 asset pipeline to include a javascript file from the manifest file's parent directory
app/assets/javascripts/folder/index.js
//=require ../file
yields: couldn't find file 'file'
How do I require a file from the parent directory?

Assets in Sprockets are always referenced by their logical path.
So you just have to use //=require file if your file is in /app/assets/javascripts.
If your file was /lib/assets/javascripts/models/lala.js for instance, you would require it with //=require models/lala.

Related

Sprockets processing image assets from a gem

I'm migrating from sprockets 3 to sprockets 4 and v4 seems to ignore assets from gems. Those were automatically handled by v3, but I can't seem to find anything about porting this behaviour.
For example there's gem mediaelement_rails which includes mediaelement_rails-0.8.2/app/assets/images/mediaelement_rails/skipback.png
In sprockets v3 it was automatically included during asset compilation, but now, even with manifest:
//= require mediaelement_rails
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
The skipback.png is not included.
I could do this very explicitly: query the gem path and add it to the assets paths. But is there a way to do this automatically similar to v3?
Sprockets v4 no longer automatically includes gem resources.
To include gem resources in your assets, you must explicitly register the gem paths that contain the resources. This can be done using the configuration: config.assets.paths in your config/application.rb file.
For example, to include the mediaelement_rails gem image resources in your assets, you could add the following to your config/application.rb file:
config.assets.paths << Rails.root.join('vendor', 'assets', 'mediaelement_rails', 'images')
This will add the vendor/assets/mediaelement_rails/images directory to the assets path.
If the gem you're using isn't following the standard naming conventions for its assets (i.e. putting your assets in subdirectories named after javascripts, stylesheets, or images), you'll need to explicitly register those directories. using a config.assets.precompile setting in your config/application.rb file.
Here they talk a little about inserting paths that don't follow a pattern
Here, the rails documentation also already shows something about this

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

How to load javascript file from rails plugin

I'm trying to create a plugin which is suppose to expose a javascript file, but I can't get it to load in my rails 5 application.
In my gem, I've tried adding my javascript file called my_gem.js to
vendor/assets/javascripts
app/assets/javascripts
lib/<gem-name>/assets/javascripts
But none of them work. I just get this error message when loading the file in my application.js
couldn't find file 'my_gem' with type 'application/javascript'
This is how I load my plugin/gem in to my rails project
gem "my-gem", path: "~/projects/my-gem"
We've created a gem which utilizes assets before.
You'll want to create an app/assets/javascripts/my_gem folder in your gem directory tree, where you'll put your my_gem.js file.
Most importantly, you need to add this file to the asset path (we use an engine):
#lib/my_gem.rb
module MyGem
class Gem < Rails::Engine
config.assets.precompile += %w(my_gem/my_gem.js)
end
end
This will either allow the file to be used standalone, or as part of your app:
#app/assets/javascripts/application.js
//= require my_gem/my_gem
You must tell Rails to compile my_gem.js. You can do this in an initializer, application.rb, or environment file.
Rails.application.config.assets.precompile += ["my_gem.js"]
I'd recommend keeping the source file at ~/projects/my-gem/app/assets/javascripts/my_gem.js

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

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