How to load a local video in React using webpack? How to load a local video in React using webpack? reactjs reactjs

How to load a local video in React using webpack?


I had the same problem, my solution is:

It uses https://github.com/webpack/html-loader and https://github.com/webpack/url-loader:

The loaders configuration:

  loaders: [{      test: /\.html$/,      loader: 'html-loader?attrs[]=video:src'    }, {      test: /\.mp4$/,      loader: 'url?limit=10000&mimetype=video/mp4'  }]

And in html:

A simple video tag, remeber webpack uses path referenced to the html file.

<video src="../../../assets/videos/cover1.mp4"></video>

This works for me.

References:https://github.com/webpack/html-loader/issues/28


Although this is an old post, and the question is specifically about WebPack...

If you're using Typescript and a boilerplate such as create-react-app, and if you have your video on the static folder, then please check out this solution, of adding the type to src/react-app-env.d.ts, before ejecting your app:

https://github.com/facebook/create-react-app/issues/6822