Skip to content

Add image handling example #1

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/additional-reading/images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Images

A full example can be found at [spec/dummy/client/app/components/ImageExample/ImageExample.js](../../spec/dummy/client/app/components/ImageExample/ImageExample.js)

You are free to use images either in image tags or as background images in SCSS files. You can
use a "global" location of /client/app/assets/images or a relative path to your JS or SCSS file, as
is done with CSS modules.

**images** is a defined alias, so "images/foobar.jpg" would point to the file at
`/client/app/assets/images/foobar.jpg.`

# Usage as Background Images or for `img` Tags

Background images for CSS/SCSS need slightly different handling than images used with `img` tags,
and thus we need to configure the webpack loaders slightly differently.

The example shows you how to put either `bg-` or `bg_` in any file to be used as a background image. A regexp match
will ensure the appropriate version of the url-loader to be used depending on if the image is used
as a background image or within an `img` tag.

The reason why this is done is that the sass-loader assumes that images will be relative to the
deployed sass file, which will already be `/assets/some-file.css`. Thus, the sass-loader is already
going to prepend `/assets` to all images. The file-loader needs an option to specify that the
public path is `/assets` and that will get prepended to any image path. Consequently, if we didn't
distinguish background images from images for `img` tags, then the background image tags will get
an image path like `/assets/assets/some-file.svg` because the sass-loader thinks the path should be
`/assets/some-file.svg` and that gets added to what the file-loader will be doing.

We solve this by requiring a naming convention of `bg-` or `bg_` in the image names. Be warned that
the regexp does not check that these three characters are the beginning of a name or path. They can
be anywhere in the string. Of course, you can create your convention.

You can see this configured: [spec/dummy/client/webpack.common.js](../../spec/dummy/client/webpack.common.js)

_Note, all of this may change when we skip the asset pipeline for processing files in the near future._
3 changes: 3 additions & 0 deletions spec/dummy/app/views/pages/_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@
<li>
<%= link_to "Generator function returns object with renderedHtml", rendered_html_path %>
</li>
<li>
<%= link_to "Image Example", image_example_path %>
</li>
</ul>
<hr/>
7 changes: 7 additions & 0 deletions spec/dummy/app/views/pages/image_example.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= render "header" %>

<%= react_component("ImageExample", prerender: true, trace: true) %>

<hr/>

This page demonstrates using images, both as background images and with img tags.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions spec/dummy/client/app/assets/images/bg_lego_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading