Serving mp3 files using the webpack file loader

Øyvind Bråthen picture Øyvind Bråthen · Jul 7, 2016 · Viewed 16k times · Source

I have a problem with getting my mp3 files to work using the webpack file loader.

This is the issue:

I have a mp3 file on my harddisk, that if I open using chrome by for example "c:\somefolder\somemp3file.mp3" opens up in a tab in the browser and plays just fine.

However, when I serve the exact same file using webpack it does not work. I have configured the loader in webpack like this:

{
    test: /\.(gif|jpg|png|mp3|aac|ogg)$/,
    loader: 'file'
}

Then, when I'm trying to link to the file I require it inside my javascript, like this:

require('file!./../content/somemp3file.mp3');

this correctly returns me the url for the mp3 file.

If I try to manually go to localhost:8080 followed by the url returned by require, the mp3 player in Chrome pops up like I expect it to, but the file cannot be played, and it's not possible to click on the play button like the file is corrupted or something.

Anyone knows what I'm doing wrong here?

Answer

nawaz1989 picture nawaz1989 · Dec 15, 2016

Use file-loader like this:

{
    test: /\.mp3$/,
    include: SRC,
    loader: 'file-loader'
}