Load video mp4 webpack loader
Asked Answered
L

2

11

How to use loader for mp4 video format with webpcak 4, I try like this:

       {
            test: /\.mp4$/,
            use: 'file-loader',
            loader: 'file-loader?name=videos/[name].[ext]',
        },

and import like this

import pressButtonAnimated from './images/pressButtonAnimated.mp4'

But it does not work for me, and I get an error You may need an appropriate loader to handle this file type.

But this one is work for me, but I don wanna add in each file

import pressButtonAnimated from '-!file-loader!./images/pressButtonAnimated.mp4'
Lacielacing answered 20/4, 2018 at 13:59 Comment(0)
M
19

The way how you declare your loader is not right. You are mixing two ways to define loaders.

 {
        test: /\.mp4$/,
        use: 'file-loader?name=videos/[name].[ext]',
 },

Could you try this please.

Link: https://webpack.js.org/concepts/loaders/

Mcconnell answered 20/4, 2018 at 16:48 Comment(0)
R
3

with Webpack5, the attributes property has been replaced with sources

We can use file-loader like this so we can give it the option name and path.

{
    test: /\.mp4$/,
    use: [
        {
            loader: "file-loader",
            options: {
                name: "[name].[ext]",
                outputPath: "video"
            }
        }
    ]
}

also for some reason if you write a video tag in your html

<video class="">
    <source src="video/video.mp4" type="video/mp4">
</video>

It doesn't load in webpack but we can use html-loader as follow

{
    test: /\.html$/,
    exclude: /node_modules/,
    use: [
        {
            loader: "html-loader",
            options: {
                sources: {
                    list: [
                        {
                            tag: "source",
                            attribute: "src",
                            type: "src"
                        }
                    ]
                }
            }
        }
    ]
}

also we can use url-loader but it only load the images and videos added through css.

PS: if you found a solution for html tags with src attribute to use it with file-loader it would be great.

Riggle answered 8/1, 2021 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.