Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Migrating static assets from Sprockets to Webpacker

Move all assets from app/assets to frontend/static:

$ mkdir frontend/static
$ mv app/assets/{images,videos} frontend/static

Then inside irb or rails c:

imports = Dir.glob("frontend/static/**/*").map do |f|
  if File.stat(f).file?
    i = f.sub(/^frontend\//, "")

    "import \"#{i}\";\n"
  end
end.compact.join

File.open("frontend/packs/static.js", "w") do |m|
  m.write(imports)
end

Now we can use our assets from inside of components with:

image_tag asset_pack_path("static/images/my-image.png") or video_tag asset_pack_path("static/videos/my-video.mp4")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.