Skip to content

Commit 45635ef

Browse files
committed
The return of single player imports
Refactors `src/ReactPlayer.js` to be a `createReactPlayer` factory that takes an array of players The default import then becomes `src/index.js` which just calls `createReactPlayer` with the whole players array from `src/players/index.js` A script now generates top level files before publishing, eg `youtube.js` that call `createReactPlayer` with just the `YouTube` player
1 parent 45369bb commit 45635ef

14 files changed

+220
-155
lines changed

MIGRATING.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,31 @@ Breaking changes are in 🔥 __bold and on fire__.
44

55
### Lazy players
66

7-
ReactPlayer v2.0 uses [React lazy loading](https://reactjs.org/docs/code-splitting.html#reactlazy) to only load the players required based on the `url` prop passed in. Previous versions of ReactPlayer would include the code for all players, regardless of what type of player is used.
7+
As of `v2.2`, if your build system supports `import()` statements, use `react-player/lazy` to [lazy load](https://reactjs.org/docs/code-splitting.html#reactlazy) the appropriate player for the `url` you pass in. This adds several `reactPlayer` chunks to your output, but reduces your main bundle size.
88

9-
Because of this, 🔥 __single player imports are now redundant, and have been removed__. Instead of importing single players, you can safely import from `react-player` and only the relevant player will be loaded if you only use one type of `url`.
9+
Due to the use of `lazy` and `Suspense`, 🔥 __React 16.6 or later is now required__.
1010

1111
```jsx
1212
// Before
13-
import YouTubePlayer from 'react-player/lib/players/YouTube'
13+
import ReactPlayer from 'react-player'
1414

1515
// After
16-
import YouTubePlayer from 'react-player'
16+
import ReactPlayer from 'react-player/lazy'
1717
```
1818

19-
Due to the use of `lazy` and `Suspense`, 🔥 __React 16.6 or later is now required__.
19+
Lazy players were the default import in `v2.1`, but moved to `react-player/lazy` in `v2.2` to avoid causing problems with common build systems.
20+
21+
### Single player imports
22+
23+
As of `v2.2`, the 🔥 __location of single player imports has changed__. Single players are not available in `v2.0` and `v2.1`.
24+
25+
```jsx
26+
// Before
27+
import ReactPlayer from 'react-player/lib/players/YouTube'
28+
29+
// After
30+
import ReactPlayer from 'react-player/youtube'
31+
```
2032

2133
### Preloading
2234

README.md

+17-10
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,40 @@
2323

2424
### Migrating to ReactPlayer `v2.0`
2525

26-
ReactPlayer `v2.0` removes single player imports in favour of lazy loading players. Support for `preload` has also been removed, plus some other changes. See [`MIGRATING.md`](/MIGRATING.md) for information.
26+
ReactPlayer `v2.0` changes single player imports and adds lazy loading players. Support for `preload` has also been removed, plus some other changes. See [`MIGRATING.md`](/MIGRATING.md) for information.
2727

2828
### Usage
2929

3030
```bash
31-
npm install react-player
31+
npm install react-player # or yarn add react-player
3232
```
3333

3434
```jsx
3535
import React from 'react'
3636
import ReactPlayer from 'react-player'
3737

38-
const App = () => (
39-
<ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' />
40-
)
38+
// Render a YouTube video player
39+
<ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' />
4140
```
4241

43-
If your build system supports `import()` statements, use `react-player/lazy` to lazy load the appropriate player for the `url` you pass in. This will add several `reactPlayer` chunks to your bundle, but reduce your main bundle size.
42+
By default, ReactPlayer supports [many different types](#supported-media) of `url`. If you only ever use one type, use imports such as `react-player/youtube` to reduce your bundle size. See [config keys](#config-prop) for all player keys.
4443

4544
```jsx
4645
import React from 'react'
47-
import ReactPlayer from 'react-player/lazy'
46+
import ReactPlayer from 'react-player/youtube'
4847

4948
// Only loads the YouTube player
50-
const App = () => (
51-
<ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' />
52-
)
49+
<ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' />
50+
```
51+
52+
If your build system supports `import()` statements, use `react-player/lazy` to lazy load the appropriate player for the `url` you pass in. This adds several `reactPlayer` chunks to your output, but reduces your main bundle size.
53+
54+
```jsx
55+
import React from 'react'
56+
import ReactPlayer from 'react-player/lazy'
57+
58+
// Lazy load the YouTube player
59+
<ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' />
5360
```
5461

5562
Demo page: [`https://cookpete.com/react-player`](https://cookpete.com/react-player)

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-player",
33
"version": "2.1.1",
44
"description": "A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion",
5-
"main": "lib/ReactPlayer.js",
5+
"main": "lib/index.js",
66
"typings": "index.d.ts",
77
"scripts": {
88
"clean": "rimraf lib lazy demo coverage",
@@ -19,8 +19,8 @@
1919
"build:standalone": "cross-env NODE_ENV=production webpack --config webpack/standalone.babel.js",
2020
"preversion": "npm run lint && npm run test",
2121
"version": "auto-changelog -p && npm run build:dist && npm run build:standalone && git add CHANGELOG.md dist",
22-
"prepublishOnly": "npm run build:lib && npm run build:lazy && npm run build:dist",
23-
"postpublish": "npm run clean"
22+
"prepublishOnly": "npm run build:lib && npm run build:lazy && npm run build:dist && node scripts/pre-publish.js",
23+
"postpublish": "node scripts/post-publish.js && npm run clean"
2424
},
2525
"repository": {
2626
"type": "git",

scripts/post-publish.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { join } = require('path')
2+
const { unlink } = require('fs').promises
3+
const { default: players } = require('../lib/players')
4+
5+
const deleteSinglePlayers = async () => {
6+
for (const { key } of players) {
7+
await unlink(join('.', `${key}.js`))
8+
}
9+
}
10+
11+
deleteSinglePlayers()

scripts/pre-publish.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { join } = require('path')
2+
const { writeFile } = require('fs').promises
3+
const { default: players } = require('../lib/players')
4+
5+
const generateSinglePlayers = async () => {
6+
for (const { key, name } of players) {
7+
const file = `
8+
const { createReactPlayer } = require('./lib/ReactPlayer')
9+
const Player = require('./lib/players/${name}').default
10+
module.exports = createReactPlayer([Player])
11+
`
12+
await writeFile(join('.', `${key}.js`), file)
13+
}
14+
}
15+
16+
generateSinglePlayers()

0 commit comments

Comments
 (0)