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
Related
I'm setting up a new Rails 6 app and moved all of my assets over to webpack. I now want to delete my empty app/assets directory, but doing so means Rails won't boot because Sprockets can't find my missing manifest.js file:
> Expected to find a manifest file in `app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError)
But did not, please create this file and use it to link any assets that need
to be rendered by your app:
Example:
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
and restart your server
I haven't found any good solutions for completely removing Sprockets. Creating an empty assets directory just to hold a manifest file I'm no longer using doesn't seem like the optimal solution here. Yes, it will work, but what's the point of moving to webpack if Rails still forces us to use Sprockets and have a JS manifest file?
Has anyone successfully removed their assets directory completely? Thanks for your help!
I just removed app/assets from my apps, getting similar error. Solve by:
Change this line in config/application.rb
from
require 'rails/all'
to
require 'rails'
# Pick only the frameworks we want:
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_mailbox/engine'
require 'action_text/engine'
require 'action_view/railtie'
require 'action_cable/engine'
# require "sprockets/railtie"
require 'rails/test_unit/railtie'
Remove all config.assets.* in config/environments/*
Delete config/initializers/assets.rb
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
I would like to keep Javascript controllers (Angular) next to Rails 4 controllers, in the same folder. I added this line to the config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join('app', 'controllers')
And restarted server (development env), but it didn't help. Javascript files are written in Coffee and work when in standard assets/javascripts folder.
Solved by adding one line in assets/javascripts/application.js
//= require_tree ../../controllers
I's because default require_tree . doesn't autoload all javascript files in asset pipeline, but only those in the "dot" folder of application.js, which is standard javascripts folder.
add this line to the application.js
//= require_directory .
It will include all js files in the app/assets/javascript directory recursively
Imagine we have Rails Engine blog.
The engine's structure is:
blog
assets
javascripts
blog
master.js
application.js
controllers
...
helpers
...
application.js is
//= require_tree .
master.js holds some JavaScript code.
Also I have my application using this engine at /blog.
My question is: How can I add some JavaScript code to engine from my application?
Solution is:
Ensure bundle files (application.js, application.css) on plugin and application have different names. In my example plugin's bundle will be bundle.js, and application's bundle — application.js
bundle.js will look like:
require_tree .
application.js will look like:
require blog/bundle
require_tree .
The problem was in name overlapping - application's bundle was overlapping engines one.
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).