Search code examples
cssfontswebpackassetswebpack-file-loader

Webpack - File Loader - Parent Directory + Different Served Path


Good evening,

Given an image/font file located at assets/images/myimg.png (or assets/fonts/myfont.woff).....

I want webpack's file-loader to create a hashed version in a different directory (../priv/static/fonts/myfont.woff)...

But I don't want that name to be part of the "served asset path for the file".

If I use file-loader with a name option that looks like this: "../priv/static/fonts/[name]-[hash].[ext]", the file ends up in the right place in my file system.

But when referenced from CSS, the full path that the browser is looking for is http://mysite.dev/priv/static/fonts/myfile-fee66e712a8a08eef5805a46892932ad.woff. My web server serves static files from priv/static, so I want the requested path to just look like http://mysite.dev/fonts/myfile-fee66e712a8a08eef5805a46892932ad.woff.

I am committing assets/images and assets/fonts to source control, so I don't want all of the hashed files to end up in that directory.

Any help you could provide would be appreciated!


Solution

  • Looking at the file-loader documentation, the publicPath parameter seems to be what you need, though they don't go into a lot of detail. https://github.com/webpack-contrib/file-loader#filename-templates

    Here's a discussion on Github with a similar issue. https://github.com/webpack-contrib/file-loader/issues/32#issuecomment-250622904

    I think you should be able to do something like this:

    ../priv/static/fonts/[name]-[hash].[ext]&publicPath=/fonts/