From 49150f092e27be42a3c06c84784b5c3e7a9bb981 Mon Sep 17 00:00:00 2001 From: Klaus Badelt Date: Thu, 28 Feb 2019 14:39:40 -0800 Subject: [PATCH 1/2] Add needed require.context to use pack helpers As discussed in #705, the documentation is missing the essential addition of require.context in your app js to make the `asset_pack_path` and `image_pack_tag` --- docs/assets.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/docs/assets.md b/docs/assets.md index f7e34c4f7..f4a007013 100644 --- a/docs/assets.md +++ b/docs/assets.md @@ -84,7 +84,7 @@ import 'assets/stylesheets/bar' ## Link in your Rails views You can also link `js/images/styles/fonts` used within your js app in views using -`asset_pack_path` helper. This helper is useful in cases where you just want to +`asset_pack_path` and `image_pack_tag` helpers. These helpers are useful in cases where you just want to create a `` or `` for an asset. ```yml @@ -97,13 +97,17 @@ app/javascript: - calendar.png ``` -```erb -<%= asset_pack_path 'hello_react.css' %> -<%# => "/packs/hello_react.css" %> +```js +// `app/javascript/packs/app.js` (or any of your packs): +require.context('../images', true) +``` - -<% # => %> +```erb + +<% # => %> -<%= image_pack_tag 'images/calendar.png' %> -<% # => %> +<%= image_pack_tag 'media/calendar.png' %> +<% # => %> ``` + +Note the `media/` prefix replacing any subfolder structure prefix you might have in `app/javascript`. From b7221dfb0611533756268555f664d4f937ab4ea0 Mon Sep 17 00:00:00 2001 From: Klaus Badelt Date: Mon, 11 Mar 2019 18:30:17 -0700 Subject: [PATCH 2/2] updated to reflect 0b86cad and PR #1964 --- docs/assets.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/assets.md b/docs/assets.md index f4a007013..51e36cfba 100644 --- a/docs/assets.md +++ b/docs/assets.md @@ -90,24 +90,30 @@ create a `` or `` for an asset. ```yml app/javascript: - packs - - hello_react.js - - styles - - hello_react.css + - app.js - images - calendar.png ``` ```js -// `app/javascript/packs/app.js` (or any of your packs): +// app/javascript/packs/app.js (or any of your packs) + +// import all image files in a folder: require.context('../images', true) ``` ```erb - -<% # => %> +<%# Rails view, for example app/views/layouts/application.html.erb %> + + +<% # => %> + +<%= image_pack_tag 'media/images/calendar.png' %> +<% # => %> -<%= image_pack_tag 'media/calendar.png' %> -<% # => %> +<%# no path resolves to default 'images' folder: %> +<%= image_pack_tag 'calendar.png' %> +<% # => %> ``` -Note the `media/` prefix replacing any subfolder structure prefix you might have in `app/javascript`. +Note you need to add a `media/` prefix (not `/media/`) to any subfolder structure you might have in `app/javascript`. See more examples in the [tests](https://github.com/rails/webpacker/blob/0b86cadb5ed921e2c1538382e72a236ec30a5d97/test/helper_test.rb#L37).