18

I'm using webpack's url-loader plugin and have it configured as:

{
    test: /\.(ttf|eot|woff|woff2|svg)$/,
    loader: "url-loader?limit=50000"
}

It output files > 50k to the file system, but I can't find how to set a destination path.

In this case, I want the files to be stored to ./fonts and not the root.

user3900456
  • 2,003
  • 3
  • 25
  • 33

4 Answers4

29

url-loader is build on file-loader, so you can use it like file-loader, as shown below:

{
    test: /\.(ttf|eot|woff|woff2|svg)$/,
    loader: "url-loader?limit=50000&name=fonts/[name].[ext]"
}
VLS
  • 2,306
  • 4
  • 22
  • 17
wandergis
  • 424
  • 4
  • 6
13

you can write it like this

{
        test: /\.(png|woff|eot|ttf|svg|gif)$/,
        use: [
          {
          loader: 'url-loader',
          options: {
            limit: 10000, // if less than 10 kb, add base64 encoded image to css
            name: "assets/[hash].[ext]" // if more than 10 kb move to this folder in build using file-loader
          }
        }]
      }
marverix
  • 7,184
  • 6
  • 38
  • 50
Johansrk
  • 5,160
  • 3
  • 38
  • 37
7

To add to the answer by @wandergis url-loader will rename the image if the size limit is exceeded and use a hash for the name. When using [name].[ext] as suggested, uses the original name of the file, which is not what I needed. I needed the name of the hash that url-loader is going to create. So, you can add [hash].[ext] to get the renamed file.

{
    test: /\.(ttf|eot|woff|woff2|svg)$/,
    loader: "url-loader?limit=50000&name=fonts/[hash].[ext]"
}
Community
  • 1
  • 1
David Glass
  • 2,334
  • 1
  • 25
  • 35
0

You can also do this, which can be cleaner

   {
     test: /\.(ttf|eot|woff|woff2|svg)$/,
     use: [
        {
          loader: 'url-loader',
          options: {
            name:'[hash].[ext]' 
            outputPath: 'fonts',
          },
        },
       ],
    }
Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82