Rails Engine assests are not precompiled
Asked Answered
G

3

5

I am studying rails and using the engines with rails. In production mode, rails did not load compiled assets of engines, although I have executed:

$ RAILS_ENV=production bundle exec rake assets:clean assets:precompile

Please help if anyone knows the issue.

My settings are as below:

environment/production.rb

config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = false 
config.assets.compile = false 
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new`

engine/xxx/lib/xxx/engine.rb

Engines's option is -- mountable

 module Moderna  
  class Engine < ::Rails::Engine  
    isolate_namespace xxx

    # parent company asset precompile
    initializer "xxx.assets.precompile" do |app|

      app.config.assets.paths << Rails.root.join("app", "assets", "fonts")

      app.config.assets.precompile << %w(
        xxx/*.css xxx/fancybox/*.css xxx/skin/*.css xxx/google-code-prettify/*.css
        xxx/*.js xxx/flexslider/*.js xxx/google-code-prettify/*.js
        xxx/portfolio/*.js xxx/quicksand/*.js
        xxx/*.svg xxx/*.eot xxx/*.woff xxx/*.ttf
        xxx/customicon/*.svg xxx/customicon/*.eot xxx/customicon/*.woff xxx/customicon/*.ttf
      )

      app.config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
    end
  end
end
Gormand answered 14/3, 2016 at 14:56 Comment(1)
What is the error?Archimage
A
4

The rails engine getting started guide suggests including the following in your engine.rb

initializer "engine_name.assets.precompile" do |app|
  app.config.assets.precompile += %w( admin.js admin.css )
end

It works for me.

Amnesia answered 14/9, 2017 at 19:28 Comment(0)
G
2

I faced same issue with Rails 4 engine, I resolved by adding following code to engine.rb

Rails.application.config.assets.precompile += ['*.js', '*.css', '**/*.js', '**/*.css', '*.jpg',
                                '*.png', '*.ico', '*.gif', '*.woff2', '*.eot',
                                '*.woff', '*.ttf', '*.svg'] 

I need to precompile all assets so wild cards are used. You can specify files.

Second option is you can specify on Host applications config/assets.rb

Rails.application.config.assets.precompile += %w( engine_name/file_name )
Graycegrayheaded answered 17/3, 2016 at 17:22 Comment(1)
Can you try second option. by specifying in host applications assets.rbGraycegrayheaded
F
1

EDIT Just realised this question is a bit old. The below will work for you if you add an app/assets/config/my_component_manifest.js file to your engine and then use that to specify asset entry points.

You an also put it in the engine.rb file:

module MyComponent
  class Engine < ::Rails::Engine
    config.assets.precompile += %w( my_component_manifest.js )
  end
end
Frumpish answered 16/4, 2021 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.