Using Webpacker in Rails 5.1 and loading image assets

So I've migrated my Rails application over to Webpack but I'm having trouble loading image assets. In the Webpacker documentation it states that I can load my assets using the following:

<img src="<%= asset_pack_path 'images/logo.svg' %>" />

But this doesn't seem to be compiled down when I run rails webpacker:compile

My folder structure looks like so

app/javascript:
  ??? packs:
  ?   # only webpack entry files here
  ?   ??? application.js
  ??? src:
  ?   ??? application.css
  ??? images:
      ??? logo.svg

and my webpacker.yml contains:

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

I'm not great at this new Webpack stuff but I assume it has something to do with my webpacker.yml

Has anyone experienced this before? There seems to be little advice on the internet.

Ta!

Answers:

Answer

I think I can resolve this by manually importing the images from inside my application.js

So for example;

# /app/javascript/packs/application.js
import '../images/logo.svg';

Then lets me reference this using the asset_pack_path

Still if somebody can tell me a better way i'd really appreciate it.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.