Skip to content

Commit 8b16d59

Browse files
feat: separate global build without blurhash
1 parent 50a3535 commit 8b16d59

File tree

14 files changed

+232
-202
lines changed

14 files changed

+232
-202
lines changed

docs/guide/installation.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ unlazy can be used without a build step. Useful for prototyping or when you don'
3838
- ESM build: <CdnLink name="unlazy.js" />
3939
- Must be used with `<script type="module">`
4040

41+
:::info
42+
To keep the size of the bundle small, the global build does not include BlurHash decoding. If you want to use BlurHash, you must import the `unlazy.blurhash.iife.js` or `unlazy.blurhash.js` files instead:
43+
44+
- Global build: <CdnLink name="unlazy.blurhash.iife.js" />
45+
- ESM build: <CdnLink name="unlazy.blurhash.js" />
46+
:::
47+
4148
### Auto-Initialization
4249

4350
When using the global build, you can use the `init` attribute to automatically initialize and watch all elements that have a `loading="lazy"` attribute:

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
"devDependencies": {
1919
"@antfu/eslint-config": "^0.38.5",
2020
"@antfu/eslint-config-react": "^0.38.5",
21-
"@types/node": "^18.15.12",
21+
"@types/node": "^18.15.13",
2222
"bumpp": "^9.1.0",
2323
"eslint": "^8.38.0",
2424
"happy-dom": "^9.8.4",
2525
"typescript": "^5.0.4",
2626
"unbuild": "^1.2.1",
27-
"vite": "^4.3.0",
27+
"vite": "^4.3.1",
2828
"vitest": "^0.30.1",
29-
"vue-tsc": "^1.4.0"
29+
"vue-tsc": "^1.4.1"
3030
}
3131
}

packages/core/src/blurhash.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { decodeBlurHash } from 'fast-blurhash'
22
import { DEFAULT_BLURHASH_SIZE } from './constants'
3-
import { calculateDimensions, isSSR } from './utils'
3+
import { calculateDimensions, getDataUriFromArr, isSSR } from './utils'
44
import { createBlurryImageSvg, encodeSvgAsDataUri } from './utils/image/svg'
55
import type { UnLazyLoadOptions } from './types'
66
import { getPngFromRgbaArr } from './utils/image'
@@ -51,3 +51,27 @@ export function createSvgDataUri(
5151

5252
return encodeSvgAsDataUri(svg)
5353
}
54+
55+
export function applyBlurhashPlaceholder(
56+
image: HTMLImageElement,
57+
blurhash: string,
58+
blurhashSize: number,
59+
) {
60+
// Preserve the original image's aspect ratio
61+
const actualWidth = image.width || image.offsetWidth || blurhashSize
62+
const actualHeight = image.height || image.offsetHeight || blurhashSize
63+
const { width, height } = calculateDimensions(
64+
actualWidth / actualHeight,
65+
blurhashSize,
66+
)
67+
68+
// Generate the blurry placeholder
69+
try {
70+
const pixels = decodeBlurHash(blurhash, width, height)
71+
const placeholder = getDataUriFromArr(pixels, width, height)
72+
image.src = placeholder
73+
}
74+
catch (error) {
75+
console.error('Error generating blurry placeholder:', error)
76+
}
77+
}

packages/core/src/lazyLoad.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { decodeBlurHash } from 'fast-blurhash'
21
import { DEFAULT_BLURHASH_SIZE } from './constants'
3-
import { calculateDimensions, getDataUriFromArr, isCrawler, isLazyLoadingSupported, toElementsArr } from './utils'
2+
import { isCrawler, isLazyLoadingSupported, isSSR, toElementsArr } from './utils'
3+
import { applyBlurhashPlaceholder } from './blurhash'
44
import type { UnLazyLoadOptions } from './types'
55

