-
Notifications
You must be signed in to change notification settings - Fork 791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails Engine not compiling assets unless explicitly declared #542
Comments
Please use StackOverflow for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only. |
@rafaelfranca I have (https://stackoverflow.com/questions/48266471/make-rails-engine-compile-all-assets) But as stated above, several Stack Overflow questions and related articles about this same issue have been raised by others, so it seems like something that isn't working as expected. Does Rails require each file to be listed or each extension to be listed in the Engine? |
It is indeed working as expected. Rails doesn't require each file to be listed in the precompile list. But it requires each engine to register its assets. You can do it in the same way rails applications does. If you want to register all assets in your engine you need to specify that using a asset manifest and one of the link* directives https://github.com/rails/sprockets#link. |
@rafaelfranca Thanks for the response. I did actually try that. So in the Engine in I also tried:
But still can't find the images (unless I do it in the An additional question I have then, IF "it requires each engine to register its assets" then how come I can use the JS, CSS, and FONT files without having to do the precompile part? It's only the images that are not working unless precompiled in the |
have you added |
Nothing should work if you don't add to precompile. If it is working, please create a minimal application reproducing this issue. |
@rafaelfranca I hadn't. But I've added it now to my Engine's
and the contents of the manifest file are:
So I've tried to register the whole images folder in the Engine as well as each image individually, but images are still not working unless I add them using I'm using |
@rafaelfranca Should the above have worked? |
I had this problem before and you are indeed correct in that it doesn't work with the manifest file. The only way to get it to work is to explicitly require the top-level .css / .js files for your app in the We presently use:
This works 100% as intended. Would obviously love to have the manifest working - but it doesn't at present (sprockets 4.0.0.beta8). |
@richpeck
|
👋 Wow, this took me 3 hours to figure out. Shall I update the rails documentation on this? I am. building a bunch of themes as engines, and it totally hurt my head, because the theme also has images. |
👋 Hi there. I'm probably late to the party but I've been debugging this very same issue in the last few days and I've finally discovered why this doesn't work. First, If you look at this line: https://github.com/rails/sprockets-rails/blob/5f75d4196b046ec6bef573b9d5e3292df7afb5e8/lib/sprockets/railtie.rb#L60 # engine_name/config/initializers/assets.rb
app.config.assets.precompile << "engine_name_manifest.js" It's still important to namespace the manifest with the engine name or it will resolve wrongly. |
I just spent half a day on this issue until I found @masylum's comment, and solved adding inside
Then the manifest gets loaded correctly by the main app that includes the engine. |
Is there any difference between these three?: Within
Within
or in an initialiser
I've settled on #2 as it's the simplest but is there any ordering issues here? |
@brendon I don't think there's any functional difference but I'd go with number 3. I treat |
I'm raising this as an issue as it feels like it doesn't work as expected or as described in the documentation and looking around the internet it seems there a number of questions/issues raised around this that all have hacky solutions of which either don't work, are messy or introduce additional problems. It could be just a unknown... or perhaps missing feature of sprockets but it seems that it's certainly something that isn't easy to implement and hard to believe that Rails doesn't support it.
So I have a Rails Engine that contains assets like so:
And in both my Host application and Engine I want to be able to use these images like so:
Now if I explicitly set these files to be precompiled in the
assets.rb
or theengine.rb
file then they work:And I can even override the images in the Host application if I have a similar named image (and parent folder) that matches the one in the Engine.
But I want to make it so that I don't have to call them by name and instead just load them all in at the Engine level so I don't have to declare anything by file name or in each Host application.
I have tried adding this to my
engine.rb
:But it doesn't work... I still end up getting the error:
Asset was not declared to be precompiled in production. Add 'Rails.application.config.assets.precompile += %w( engine_name/image.png )' to
config/initializers/assets.rband restart your server
So I've tried some alternate ways in the
engine.rb
:This also works (so I don't have to specify each folder):
app.config.assets.precompile << "**/image.png"
and so does this (so I don't have to specify the name or folder):
app.config.assets.precompile << "**/*.png"
But I don't want to have to specify the extension of the image (as I might have lots)
So I've tried:
app.config.assets.precompile << "**/*.*"
But that does not work. Seems you MUST specify an extension.
So I tried this:
app.config.assets.precompile << "**"
Which does work for the images, but then breaks all the SASS and raises Sass syntax errors about unknown variables and that! So this isn't a solution either.
Is there a proper way to be able to get an Engine's assets to precompile without having to resort to a manic regex to only gets certain files?
The text was updated successfully, but these errors were encountered: