Skip to content

Commit

Permalink
Move lazy loading players to react-player/lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Webmaster1116 committed Jun 7, 2020
1 parent 3d5cb15 commit 96f435c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ yarn-error.log
package-lock.json
.DS_Store
/lib
/lazy
/demo
/coverage
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,28 @@ ReactPlayer `v2.0` removes single player imports in favour of lazy loading playe
### Usage

```bash
npm install react-player --save
# or
yarn add react-player
npm install react-player
```

```js
import React, { Component } from 'react'
```jsx
import React from 'react'
import ReactPlayer from 'react-player'

class App extends Component {
render () {
return <ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' playing />
}
}
const App = () => (
<ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' />
)
```

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.

```jsx
import React from 'react'
import ReactPlayer from 'react-player/lazy'

// Only loads the YouTube player
const App = () => (
<ReactPlayer url='https://www.youtube.com/watch?v=ysz5S6PUM-U' />
)
```

Demo page: [`https://cookpete.com/react-player`](https://cookpete.com/react-player)
Expand Down
28 changes: 28 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const getExcludes = lazy => {
if (lazy) {
// Disable transforming `import()` statements to enable lazy players
return ['@babel/plugin-proposal-dynamic-import']
}
return []
}

module.exports = {
presets: [
[
'@babel/preset-env',
{ exclude: getExcludes(process.env.LAZY) }
],
'@babel/preset-react'
],
plugins: [
'react-hot-loader/babel',
'@babel/plugin-proposal-class-properties'
],
env: {
test: {
plugins: [
'istanbul'
]
}
}
}
29 changes: 3 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
"main": "lib/ReactPlayer.js",
"typings": "index.d.ts",
"scripts": {
"clean": "rimraf lib demo coverage",
"clean": "rimraf lib lazy demo coverage",
"start": "webpack-dev-server --config webpack/config.babel.js",
"lint": "standard --verbose | snazzy",
"lint:fix": "standard --fix",
"test": "cross-env NODE_ENV=test ava",
"test:coverage": "cross-env NODE_ENV=test nyc ava",
"test:codecov": "nyc report --reporter=json && codecov -f coverage/coverage-final.json",
"build:lib": "cross-env NODE_ENV=production babel src -d lib --ignore src/demo",
"build:lazy": "cross-env NODE_ENV=production LAZY=true babel src -d lazy --ignore src/demo",
"build:demo": "cross-env NODE_ENV=production webpack --config webpack/production.babel.js",
"build:dist": "cross-env NODE_ENV=production webpack --config webpack/dist.babel.js",
"build:standalone": "cross-env NODE_ENV=production webpack --config webpack/standalone.babel.js",
"preversion": "npm run lint && npm run test",
"version": "auto-changelog -p && npm run build:dist && npm run build:standalone && git add CHANGELOG.md dist",
"prepublishOnly": "npm run build:lib && npm run build:dist",
"prepublishOnly": "npm run build:lib && npm run build:lazy && npm run build:dist",
"postpublish": "npm run clean"
},
"repository": {
Expand Down Expand Up @@ -102,30 +103,6 @@
"prop-types": "^15.7.2",
"react-fast-compare": "^3.0.1"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"exclude": [
"@babel/plugin-proposal-dynamic-import"
]
}
],
"@babel/preset-react"
],
"plugins": [
"react-hot-loader/babel",
"@babel/plugin-proposal-class-properties"
],
"env": {
"test": {
"plugins": [
"istanbul"
]
}
}
},
"postcss": {
"plugins": {
"autoprefixer": {},
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ReactPlayer from './ReactPlayer'

export default ReactPlayer

0 comments on commit 96f435c

Please sign in to comment.