lazy-react 2.0.4 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +4 -2
- package/.eslintrc +36 -36
- package/README.md +1 -1
- package/demo/demo.js +39 -56
- package/demo/index.js +219 -5
- package/demo/index.js.LICENSE.txt +32 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +24 -19
- package/src/baseClass.js +65 -61
- package/src/lazyLoadBackgroundImage.js +16 -31
- package/src/lazyLoadComponent.js +20 -28
- package/src/lazyLoadFrame.js +17 -33
- package/src/lazyLoadImage.js +14 -31
- package/webpack.config.demo.js +16 -23
- package/webpack.config.js +70 -39
- package/.npmignore +0 -73
- package/src/indexSingleFile.js +0 -75
package/src/lazyLoadImage.js
CHANGED
|
@@ -1,34 +1,17 @@
|
|
|
1
|
-
import React from 'react' // eslint-disable-line no-unused-vars
|
|
2
|
-
import
|
|
1
|
+
import React, { useRef } from 'react' // eslint-disable-line no-unused-vars
|
|
2
|
+
import useIsInViewPort from './baseClass'
|
|
3
3
|
|
|
4
|
-
export default
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
render() {
|
|
9
|
-
return (
|
|
10
|
-
<img
|
|
11
|
-
src={this.state.link}
|
|
12
|
-
alt={this.props.alt}
|
|
13
|
-
style={this.style}
|
|
14
|
-
className={this.props.className}
|
|
15
|
-
ref={node => (this.domNode = node)}
|
|
16
|
-
/>
|
|
17
|
-
)
|
|
18
|
-
}
|
|
19
|
-
componentWillMount() {
|
|
20
|
-
if (this.props.className === '' && this.props.preserveAspect === false) {
|
|
21
|
-
this.style = {
|
|
22
|
-
minHeight: '300px',
|
|
23
|
-
minWidth: '300px',
|
|
24
|
-
...this.props.style
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
4
|
+
export default function LazyImage(props) {
|
|
5
|
+
const ref = useRef()
|
|
6
|
+
const isViewable = useIsInViewPort(ref, props)
|
|
29
7
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
8
|
+
return (
|
|
9
|
+
<img
|
|
10
|
+
src={isViewable.link}
|
|
11
|
+
alt={props.alt}
|
|
12
|
+
style={props.style}
|
|
13
|
+
className={props.className}
|
|
14
|
+
ref={ref}
|
|
15
|
+
/>
|
|
16
|
+
)
|
|
34
17
|
}
|
package/webpack.config.demo.js
CHANGED
|
@@ -1,26 +1,19 @@
|
|
|
1
1
|
var path = require('path')
|
|
2
|
-
var webpack = require('webpack')
|
|
3
2
|
|
|
4
3
|
module.exports = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
test: /\.js$/,
|
|
22
|
-
use: 'babel-loader',
|
|
23
|
-
exclude: /node_modules/
|
|
24
|
-
}]
|
|
25
|
-
}
|
|
26
|
-
}
|
|
4
|
+
mode:'development',
|
|
5
|
+
entry: './demo/demo.js',
|
|
6
|
+
output: {
|
|
7
|
+
path: path.join(__dirname),
|
|
8
|
+
filename: './demo/index.js'
|
|
9
|
+
},
|
|
10
|
+
module: {
|
|
11
|
+
rules: [
|
|
12
|
+
{
|
|
13
|
+
test: /\.js$/,
|
|
14
|
+
use: 'babel-loader',
|
|
15
|
+
exclude: /node_modules/
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
}
|
package/webpack.config.js
CHANGED
|
@@ -2,43 +2,74 @@ var path = require('path')
|
|
|
2
2
|
var webpack = require('webpack')
|
|
3
3
|
|
|
4
4
|
module.exports = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
5
|
+
mode:'production',
|
|
6
|
+
entry: './src/index.js',
|
|
7
|
+
devtool: 'source-map',
|
|
8
|
+
output: {
|
|
9
|
+
library: 'LazyReact',
|
|
10
|
+
libraryTarget: 'umd',
|
|
11
|
+
path: path.join(__dirname),
|
|
12
|
+
filename: './dist/index.js'
|
|
13
|
+
},
|
|
14
|
+
// optimization: {
|
|
15
|
+
// minimizer: [
|
|
16
|
+
// new UglifyJsPlugin({
|
|
17
|
+
// sourceMap: true,
|
|
18
|
+
// uglifyOptions: {
|
|
19
|
+
// compress: {
|
|
20
|
+
// inline: false
|
|
21
|
+
// },
|
|
22
|
+
// output: {
|
|
23
|
+
// comments: false
|
|
24
|
+
// }
|
|
25
|
+
// }
|
|
26
|
+
// })
|
|
27
|
+
// ],
|
|
28
|
+
// runtimeChunk: false,
|
|
29
|
+
// splitChunks: {
|
|
30
|
+
// cacheGroups: {
|
|
31
|
+
// default: false,
|
|
32
|
+
// commons: {
|
|
33
|
+
// test: /[\\/]node_modules[\\/]/,
|
|
34
|
+
// name: 'vendor_app',
|
|
35
|
+
// chunks: 'all',
|
|
36
|
+
// minChunks: 2
|
|
37
|
+
// }
|
|
38
|
+
// }
|
|
39
|
+
// }
|
|
40
|
+
// },
|
|
41
|
+
plugins: [
|
|
42
|
+
new webpack.DefinePlugin({
|
|
43
|
+
'process.env': {
|
|
44
|
+
NODE_ENV: '"production"'
|
|
45
|
+
},
|
|
46
|
+
__DEVELOPMENT__: false
|
|
47
|
+
})],
|
|
48
|
+
externals: [
|
|
49
|
+
{
|
|
50
|
+
react: {
|
|
51
|
+
root: 'React',
|
|
52
|
+
commonjs2: 'react',
|
|
53
|
+
commonjs: 'react',
|
|
54
|
+
amd: 'react'
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
'react-dom': {
|
|
59
|
+
root: 'ReactDOM',
|
|
60
|
+
commonjs2: 'react-dom',
|
|
61
|
+
commonjs: 'react-dom',
|
|
62
|
+
amd: 'react-dom'
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
],
|
|
66
|
+
module: {
|
|
67
|
+
rules: [
|
|
68
|
+
{
|
|
69
|
+
test: /\.js$/,
|
|
70
|
+
use: 'babel-loader',
|
|
71
|
+
exclude: /node_modules/
|
|
72
|
+
}
|
|
73
|
+
]
|
|
74
|
+
}
|
|
44
75
|
}
|
package/.npmignore
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# Created by https://www.gitignore.io/api/osx,sublimetext,node
|
|
2
|
-
|
|
3
|
-
### OSX ###
|
|
4
|
-
.DS_Store
|
|
5
|
-
.AppleDouble
|
|
6
|
-
.LSOverride
|
|
7
|
-
|
|
8
|
-
# Icon must end with two \r
|
|
9
|
-
Icon
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
# Thumbnails
|
|
13
|
-
._*
|
|
14
|
-
|
|
15
|
-
# Files that might appear in the root of a volume
|
|
16
|
-
.DocumentRevisions-V100
|
|
17
|
-
.fseventsd
|
|
18
|
-
.Spotlight-V100
|
|
19
|
-
.TemporaryItems
|
|
20
|
-
.Trashes
|
|
21
|
-
.VolumeIcon.icns
|
|
22
|
-
|
|
23
|
-
# Directories potentially created on remote AFP share
|
|
24
|
-
.AppleDB
|
|
25
|
-
.AppleDesktop
|
|
26
|
-
Network Trash Folder
|
|
27
|
-
Temporary Items
|
|
28
|
-
.apdisk
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
### SublimeText ###
|
|
32
|
-
# cache files for sublime text
|
|
33
|
-
*.tmlanguage.cache
|
|
34
|
-
*.tmPreferences.cache
|
|
35
|
-
*.stTheme.cache
|
|
36
|
-
|
|
37
|
-
# workspace files are user-specific
|
|
38
|
-
*.sublime-workspace
|
|
39
|
-
|
|
40
|
-
# project files should be checked into the repository, unless a significant
|
|
41
|
-
# proportion of contributors will probably not be using SublimeText
|
|
42
|
-
# *.sublime-project
|
|
43
|
-
|
|
44
|
-
# sftp configuration file
|
|
45
|
-
sftp-config.json
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
### Node ###
|
|
49
|
-
# Logs
|
|
50
|
-
logs
|
|
51
|
-
*.log
|
|
52
|
-
npm-debug.log*
|
|
53
|
-
|
|
54
|
-
# Runtime data
|
|
55
|
-
pids
|
|
56
|
-
*.pid
|
|
57
|
-
*.seed
|
|
58
|
-
|
|
59
|
-
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
60
|
-
lib-cov
|
|
61
|
-
|
|
62
|
-
# Coverage directory used by tools like istanbul
|
|
63
|
-
coverage
|
|
64
|
-
|
|
65
|
-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
|
66
|
-
.grunt
|
|
67
|
-
|
|
68
|
-
# node-waf configuration
|
|
69
|
-
.lock-wscript
|
|
70
|
-
|
|
71
|
-
# Dependency directory
|
|
72
|
-
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
|
|
73
|
-
node_modules
|
package/src/indexSingleFile.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
//this file it's here just for reference on how build size differs with webpack and babel when there is one or more files involved
|
|
2
|
-
|
|
3
|
-
import React from 'react' // eslint-disable-line no-unused-vars
|
|
4
|
-
import ReactDom from 'react-dom'
|
|
5
|
-
|
|
6
|
-
class CheckIfRender extends React.Component {
|
|
7
|
-
constructor(props) {
|
|
8
|
-
super(props)
|
|
9
|
-
this.state = {
|
|
10
|
-
link: ''
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
listener() {
|
|
14
|
-
if (window.scrollY + window.innerWidth + 100 > this.state.top) {
|
|
15
|
-
this.setState({ link: this.props.link })
|
|
16
|
-
this.removeListener()
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
removeListener() {
|
|
20
|
-
window.removeEventListener('scroll', this.listener)
|
|
21
|
-
}
|
|
22
|
-
componentDidMount() {
|
|
23
|
-
const element = ReactDom.findDOMNode(this)
|
|
24
|
-
const { top } = element.getBoundingClientRect()
|
|
25
|
-
this.setState({ top })
|
|
26
|
-
window.addEventListener('scroll', this.listener)
|
|
27
|
-
}
|
|
28
|
-
componentWillUnmount() {
|
|
29
|
-
this.removeListener()
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
class LazyFrame extends CheckIfRender {
|
|
34
|
-
constructor(props) {
|
|
35
|
-
super(props)
|
|
36
|
-
}
|
|
37
|
-
render() {
|
|
38
|
-
return (
|
|
39
|
-
<iframe
|
|
40
|
-
height={this.props.height || '500'}
|
|
41
|
-
scrolling={this.props.scrolling || 'no'}
|
|
42
|
-
src={this.state.link}
|
|
43
|
-
frameBorder={this.props.frameBorder || 'no'}
|
|
44
|
-
allowTransparency={this.props.allowTransparency || 'true'}
|
|
45
|
-
allowFullScreen={this.props.allowFullScreen || 'true'}
|
|
46
|
-
style={this.props.style || { width: '100%' }}
|
|
47
|
-
/>
|
|
48
|
-
)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
class LazyBackgroundImage extends CheckIfRender {
|
|
53
|
-
constructor(props) {
|
|
54
|
-
super(props)
|
|
55
|
-
}
|
|
56
|
-
render() {
|
|
57
|
-
return (
|
|
58
|
-
<div
|
|
59
|
-
className={this.props.className}
|
|
60
|
-
style={{ backgroundImage: `url(${this.state.link})` }}
|
|
61
|
-
/>
|
|
62
|
-
)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
class LazyImage extends CheckIfRender {
|
|
67
|
-
constructor(props) {
|
|
68
|
-
super(props)
|
|
69
|
-
}
|
|
70
|
-
render() {
|
|
71
|
-
return <img src={this.state.link} alt={this.props.alt} />
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export { LazyBackgroundImage, LazyImage, LazyFrame }
|