Skip to content

Commit

Permalink
feat: 支持 ssr (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
gd4Ark authored Sep 29, 2020
1 parent 1e1d456 commit 82155f7
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- 支持云服务
- [x] 阿里云
- [x] 七牛
- 支持 SSR

## Install

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This component aims to replace native img element and use webp!
- Support cloud image service
- [x] Alibaba
- [x] Qiniu
- Support SSR

[⬆ Back to Top](#table-of-contents)

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
"lint": "eslint \"**/*.@(js|vue)\" --fix",
"release": "gren release --override"
},
"dependencies": {
"lazysizes": "^5.1.1"
},
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/plugin-transform-runtime": "^7.4.3",
Expand Down
1 change: 0 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'lazysizes/plugins/bgset/ls.bgset'
import getImageSrc from './provider-config'

function getSrc(config) {
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@
import Component from './v-img.vue'
import background from './background'
import placeholder from './spinner.svg'
import loadScript from './loadScript.js'

const defaultOptions = {
placeholder,
error:
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAABSCAMAAADw8nOpAAAArlBMVEUAAAAAAAD///9paWkyMjL////////////29vb////09PTn5+fh4eGvr6/////6+vqZmZm8vLz39/fj4+P8/PyBgYH////////////////Gxsb////////////v7+/MzMzr6+v///+4uLj////o6OhNTU3Y2NjQ0ND9/f35+fn////////////t7e3////////////////z8/Pb29v////y8vLw8PDU1NT////////ym0LiAA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3F3kZSUjU0zpbrFisSyUVLc9tTQuSCpuJWUYH9A7dCRlS0U+3bGCEbpcsa8VkoHSfmiG7dSxkgrWoPpRS7eGlQ1MG6b/c+uss1iFWsaH5fYZtU0g6XrxUzj/xKawbqW1Sx+mhX4dlNoWesba321lvvAIA8Lb5qExrq2/b/thee7vy2O2FTa/tdvUoLVzFMOdwkjvvvjcRY2KRdspZZ6g63UFRu+fc2Gr4NtaQeb5w62+B0cRHR0XHL9oU73R0/XH5B1c4zX/WEj5/f1R6LdH9xef7x8/SF410f1139Q+P+fPa74ONPBJ6Q/+TfzjGYmPq8AAAAASUVORK5CYII='
}

const lazysizes = 'https://cdn.jsdelivr.net/npm/lazysizes/lazysizes.min.js'
const bgset =
'https://cdn.jsdelivr.net/npm/lazysizes/plugins/bgset/ls.bgset.min.js'

// `Vue.use` automatically prevents you from using
// the same plugin more than once,
// so calling it multiple times on the same plugin
// will install the plugin only once
Component.install = (Vue, options = {}) => {
Vue.prototype.$vImg = {...defaultOptions, ...options}

if (typeof window !== 'undefined' && !window.lazySizes) {
Promise.all([loadScript(lazysizes), loadScript(bgset)]).catch(console.error)
}

Vue.component(Component.name, Component)

Vue.directive('img', {
Expand Down
24 changes: 24 additions & 0 deletions src/loadScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const loaded = new Set()

export default function(script) {
return new Promise((resolve, reject) => {
if (!script) {
reject(new Error('Script is not found'))
}
if (loaded.has(script)) {
resolve(true)
} else {
const loadScript = document.createElement('script')
loadScript.onload = function() {
loaded.add(script)
resolve(true)
}
loadScript.onerror = function(err) {
loadScript.remove()
reject(err)
}
loadScript.src = script
document.body.appendChild(loadScript)
}
})
}
7 changes: 4 additions & 3 deletions src/provider-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export const providerConfig = {

if (!autocrop || is(svg, src) || !src) return vm
const DPR = 2
let dpr = (window && window.devicePixelRatio) || DPR
let dpr =
(typeof window !== 'undefined' && window.devicePixelRatio) || DPR
if (dpr === 1) {
dpr = DPR
}
const actions = ['/resize']
const WIDTH = `w_${width * dpr}`
const HEIGHT = `h_${height * dpr}`
const WIDTH = `w_${parseInt(width * dpr)}`
const HEIGHT = `h_${parseInt(height * dpr)}`
const AUTOCROP = `m_fill`

if (isNaN(width) && isNaN(height)) {
Expand Down
1 change: 0 additions & 1 deletion src/v-img.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {providerConfig, default as getSrc} from './provider-config'
* 如果顺序是1 => 2,则是理想的
* 如果顺序是2 => 1,则图片会被请求两次:一是原始src;二是转换为webp的src
*/
import 'lazysizes'
const STATUS_IDLE = 0
// 目前没有必要区分 idle 和 loading,暂且保留标识符
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6749,11 +6749,6 @@ lazy-property@~1.0.0:
resolved "https://registry.npmjs.org/lazy-property/-/lazy-property-1.0.0.tgz#84ddc4b370679ba8bd4cdcfa4c06b43d57111147"
integrity sha1-hN3Es3Bnm6i9TNz6TAa0PVcREUc=

lazysizes@^5.1.1:
version "5.1.1"
resolved "https://registry.npm.taobao.org/lazysizes/download/lazysizes-5.1.1.tgz#7ca0266519d531ce740d6782e42faf700883646c"
integrity sha1-fKAmZRnVMc50DWeC5C+vcAiDZGw=

lcid@^2.0.0:
version "2.0.0"
resolved "https://registry.npm.taobao.org/lcid/download/lcid-2.0.0.tgz#6ef5d2df60e52f82eb228a4c373e8d1f397253cf"
Expand Down

0 comments on commit 82155f7

Please sign in to comment.