What is the best method fo loading static resources?

6
closed
dbhandel
dbhandel
Posted 5 years ago

What is the best method fo loading static resources? #18

Hi, I'm trying to load a logo.png file. I created a directory /public/images. I have attempted to use github: tcoopman/image-webpack-loader although I may have misused this or it may not be the correct loader.

Do you have any guidance on how to load images using your boilerplate?

Thanks

jirikolarik
jirikolarik
Created 5 years ago

Hey,

sure thing.

You can use https://github.com/webpack/file-loader and loader to web pack config

{ test: /\.(png|jpg|svg)$/, loader: 'file?name=/[name]-[hash].[ext]' }
jirikolarik
jirikolarik
Created 5 years ago

Give me second, I'll prepare you a demo

jirikolarik
jirikolarik
dbhandel
dbhandel
Created 5 years ago

Thanks Jiri. Your instructions are crystal clear but I get this error:

Module parse failed: /Users/davidhandel/code/idr/src/public/images/logo.png Line 1: Unexpected token ILLEGAL You may need an appropriate loader to handle this file type. (Source code omitted for this binary file) @src/components/LoggedInHeader.js 31:12-48

I have a png file loaded in logo.png. I have placed your loader in my webpack.development.js. All I see is a broken image in the browser.

jirikolarik
jirikolarik
Created 5 years ago

It says that you need a loader which I forgot to save in packages.json

npm i file-loader --save

dbhandel
dbhandel
Created 5 years ago

For whatever reason, the loader code didn't work for me in Webpack config but when I used it in the component like this: const logo = require('file?name=/images/[name]-[hash].[ext]!../public/images/logo.png');

I could successfully render the logo with an img tag with src={logo} ,

Many thanks.