Skip to content

Commit d0e3c03

Browse files
authored
Merge pull request hilongjw#462 from caozhong1996/next
refactor(core): refactor code by ts and composition api
2 parents dd011bd + 763081e commit d0e3c03

21 files changed

+1922
-4608
lines changed

build.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const replace = require('@rollup/plugin-replace')
55
const { terser } = require('rollup-plugin-terser')
66
const resolve = require('rollup-plugin-node-resolve')
77
const commonjs = require('rollup-plugin-commonjs')
8+
const typescript = require('rollup-plugin-typescript')
89
const version = process.env.VERSION || require('./package.json').version
910

1011
const banner =
@@ -42,13 +43,15 @@ function blue (str) {
4243
}
4344

4445
build({
45-
input: path.resolve(__dirname, 'src/index.js'),
46+
input: path.resolve(__dirname, 'src/index.ts'),
47+
external: ['vue'],
4648
plugins: [
4749
resolve(),
4850
commonjs(),
51+
typescript(),
4952
babel({ runtimeHelpers: true }),
5053
replace({
51-
'__VUE_LAZYLOAD_VERSION__': JSON.stringify(version)
54+
__VUE_LAZYLOAD_VERSION__: JSON.stringify(version)
5255
}),
5356
terser()
5457
]
@@ -58,12 +61,14 @@ build({
5861
})
5962

6063
build({
61-
input: path.resolve(__dirname, 'src/index.js'),
64+
input: path.resolve(__dirname, 'src/index.ts'),
65+
external: ['vue'],
6266
plugins: [
6367
resolve(),
6468
commonjs(),
69+
typescript(),
6570
replace({
66-
'__VUE_LAZYLOAD_VERSION__': JSON.stringify(version)
71+
__VUE_LAZYLOAD_VERSION__: JSON.stringify(version)
6772
}),
6873
babel({ runtimeHelpers: true })
6974
]

package.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"browserslist": [
3030
"> 1%",
3131
"last 2 versions",
32-
"not ie <= 8"
32+
"not ie < 11"
3333
],
3434
"license": "MIT",
3535
"jest": {
@@ -39,9 +39,8 @@
3939
},
4040
"devDependencies": {
4141
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
42-
"@babel/preset-env": "^7.12.17",
43-
"@rollup/plugin-replace": "^2.3.4",
44-
"@vue/test-utils": "^2.0.0-rc.4",
42+
"@babel/preset-env": "^7.13.12",
43+
"@rollup/plugin-replace": "^2.4.2",
4544
"assign-deep": "^1.0.1",
4645
"babel-cli": "^6.26.0",
4746
"babel-core": "^6.26.3",
@@ -50,23 +49,26 @@
5049
"babel-preset-env": "^1.7.0",
5150
"babel-preset-stage-0": "^6.24.1",
5251
"babel-register": "^6.26.0",
53-
"chai": "^4.3.0",
54-
"eslint": "^4.19.1",
55-
"eslint-config-standard": "^11.0.0",
52+
"chai": "^4.3.4",
53+
"eslint": "^7.23.0",
54+
"eslint-config-standard": "^16.0.2",
5655
"eslint-plugin-import": "^2.22.1",
57-
"eslint-plugin-node": "^5.2.1",
58-
"eslint-plugin-promise": "^3.8.0",
59-
"eslint-plugin-standard": "^3.1.0",
56+
"eslint-plugin-node": "^11.1.0",
57+
"eslint-plugin-promise": "^4.3.1",
58+
"eslint-plugin-standard": "^5.0.0",
6059
"jest": "^26.6.3",
6160
"jest-canvas-mock": "^2.3.1",
62-
"mocha": "^4.0.1",
63-
"rollup": "^2.39.0",
61+
"mocha": "^8.3.2",
62+
"rollup": "^2.43.1",
6463
"rollup-plugin-babel": "^2.6.1",
6564
"rollup-plugin-commonjs": "^8.4.1",
6665
"rollup-plugin-node-resolve": "^3.4.0",
6766
"rollup-plugin-replace": "^2.2.0",
6867
"rollup-plugin-terser": "^7.0.2",
69-
"rollup-plugin-uglify": "^1.0.1",
70-
"vue": "^3.0.5"
68+
"rollup-plugin-uglify": "^6.0.4",
69+
"rollup-plugin-typescript": "^1.0.1",
70+
"tslib": "^2.1.0",
71+
"typescript": "^4.1.3",
72+
"vue": "^3.0.9"
7173
}
7274
}

src/index.js renamed to src/index.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ import Lazy from './lazy'
22
import LazyComponent from './lazy-component'
33
import LazyContainer from './lazy-container'
44
import LazyImage from './lazy-image'
5+
import { VueLazyloadOptions } from '../types/lazyload'
6+
import { App } from 'vue'
57

68
export default {
79
/*
8-
* install function
9-
* @param {Vue} Vue
10-
* @param {object} options lazyload options
11-
*/
12-
install (Vue, options = {}) {
13-
const LazyClass = Lazy(Vue)
14-
const lazy = new LazyClass(options)
15-
const lazyContainer = new LazyContainer({ lazy })
10+
* install function
11+
* @param {Vue} Vue
12+
* @param {object} options lazyload options
13+
*/
14+
install (Vue: App, options: VueLazyloadOptions = {}) {
15+
const lazy = new Lazy(options)
16+
const lazyContainer = new LazyContainer(lazy)
17+
18+
const vueVersion = Number(Vue.version.split('.')[0])
19+
if (vueVersion < 3) return new Error('Vue version at least 3.0')
20+
21+
Vue.config.globalProperties.$Lazyload = lazy
1622

1723
Vue.provide('Lazyload', lazy)
1824

@@ -37,10 +43,3 @@ export default {
3743
})
3844
}
3945
}
40-
41-
export {
42-
Lazy,
43-
LazyComponent,
44-
LazyImage,
45-
LazyContainer
46-
}

src/lazy-component.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/lazy-component.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import Lazy from './lazy'
2+
import {
3+
defineComponent,
4+
onMounted,
5+
onUnmounted,
6+
ref,
7+
Ref,
8+
reactive,
9+
computed,
10+
createVNode
11+
} from 'vue'
12+
import { useCheckInView } from './useCheckInView'
13+
14+
export default (lazy: Lazy) => {
15+
return defineComponent({
16+
props: {
17+
tag: {
18+
type: String,
19+
default: 'div'
20+
}
21+
},
22+
emits: ['show'],
23+
setup(props, { emit, slots }) {
24+
const el: Ref = ref(null)
25+
const state = reactive({
26+
loaded: false,
27+
error: false,
28+
attempt: 0
29+
})
30+
const show = ref(false)
31+
const { rect, checkInView } = useCheckInView(el, lazy.options.preLoad!)
32+
const load = () => {
33+
show.value = true
34+
state.loaded = true
35+
emit('show', show.value)
36+
}
37+
const vm = computed(() => {
38+
return {
39+
el: el.value,
40+
rect,
41+
checkInView,
42+
load,
43+
state,
44+
}
45+
})
46+
47+
onMounted(() => {
48+
lazy.addLazyBox(vm.value)
49+
lazy.lazyLoadHandler()
50+
})
51+
52+
onUnmounted(() => {
53+
lazy.removeComponent(vm.value)
54+
})
55+
56+
return () => createVNode(
57+
props.tag,
58+
{
59+
ref: el
60+
},
61+
[show.value && slots.default?.()]
62+
)
63+
}
64+
})
65+
}

src/lazy-container.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)