Open DouglasDev opened 2 years ago
It seems like my implementation of lazy loading players is prone to errors. I am open to better suggestions.
Would it be better to just have a component that accepts a players
array and you just import anything that you need? eg:
import ReactPlayerCustom from 'react-player/custom'
import Youtube from 'react-player/youtube'
import Facebook from 'react-player/facebook'
<ReactPlayerCustom players={[Youtube, Facebook]} etc etc />
I think the current api is good. I have an app where a user can enter any video url so it would be good if react player could automatically figure out which player to load (like it's doing now) since I can't know in advance. If I have time today, maybe I can figure out what the actual cause of the error is and open a PR.
@cookpete That looks great. We import everything cause of that problem, but only use file
with hls
at radio.net.
Also disableDeferredLoading
should be documented. :)
@cmaerz I have to ask… why not just use hls.js
directly instead of importing this whole library?
@cookpete We mostly play radio streams i guess that mostly uses either just the file player or the hls player. I had problems with just importing the file player, but tbh I don't really know why.
@cookpete Haven't had time to write a fix but I think the issue is that vite uses native ESM imports whereas ReactPlayer appears to use ESM in the source code but exports CommonJS in the npm package. So I think just adding an untransformed ESM version to the npm package would fix the bug.
If it may help, problem stems from dynamic imports of players/index
var _default = [{
key: "youtube",
name: "YouTube",
canPlay: _patterns.canPlay.youtube,
lazyPlayer: (0, _react.lazy)(function() {
return import(
/* webpackChunkName: 'reactPlayerYouTube' */
"./YouTube-PILJPAEI.js"
).then(m => {
console.log('lazy vite')
/*
Here in vite, m is a module
m.default contains { default: fn, __esModule: true }
in wp, returning m is ok, in vite it is not
*/
// below fix for vite
return m.default
});
})
}
A workaround would be e.g to lazyPlayer: lazy(() => import(/* webpackChunkName: 'reactPlayerYouTube' */'./YouTube').then(m => m.default.default ? m.default : m))
pretty much what does
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { "default": obj };
}
This feels a lot intrusive in source code so I guess an esm export would be preferrable (as @DouglasDev suggested)?
We are also facing the same issue with Vite in dev (esbuild). I think an ESM export would be the best way forward too.
I'm getting the same error. Anyone with a solution?
I'm still having this issue using React Player in an app that builds with Vite. App crashes in dev but works in production using react-player/lazy. Is there a workaround for this?
Hi @cookpete! Do you think we could reopen this issue (at least for clarity sake)? Thank you so much!
I also have this issue. For now, we're not using lazy
but we definitely experienced the crash after moving to Vite from CRA.
Would using @loadable/component
be interesting in this case instead of lazy? I tried locally and it seems to work.
Maybe it's a little bit too late but in my case the import of lazy component throw general error about not found the start template. I realizied the solution:
import { Suspense, lazy, useEffect, useState } from 'react' import { BaseReactPlayerProps } from 'react-player/base'
`
const Loader = () => {
return (
)
} `
const VideoPlayer = (props: BaseReactPlayerProps) => {
const [Component, setComponent] = useState<React.ComponentType<BaseReactPlayerProps> | null>(null);
useEffect(() => {
setComponent(() => lazy(() => import('react-player/lazy')))
}, [])
if (!Component) {
return <Loader />;
}
return (
<Suspense fallback={<Loader />}>
<Component {...props} />
</Suspense>
)
}
export default VideoPlayer;
I've used node 20.x, vite 5.0, react 18.2, react-player 2.16.0, together with the Laravel / Inertia stack. Hope this would help someone.
this should be fixed in v3.0.0-canary.3 https://www.npmjs.com/package/react-player/v/3.0.0-canary.3 https://codesandbox.io/p/devbox/react-player-vite-6n78q7
note that this version has only ESM builds for now, we are testing if CJS or IIFE is actually still needed.
@luwes any idea why my import/linter shows an error after updating to v3.0.0-canary.3?
import ReactPlayer from 'react-player/lazy'
ESLint: Unable to resolve path to module 'react-player/ lazy'.(import/ no-unresolved)
Current Behavior
The app crashes with the following error:
Expected Behavior
I expect the video to load
Steps to Reproduce
npm i
npm run dev
Environment