问题
I'm having trouble getting vendorized assets to be compiled as root files.
I want to have the following assets available standalone (not packaged with other assets_:
vendor/gems/neo-viz/app/assets/stylesheets/neo-viz.css.scss
vendor/gems/neo-viz/app/assets/javascripts/neo-viz.js.coffee
vendor/gems/neo-viz/app/assets/javascripts/lib/jQuery/jquery-1.6.1.min.js
vendor/gems/neo-viz/app/assets/javascripts/lib/arbor/arbor.js
With the following line:
config.assets.precompile += %w( jquery-1.6.1.min.js arbor.js neo-viz.js neo-viz.css )
Only the css file is precompiled and made available. Why?
Also, after seeing this question ( Including assets in subdirectories with config.assets.precompile in Rails ), I tried:
config.assets.precompile += %w( jquery-1.6.1.min.js arbor.js neo-viz.js neo-viz.css lib/arbor/arbor.js arbor/arbor.js lib/jQuery/jquery-1.6.1.min.js jQuery/jquery-1.6.1.min.js )
But it made no difference. Ideas?
回答1:
By default Rails only looks into 3 locations: app/assets
, lib/assets
or vendor/assets
. Sprockets looks for JS assets in the sub-directory javascripts
. E.g.
app/assets/javascripts/arbor.js
lib/assets/javascripts/arbor.js
vendor/assets/javascripts/arbor.js
If you want to see where Rails is looking for you can use this in the console: Rails.application.config.assets.paths
What you can do is add your custom path to the pipeline:
# @file: config/application.rb
config.assets.paths << Rails.root.join("vendor", "gems", "neo-viz", "app", "assets", "javascripts")
Then create a manifest, where you include your JS files:
# @file: /vendor/gems/neo-viz/app/assets/javascripts/neo-viz.js
//= require lib/jQuery/jquery-1.6.1.min
//= require lib/arbor/arbor
//= require neo-viz
Finally add your manifest:
config.assets.precompile += %w( neo-viz.js )'
If you still want to add them separatly, which I do not see the point, why not include them in a manifest, then you'll have to add all the possible path prefix so Rails knows where to look:
# @file: config/application.rb
config.assets.paths << Rails.root.join("vendor", "gems", "neo-viz", "app", "assets", "stylesheets")
config.assets.paths << Rails.root.join("vendor", "gems", "neo-viz", "app", "assets", "javascripts")
config.assets.paths << Rails.root.join("vendor", "gems", "neo-viz", "app", "assets", "javascripts", "lib", "jQuery")
config.assets.paths << Rails.root.join("vendor", "gems", "neo-viz", "app", "assets", "javascripts", "lib", "arbor")
Then you can add them like this, since assets outside a manifest must be added to the precompile array:
config.assets.precompile += %w( jquery-1.6.1.min.js arbor.js neo-viz.js neo-viz.css )
Source: 2.1 Asset Organization
来源:https://stackoverflow.com/questions/10067327/config-assets-precompile-not-adding-vendor-gem-assets