0

I have two folders src/images/ and src/images/icons. All favicons are in png format.

In src/images/icons I put all the favicons for different devices, which I want to be webpacked to wwwroot/images/icons and all other images to wwwroot/images.

How can I separate images and favicons?

Now for images I have:

 {
            test: /\.(png|ico|svg|jpg|gif)$/,
            use: [
                'file-loader?name=/images/[name].[ext]'
            ]
        },

But this would copy all images to dist\images, including the icons, which should be one level deeper in folder dist\images\icons

Legends
  • 21,202
  • 16
  • 97
  • 123

1 Answers1

1

There are several ways to do this (i.e. use test key against filename, separate rules, etc). However, here is one way that seems to work well and is fairly clear:

const path = require('path')

module.exports = {
  // ...
  module: {
    rules: [

      {
        test: /\.(png|ico|svg|jpg|gif)$/,
        exclude: /node_modules/,
        use: {
          loader: 'file-loader',
          options: {
            name: function(fullPath) {
              return path.relative(__dirname + '/src', fullPath)
            }
          }
        }
      }

    ]
  }
  // ...
}
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
  • All the icons are in `png` format... that's the problem and I have other images in `png` format – Legends Feb 22 '18 at 16:17
  • @Legends how do you tell what is an icon and what isn't? – Joel Cornett Feb 22 '18 at 16:18
  • I have a `src/images/icons` folder where i have put all the icons and images are in `src/images`. In my markup I would use the respective url: `` – Legends Feb 22 '18 at 16:21
  • I guess the `test:` checks against the resource path, perhaps if the regex could be modified to include the `icons` folder? Would that be a valid approach? – Legends Feb 22 '18 at 16:28
  • @Legends after playing around a bit, I found a way that seems to work well by preserving directory structure without needing too many regex acrobatics. Updated my answer accordingly – Joel Cornett Feb 22 '18 at 16:35
  • This works, great! One question, I get another `node_modules/font-awesome/fonts/fontawesome-webfont.svg` folder under `wwwroot` folder. How can I exclude this. With the old approach it wasn't there... – Legends Feb 22 '18 at 17:04
  • 1
    I've updated the answer: the standard way to do this is to put an `exclude: /node_modules/` key in the rule. – Joel Cornett Feb 22 '18 at 17:05
  • Perfect. Thanks a lot Joel! – Legends Feb 22 '18 at 17:13
  • There is one issue with this solution: When using background-images in a css file, I get this: `background: url(/dist/images\blue.jpg)` --> the last slash is a backslash. Now the image does not get resolved anymore. Don't know, if there is a way to configure this, but now I replace all back-slashes with forward-slashes: `return path.relative(rootfolder + '/src', fullPath).replace(/\\/g, '\/');` – Legends Feb 22 '18 at 20:31
  • @Legends that is strange... it could be a bug in the file loader plugin. – Joel Cornett Feb 24 '18 at 01:35