Reputation: 329
I have a theme I'm constantly using on my sites. I've been trying to load css, js and plugin folder (included with the theme) assets through a gem.
Sprockets::FileNotFound: couldn't find file 'phcthemes' with type 'application/javascript'
If I take out the javascript (css only) this doesn't execute and shows up exactly like this in compiled asset
*= phcthemes
My thoughts app wasn't recognizing the gem, I tried using isolate_namespace (commented out for now) also tried several different config using the info from answer below to no avail.
Main App application.css
*= phcthemes
Main App application.js
// require phcthemes
lib/phcthemers.rb
require "phcthemes/version"
module Phcthemes
class MyRailtie < Rails::Railtie
end
class Engine < ::Rails::Engine
# Isolate Namespace
#isolate_namespace Phcthemes
# Initialize Assets
initializer :assets do |config|
Rails.application.config.assets.precompile += %w{ application.js application.css }
Rails.application.config.assets.paths << root.join("app", "assets", "javascripts")
Rails.application.config.assets.paths << root.join("app", "assets", "stylesheets")
end
end
end
phcthemes.gemspec
$:.push File.expand_path("../lib", __FILE__)
# Maintain your gem's version:
require "phcthemes/version"
Gem::Specification.new do |spec|
spec.name = "phcthemes"
spec.version = Phcthemes::VERSION
spec.authors = ["BradPotts"]
spec.email = ["[email protected]"]
spec.homepage = "http://phcnetworks.net"
spec.summary = "PHCThemes"
spec.description = "PHCNetworks theme with mtdevise and assets built in."
spec.license = "GPL-3.0"
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.files = Dir["{app,config,db,lib}/**/*", "LICENSE", "Rakefile", "README.md"]
# Main Structure & Security
spec.add_dependency "rails", "~> 4.2.5.2"
# Development & Testing
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
end
Upvotes: 2
Views: 899
Reputation: 26778
There's some code missing from your Engine class. Include the following
initializer :assets do |config|
Rails.application.config.assets.precompile += %w{ myfile.js myfile.css }
Rails.application.config.assets.paths << root.join("app", "assets", "javascripts")
Rails.application.config.assets.paths << root.join("app", "assets", "stylesheets")
end
and then include your files in app/assets/javascripts or app/assets/stylesheets in your gem directory. I'd be surprised if it didn't work the same for the vendor directory or any other directory.
When the gem is installed to a Rails app, you'll still need to put require statements in application.css and application.js. By the way, your CSS requirement statement is incorrect - it should be *= require phcthemes
The Railtie docs say that a Railtie class is needed when running initializers, so include the following class inside your main gem module:
class MyRailtie < Rails::Railtie
end
That should do it. You don't actually have to change the gemspec at all.
You can see my socket_helpers gem for an example. That's what I was building when I encountered this same issue.
Upvotes: 2