6+
// Compile-time flag to exclude blurhash from IIFE bundle
7+
const __ENABLE_BLURHASH__ = true
8+
69
export function lazyLoad<T extends HTMLImageElement>(
710
/**
811
* A CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements to lazy-load.
@@ -23,9 +26,11 @@ export function lazyLoad<T extends HTMLImageElement>(
2326
updateSizesAttribute(image)
2427

2528
// Generate the blurry placeholder from a Blurhash string if applicable
26-
const _blurhash = blurhash === true ? image.dataset.blurhash : blurhash
27-
if (_blurhash)
28-
applyBlurhashPlaceholder(image, _blurhash, blurhashSize)
29+
if (__ENABLE_BLURHASH__) {
30+
const _blurhash = blurhash === true ? image.dataset.blurhash : blurhash
31+
if (_blurhash && !isSSR)
32+
applyBlurhashPlaceholder(image, _blurhash, blurhashSize)
33+
}
2934

3035
// Bail if the image doesn't provide a `data-src` or `data-srcset` attribute
3136
if (!image.dataset.src && !image.dataset.srcset)
@@ -122,27 +127,3 @@ function updatePictureSources(image: HTMLImageElement) {
122127
if (picture?.tagName.toLowerCase() === 'picture')
123128
[...picture.querySelectorAll<HTMLSourceElement>('source[data-srcset]')].forEach(updateImageSrcset)
124129
}
125-
126-
function applyBlurhashPlaceholder(
127-
image: HTMLImageElement,
128-
blurhash: string,
129-
blurhashSize: number,
130-
) {
131-
// Preserve the original image's aspect ratio
132-
const actualWidth = image.width || image.offsetWidth || blurhashSize
133-
const actualHeight = image.height || image.offsetHeight || blurhashSize
134-
const { width, height } = calculateDimensions(
135-
actualWidth / actualHeight,
136-
blurhashSize,
137-
)
138-
139-
// Generate the blurry placeholder
140-
try {
141-
const pixels = decodeBlurHash(blurhash, width, height)
142-
const placeholder = getDataUriFromArr(pixels, width, height)
143-
image.src = placeholder
144-
}
145-
catch (error) {
146-
console.error('Error generating blurry placeholder:', error)
147-
}
148-
}

packages/nuxt/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"devDependencies": {
5959
"@nuxt/module-builder": "^0.3.0",
6060
"@nuxt/schema": "^3.4.2",
61-
"@types/node": "^18.15.12",
61+
"@types/node": "^18.15.13",
6262
"@unhead/vue": "^1.1.26",
6363
"@unocss/nuxt": "^0.51.5",
6464
"@unocss/reset": "^0.51.5",

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"unlazy": "workspace:*"
5454
},
5555
"devDependencies": {
56-
"@types/node": "^18.15.12",
56+
"@types/node": "^18.15.13",
5757
"@types/react": "^18.0.37",
5858
"@types/react-dom": "^18.0.11",
5959
"@vitejs/plugin-react": "^4.0.0",

packages/solid/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"unlazy": "workspace:*"
5555
},
5656
"devDependencies": {
57-
"@types/node": "^18.15.12",
57+
"@types/node": "^18.15.13",
5858
"fast-blurhash": "^1.1.2",
5959
"solid-js": "^1.7.3",
6060
"vite-plugin-solid": "^2.7.0"

packages/unlazy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"scripts": {
4747
"jiti-fix": "find ./dist -type f -exec sed -i '' 's|file://||g' {} \\;",
4848
"dev": "unbuild --stub && npm run jiti-fix",
49-
"build": "unbuild && vite build",
49+
"build": "unbuild && vite build -c vite.config.bundle.ts && vite build -c vite.config.bundle.blurhash.ts",
5050
"test:types": "tsc --noEmit"
5151
},
5252
"peerDependencies": {

packages/unlazy/src/blurhash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from '@unlazy/core/blurhash'
1+
export { createPngDataUri, createSvgDataUri } from '@unlazy/core/blurhash'

packages/unlazy/src/index.iife.ts renamed to packages/unlazy/src/bundle/index.iife.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { autoSizes, lazyLoad, loadImage } from '@unlazy/core'
1+
import { autoSizes, lazyLoad, loadImage } from '../../../core/src'
22

3-
export { autoSizes, lazyLoad, loadImage } from '@unlazy/core'
3+
export { autoSizes, lazyLoad, loadImage } from '../../../core/src'
44

55
// Default export for IIFE bundle
66
export default Object.freeze({

0 commit comments

Comments
 (0)