I can't seem to figure out how to get an html5 video to render in a react app from local files. Literally the only way I've been able to get this to work is like this:
<video src="http://www.w3schools.com/html/movie.mp4" controls />
Here's what I've tried
1. Including the path directly
<video src={require('path/to/file.mp4')} controls />
which returns an error
Module parse failed: /path/to/file.mp4 Line 1: Unexpected token ILLEGAL
You may need an appropriate loader to handle this file type.
2. Adding these loaders one at a time to the webpack config
{
test: /\.(mp4)$/,
loader: 'file'
// loader: 'url-loader'
// loader: 'url-loader?limit=100000'
// loader: 'file-loader'
// loader: 'file-loader?name=videos/[name].[ext]'
},
this spit out the following error in the browser
GET http://localhost:3000/530c2bf99dad0f857d46940b62b84946.mp4 404 (Not Found)
3. I tried adding a direct url to the file
<video src={require('http://localhost:3000/path/to/file.mp4')} controls />
but still errors:
Module not found: Error: Cannot resolve module 'http://localhost:3000/path/to/file.mp4' in path/to/file.mp4
4. I tried adding the mp4 extension in my webpack config like this person did
{
test: /\.jpe?g$|\.gif$|\.png$|\.ico|\.svg$|\.woff$|\.ttf$|.mp4$/,
loader: 'url-loader?limit=8192'
}
but no luck
5. I started implementing webpack-isomorphic-tools but then I wasn't sure if this was the right direction so I paused on it. It seems like this guy got it working this way. (see file)
I also noticed in the webpack documentation under loader conventions that file-loader
will load video files.
Does this mean webpack won't load other video types such as .mov, .vob, .avi, etc.
?
If you want to take a look at the code, here's the repository.
Resources
test
passes. – Komarek/\.(png|jpg|jpeg|gif|svg|mp4)$/
. – Ollie.
was not escaped, but I don't think it is the origin of the problem, i.e. not wrong. What do you get in thesrc
when using solution 4? – Ollie.
must be escaped, but that could only lead to false-positives, that is not a problem at the moment. – Komarekimport videoUrl from '/path/to/file.mp4'
and then inside jsx put<video src={videoUrl} />
. – Typecastimport
statement didn't work. It still gave the same error as solution 2. – Hanumanvar x = require('path/to/video')
. I also used thename=videos/[name].[ext]
, may be related. Also make sure you start and stop webpack if you're watching files. – Radionuclide