astro-lqip 1.5.0 → 1.6.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/README.md +41 -2
- package/package.json +1 -1
- package/src/components/Image.astro +8 -3
- package/src/components/Picture.astro +8 -3
- package/src/types/components-options.type.ts +1 -1
- package/src/types/image-path.type.ts +3 -0
- package/src/utils/resolveImagePath.ts +124 -6
- package/src/utils/useLqipImage.ts +8 -13
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ Native extended Astro components for generating low quality image placeholders (
|
|
|
13
13
|
- 🖼️ Supports both `<Image>` and `<Picture>` components.
|
|
14
14
|
- 🎨 Multiple LQIP techniques: base64, solid color, CSS via gradients and SVG.
|
|
15
15
|
- 🚀 Easy to use, just replace the native Astro components with the ones from [astro-lqip](https://astro-lqip.web.app/).
|
|
16
|
+
- ⚡️ Support images as static imports or using string paths.
|
|
16
17
|
- 🔧 Fully compatible with [Astro's image optimization features](https://docs.astro.build/en/guides/images/).
|
|
17
18
|
- 🌍 Supports both local and remote images.
|
|
18
19
|
- ⚙️ Supports SSR mode with [Node Adapter](https://docs.astro.build/en/guides/integrations-guide/node/).
|
|
@@ -66,9 +67,47 @@ import otherImage from './path/to/other-image.png';
|
|
|
66
67
|
---
|
|
67
68
|
|
|
68
69
|
<Image src={image} alt="Cover Image" width={220} height={220} />
|
|
69
|
-
<Picture src={otherImage} alt="Other
|
|
70
|
+
<Picture src={otherImage} alt="Other Image" width={220} height={220} />
|
|
70
71
|
```
|
|
71
72
|
|
|
73
|
+
> [!TIP]
|
|
74
|
+
> Since version `1.6.0`, you can also put the image path as string directly in the `src` prop. Support absolute paths in `src`, relative paths and alias.
|
|
75
|
+
|
|
76
|
+
Example with absolute path:
|
|
77
|
+
|
|
78
|
+
```astro
|
|
79
|
+
---
|
|
80
|
+
import { Image, Picture } from 'astro-lqip/components';
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
<Image src="/src/path/to/image.png" alt="Cover Image" width={220} height={220} />
|
|
84
|
+
<Picture src="/src/path/to/other-image.png" alt="Other Image" width={220} height={220} />
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Example with relative path:
|
|
88
|
+
|
|
89
|
+
```astro
|
|
90
|
+
---
|
|
91
|
+
import { Image, Picture } from 'astro-lqip/components';
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
<Image src="../path/to/image.png" alt="Cover Image" width={220} height={220} />
|
|
95
|
+
<Picture src="../path/to/other-image.png" alt="Other Image" width={220} height={220} />
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Example with alias:
|
|
99
|
+
|
|
100
|
+
```astro
|
|
101
|
+
---
|
|
102
|
+
import { Image, Picture } from 'astro-lqip/components';
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
<Image src="@/assets/image.png" alt="Cover Image" width={220} height={220} />
|
|
106
|
+
<Picture src="@/assets/other-image.png" alt="Other Image" width={220} height={220} />
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Learn how to configure path aliasing in the [Astro documentation](https://docs.astro.build/en/guides/typescript/#import-aliases).
|
|
110
|
+
|
|
72
111
|
## ⚙️ Props
|
|
73
112
|
|
|
74
113
|
Both `<Image>` and `<Picture>` components support all the props of the [native Astro components](https://docs.astro.build/en/reference/modules/astro-assets/), but adds a couple of props for LQIP management:
|
|
@@ -94,7 +133,7 @@ import otherImage from './path/to/other-image.png';
|
|
|
94
133
|
---
|
|
95
134
|
|
|
96
135
|
<Image src={image} alt="Cover Image" width={220} height={220} lqip="svg" lqipSize={10} />
|
|
97
|
-
<Picture src={otherImage} alt="Other
|
|
136
|
+
<Picture src={otherImage} alt="Other Image" width={220} height={220} lqip="css" lqipSize={7} />
|
|
98
137
|
```
|
|
99
138
|
|
|
100
139
|
> [!TIP]
|
package/package.json
CHANGED
|
@@ -18,15 +18,20 @@ const { class: className, lqip = 'base64', lqipSize = 4, parentAttributes = {},
|
|
|
18
18
|
const isDevelopment = import.meta.env.MODE === 'development'
|
|
19
19
|
const isPrerendered = Astro.isPrerendered
|
|
20
20
|
|
|
21
|
-
const { combinedStyle } = await useLqipImage({
|
|
21
|
+
const { combinedStyle, resolvedSrc } = await useLqipImage({
|
|
22
22
|
src: props.src,
|
|
23
23
|
lqip,
|
|
24
24
|
lqipSize,
|
|
25
|
-
styleProps: parentAttributes.style ?? {},
|
|
25
|
+
styleProps: (parentAttributes.style ?? {}) as Record<string, string | number | undefined>,
|
|
26
|
+
forbiddenVars: [],
|
|
26
27
|
isDevelopment,
|
|
27
28
|
isPrerendered
|
|
28
29
|
})
|
|
29
30
|
|
|
31
|
+
const componentProps = {
|
|
32
|
+
...props,
|
|
33
|
+
src: resolvedSrc ?? props.src
|
|
34
|
+
}
|
|
30
35
|
const combinedParentAttributes = {
|
|
31
36
|
...parentAttributes,
|
|
32
37
|
style: combinedStyle
|
|
@@ -35,7 +40,7 @@ const combinedParentAttributes = {
|
|
|
35
40
|
|
|
36
41
|
<div class={className} data-astro-lqip {...combinedParentAttributes}>
|
|
37
42
|
<ImageComponent
|
|
38
|
-
{...
|
|
43
|
+
{...componentProps as LocalImageProps | RemoteImageProps}
|
|
39
44
|
class={className}
|
|
40
45
|
onload="parentElement.style.setProperty('--z-index', 1), parentElement.style.setProperty('--opacity', 0)"
|
|
41
46
|
/>
|
|
@@ -15,15 +15,20 @@ const { class: className, lqip = 'base64', lqipSize = 4, pictureAttributes = {},
|
|
|
15
15
|
const isDevelopment = import.meta.env.MODE === 'development'
|
|
16
16
|
const isPrerendered = Astro.isPrerendered
|
|
17
17
|
|
|
18
|
-
const { combinedStyle } = await useLqipImage({
|
|
18
|
+
const { combinedStyle, resolvedSrc } = await useLqipImage({
|
|
19
19
|
src: props.src,
|
|
20
20
|
lqip,
|
|
21
21
|
lqipSize,
|
|
22
|
-
styleProps: pictureAttributes.style ?? {},
|
|
22
|
+
styleProps: (pictureAttributes.style ?? {}) as Record<string, string | number | undefined>,
|
|
23
|
+
forbiddenVars: [],
|
|
23
24
|
isDevelopment,
|
|
24
25
|
isPrerendered
|
|
25
26
|
})
|
|
26
27
|
|
|
28
|
+
const componentProps = {
|
|
29
|
+
...props,
|
|
30
|
+
src: resolvedSrc ?? props.src
|
|
31
|
+
}
|
|
27
32
|
const combinedPictureAttributes = {
|
|
28
33
|
...pictureAttributes,
|
|
29
34
|
style: combinedStyle
|
|
@@ -31,7 +36,7 @@ const combinedPictureAttributes = {
|
|
|
31
36
|
---
|
|
32
37
|
|
|
33
38
|
<PictureComponent
|
|
34
|
-
{...
|
|
39
|
+
{...(componentProps as AstroPictureProps)}
|
|
35
40
|
class={className}
|
|
36
41
|
pictureAttributes={{ 'data-astro-lqip': '', ...combinedPictureAttributes }}
|
|
37
42
|
onload="parentElement.style.setProperty('--z-index', 1), parentElement.style.setProperty('--opacity', 0)"
|
|
@@ -4,7 +4,7 @@ export type ComponentsOptions = {
|
|
|
4
4
|
src: string | object
|
|
5
5
|
lqip: LqipType
|
|
6
6
|
lqipSize: number
|
|
7
|
-
styleProps: Record<string,
|
|
7
|
+
styleProps: Record<string, string | number | undefined>
|
|
8
8
|
forbiddenVars: string[]
|
|
9
9
|
isDevelopment: boolean | undefined
|
|
10
10
|
isPrerendered: boolean | undefined
|
|
@@ -1 +1,4 @@
|
|
|
1
1
|
export type ImagePath = string | { src: string } | Promise<{ default: { src: string } }>
|
|
2
|
+
export type ResolvedImage = { src: string, width?: number, height?: number, [k: string]: unknown }
|
|
3
|
+
export type ImportModule = Record<string, unknown> & { default?: unknown }
|
|
4
|
+
export type GlobMap = Record<string, () => Promise<ImportModule>>
|
|
@@ -1,10 +1,128 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import type { GlobMap, ImagePath, ImportModule, ResolvedImage } from '../types'
|
|
5
|
+
|
|
6
|
+
import { PREFIX } from '../constants'
|
|
7
|
+
|
|
8
|
+
const PUBLIC_DIR = join(process.cwd(), 'public')
|
|
9
|
+
|
|
10
|
+
const globFilesInSrc: GlobMap = ({ ...import.meta.glob('/src/**/*.{png,jpg,jpeg,svg}') } as unknown) as GlobMap
|
|
11
|
+
|
|
12
|
+
function warnFiles(filePath: string | undefined) {
|
|
13
|
+
if (!filePath) return
|
|
14
|
+
|
|
15
|
+
const lowerPath = filePath.toLowerCase()
|
|
16
|
+
|
|
17
|
+
if (lowerPath.includes(`${join('/', 'public')}`) || lowerPath.includes('/public/') || filePath.startsWith(PUBLIC_DIR)) {
|
|
18
|
+
console.warn(
|
|
19
|
+
`${PREFIX} Warning: image resolved from /public. Images should not be placed in /public — move them to /src so Astro can process them correctly.`
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (lowerPath.endsWith('.webp') || lowerPath.endsWith('.avif')) {
|
|
24
|
+
const extension = lowerPath.endsWith('.webp') ? 'webp' : 'avif'
|
|
25
|
+
console.warn(
|
|
26
|
+
`${PREFIX} Warning: image is in ${extension} format. These formats are usually already optimized; using this component to re-process them may degrade quality.`
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isObject(v: unknown): v is Record<string, unknown> {
|
|
32
|
+
return typeof v === 'object' && v !== null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isPromise(v: unknown): v is Promise<unknown> {
|
|
36
|
+
if (!isObject(v)) return false
|
|
37
|
+
const promise = v as { then?: unknown }
|
|
38
|
+
return typeof promise.then === 'function'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function hasSrc(v: unknown): v is ResolvedImage {
|
|
42
|
+
return isObject(v) && typeof (v as Record<string, unknown>)['src'] === 'string'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isRemoteUrl(v: string) {
|
|
46
|
+
return /^https?:\/\//.test(v)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function findGlobMatch(keys: string[], path: string) {
|
|
50
|
+
const candidates = [path.replace(/^\//, ''), `/${path.replace(/^\//, '')}`]
|
|
51
|
+
const match = keys.find((k) => candidates.includes(k) || k.endsWith(path) || k.endsWith(path.replace(/^\//, '')))
|
|
52
|
+
if (match) return match
|
|
53
|
+
|
|
54
|
+
const fileName = path.split('/').pop()
|
|
55
|
+
if (!fileName) return null
|
|
56
|
+
|
|
57
|
+
return keys.find((k) => k.endsWith(`/${fileName}`) || k.endsWith(fileName)) ?? null
|
|
58
|
+
}
|
|
2
59
|
|
|
3
60
|
export async function resolveImagePath(path: ImagePath) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
//
|
|
7
|
-
if (
|
|
8
|
-
|
|
61
|
+
if (path == null) return null
|
|
62
|
+
|
|
63
|
+
// validate dynamic import (Promise-like)
|
|
64
|
+
if (isPromise(path)) {
|
|
65
|
+
const mod = (await (path as Promise<ImportModule>)) as ImportModule
|
|
66
|
+
const resolved = (mod.default ?? mod) as unknown
|
|
67
|
+
if (hasSrc(resolved)) {
|
|
68
|
+
warnFiles(resolved.src)
|
|
69
|
+
return resolved
|
|
70
|
+
}
|
|
71
|
+
if (typeof resolved === 'string') {
|
|
72
|
+
warnFiles(resolved)
|
|
73
|
+
return resolved
|
|
74
|
+
}
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// validate already-resolved object (import result or { src: ... })
|
|
79
|
+
if (isObject(path)) {
|
|
80
|
+
const obj = path as Record<string, unknown>
|
|
81
|
+
const objSrc = typeof obj['src'] === 'string' ? (obj['src'] as string) : undefined
|
|
82
|
+
warnFiles(objSrc)
|
|
83
|
+
return hasSrc(obj) ? (obj as ResolvedImage) : null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// validate string path
|
|
87
|
+
if (typeof path === 'string') {
|
|
88
|
+
if (isRemoteUrl(path)) return path
|
|
89
|
+
|
|
90
|
+
const keys = Object.keys(globFilesInSrc)
|
|
91
|
+
const matchKey = findGlobMatch(keys, path)
|
|
92
|
+
|
|
93
|
+
if (matchKey) {
|
|
94
|
+
try {
|
|
95
|
+
const mod = await globFilesInSrc[matchKey]()
|
|
96
|
+
const resolved = (mod.default ?? mod) as unknown
|
|
97
|
+
|
|
98
|
+
if (hasSrc(resolved)) {
|
|
99
|
+
warnFiles((resolved as ResolvedImage).src)
|
|
100
|
+
return resolved as ResolvedImage
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (typeof resolved === 'string') {
|
|
104
|
+
warnFiles(resolved)
|
|
105
|
+
return resolved
|
|
106
|
+
}
|
|
107
|
+
} catch (err) {
|
|
108
|
+
console.log(`${PREFIX} resolveImagePath: failed to import glob match "${matchKey}" — falling back to filesystem.`, err)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// If module doesn't expose a usable value, fall through to filesystem check
|
|
113
|
+
try {
|
|
114
|
+
const absCandidate = path.startsWith('/') ? join(process.cwd(), path) : join(process.cwd(), path)
|
|
115
|
+
|
|
116
|
+
if (existsSync(absCandidate)) {
|
|
117
|
+
warnFiles(absCandidate)
|
|
118
|
+
return { src: `/@fs${absCandidate}` }
|
|
119
|
+
}
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.debug(`${PREFIX} resolveImagePath: filesystem check failed for "${path}".`, err)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
|
|
9
127
|
return null
|
|
10
128
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ComponentsOptions, SVGNode } from '../types'
|
|
1
|
+
import type { ComponentsOptions, ImagePath, SVGNode } from '../types'
|
|
2
2
|
|
|
3
3
|
import { PREFIX } from '../constants'
|
|
4
4
|
|
|
@@ -16,19 +16,14 @@ export async function useLqipImage({
|
|
|
16
16
|
isDevelopment,
|
|
17
17
|
isPrerendered
|
|
18
18
|
}: ComponentsOptions) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
} else if (typeof src === 'object' && src !== null) {
|
|
24
|
-
getImagePath = await resolveImagePath(src as unknown as string)
|
|
25
|
-
} else {
|
|
26
|
-
getImagePath = null
|
|
27
|
-
}
|
|
19
|
+
// resolve any kind of src (string, alias, import result, dynamic import)
|
|
20
|
+
const resolved = await resolveImagePath(src as unknown as ImagePath)
|
|
21
|
+
// resolved may be an object (module-like), { src: '...' } or null
|
|
22
|
+
const resolvedSrc = resolved ?? null
|
|
28
23
|
|
|
29
24
|
let lqipImage
|
|
30
|
-
if (
|
|
31
|
-
const lqipInput = typeof
|
|
25
|
+
if (resolvedSrc) {
|
|
26
|
+
const lqipInput = typeof resolvedSrc === 'string' ? { src: resolvedSrc } : resolvedSrc
|
|
32
27
|
lqipImage = await getLqip(lqipInput, lqip, lqipSize, isDevelopment, isPrerendered)
|
|
33
28
|
}
|
|
34
29
|
|
|
@@ -52,5 +47,5 @@ export async function useLqipImage({
|
|
|
52
47
|
...lqipStyle
|
|
53
48
|
}
|
|
54
49
|
|
|
55
|
-
return { lqipImage, svgHTML, lqipStyle, combinedStyle }
|
|
50
|
+
return { lqipImage, svgHTML, lqipStyle, combinedStyle, resolvedSrc }
|
|
56
51
|
}
|