Skip to content

Commit 823b794

Browse files
Merge pull request #100 from psmir/patch-1
Update hyper-component.md
2 parents d24c632 + 066a1b0 commit 823b794

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

docs/dsl-client/hyper-component.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,62 @@ end
13221322

13231323
Note that the `rename` directive can be used to rename both components and sublibraries, giving you full control over the ruby names of the components and libraries.
13241324

1325+
### Importing Webpack Images
1326+
1327+
If you store your images in app/javascript/images directory and want to display them in components, please add the following code to app/javascript/packs/application.js
1328+
1329+
```javascript
1330+
var webpackImagesMap = {};
1331+
var imagesContext = require.context('../images/', true, /\.(gif|jpg|png|svg)$/i);
1332+
1333+
function importAll (r) {
1334+
r.keys().forEach(key => webpackImagesMap[key] = r(key));
1335+
}
1336+
1337+
importAll(imagesContext);
1338+
1339+
global.webpackImagesMap = webpackImagesMap;
1340+
```
1341+
1342+
The above code creates an images map and stores it in webpackImagesMap variable. It looks something like this
1343+
1344+
```javascript
1345+
{
1346+
"./logo.png": "/packs/images/logo-3e11ad2e3d31a175aec7bb2f20a7e742.png",
1347+
...
1348+
}
1349+
```
1350+
Add the following helper
1351+
1352+
```ruby
1353+
# app/hyperstack/helpers/images_import.rb
1354+
1355+
module ImagesImport
1356+
def img_src(filepath)
1357+
img_map = Native(`webpackImagesMap`)
1358+
img_map["./#{filepath}"]
1359+
end
1360+
end
1361+
```
1362+
1363+
Include it into HyperComponent
1364+
1365+
```ruby
1366+
require 'helpers/images_import'
1367+
class HyperComponent
1368+
...
1369+
include ImagesImport
1370+
end
1371+
```
1372+
1373+
After that you will be able to display the images in your components like this
1374+
1375+
```ruby
1376+
IMG(src: img_src('logo.png')) # app/javascript/images/logo.png
1377+
IMG(src: img_src('landing/some_image.png')) # app/javascript/images/landing/some_image.png
1378+
```
1379+
1380+
13251381
### Auto Import
13261382

13271383
If you use a lot of libraries and are using a Javascript tool chain with Webpack, having to import the libraries in both Hyperstack and Webpack is redundant and just hard work.

0 commit comments

Comments
 (0)