Skip to content

Instantly share code, notes, and snippets.

@progapandist
Last active April 17, 2018 10:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save progapandist/413086b5721e20c2970692a799688e54 to your computer and use it in GitHub Desktop.
Save progapandist/413086b5721e20c2970692a799688e54 to your computer and use it in GitHub Desktop.

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