@son426/vite-image 0.1.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/LICENSE +22 -0
- package/README.md +128 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +77 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin/index.d.ts +33 -0
- package/dist/plugin/index.js +10 -0
- package/dist/plugin/index.js.map +1 -0
- package/dist/react/index.d.ts +22 -0
- package/dist/react/index.js +73 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# @son426/vite-image
|
|
2
|
+
|
|
3
|
+
A Vite plugin and React component for optimized images with LQIP (Low Quality Image Placeholder) support using `vite-imagetools`.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🖼️ **Optimized Images**: Automatic image optimization using vite-imagetools
|
|
8
|
+
- 🎨 **LQIP Support**: Low Quality Image Placeholder for smooth loading experience
|
|
9
|
+
- 📱 **Responsive Images**: Built-in support for srcSet and sizes attributes
|
|
10
|
+
- 🎯 **TypeScript**: Full TypeScript support with type definitions
|
|
11
|
+
- 🚀 **Zero Config**: Works out of the box with sensible defaults
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @son426/vite-image
|
|
17
|
+
# or
|
|
18
|
+
npm install @son426/vite-image
|
|
19
|
+
# or
|
|
20
|
+
yarn add @son426/vite-image
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Peer Dependencies
|
|
24
|
+
|
|
25
|
+
Make sure you have these installed:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add react vite vite-imagetools
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
### 1. Setup Vite Plugin
|
|
34
|
+
|
|
35
|
+
Add the plugin to your `vite.config.ts`:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { defineConfig } from "vite";
|
|
39
|
+
import { customImage } from "@son426/vite-image/plugin";
|
|
40
|
+
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
plugins: [
|
|
43
|
+
customImage({
|
|
44
|
+
// Optional: vite-imagetools options
|
|
45
|
+
defaultDirectives: (url) => {
|
|
46
|
+
if (url.searchParams.has("webp")) {
|
|
47
|
+
return new URLSearchParams("format=webp");
|
|
48
|
+
}
|
|
49
|
+
return new URLSearchParams();
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 2. Use the Component
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { CustomImage } from "@son426/vite-image/react";
|
|
60
|
+
|
|
61
|
+
// Import optimized images
|
|
62
|
+
import imageSrcSet from "@/assets/image.webp?w=640;1024;1920&format=webp&as=srcset";
|
|
63
|
+
import imageMeta from "@/assets/image.webp?w=1920&format=webp&as=meta";
|
|
64
|
+
import imageLqipSrc from "@/assets/image.webp?w=20&blur=2&quality=20&format=webp&inline";
|
|
65
|
+
|
|
66
|
+
function MyComponent() {
|
|
67
|
+
return (
|
|
68
|
+
<CustomImage
|
|
69
|
+
src={imageMeta.src}
|
|
70
|
+
srcSet={imageSrcSet}
|
|
71
|
+
lqipSrc={imageLqipSrc}
|
|
72
|
+
width={imageMeta.width}
|
|
73
|
+
height={imageMeta.height}
|
|
74
|
+
fill={false}
|
|
75
|
+
sizes="(max-width: 640px) 100vw, (max-width: 1024px) 100vw, 1920px"
|
|
76
|
+
alt="Description"
|
|
77
|
+
/>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Fill Mode
|
|
83
|
+
|
|
84
|
+
For images that fill their container (similar to Next.js Image):
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
<div style={{ position: "relative", width: "100%", height: "400px" }}>
|
|
88
|
+
<CustomImage
|
|
89
|
+
src={imageMeta.src}
|
|
90
|
+
srcSet={imageSrcSet}
|
|
91
|
+
lqipSrc={imageLqipSrc}
|
|
92
|
+
fill={true}
|
|
93
|
+
sizes="100vw"
|
|
94
|
+
alt="Description"
|
|
95
|
+
/>
|
|
96
|
+
</div>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## API
|
|
100
|
+
|
|
101
|
+
### CustomImage Props
|
|
102
|
+
|
|
103
|
+
| Prop | Type | Required | Description |
|
|
104
|
+
| --------- | ----------------------- | -------- | ---------------------------------------------- |
|
|
105
|
+
| `src` | `string` | Yes | Image source URL |
|
|
106
|
+
| `srcSet` | `string` | No | Responsive image srcSet |
|
|
107
|
+
| `lqipSrc` | `string` | No | Low Quality Image Placeholder source |
|
|
108
|
+
| `fill` | `boolean` | No | Fill container mode (default: `false`) |
|
|
109
|
+
| `width` | `number` | Yes* | Image width (required when `fill={false}`) |
|
|
110
|
+
| `height` | `number` | Yes* | Image height (required when `fill={false}`) |
|
|
111
|
+
| `sizes` | `string` | No | Sizes attribute (default: `"100vw"`) |
|
|
112
|
+
| `className` | `string` | No | Additional CSS classes |
|
|
113
|
+
| `...props`| `ImgHTMLAttributes` | No | All standard img element attributes |
|
|
114
|
+
|
|
115
|
+
\* Required when `fill={false}`
|
|
116
|
+
|
|
117
|
+
## TypeScript
|
|
118
|
+
|
|
119
|
+
Type definitions are included. The package also extends vite-imagetools types for better TypeScript support:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import type { CustomImageProps } from "@son426/vite-image/react";
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
MIT
|
|
128
|
+
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { imagetools } from 'vite-imagetools';
|
|
4
|
+
|
|
5
|
+
// src/react/CustomImage.tsx
|
|
6
|
+
function CustomImage({
|
|
7
|
+
src,
|
|
8
|
+
srcSet,
|
|
9
|
+
lqipSrc,
|
|
10
|
+
width,
|
|
11
|
+
height,
|
|
12
|
+
fill = false,
|
|
13
|
+
sizes = "100vw",
|
|
14
|
+
className = "",
|
|
15
|
+
...props
|
|
16
|
+
}) {
|
|
17
|
+
const [isImageLoaded, setIsImageLoaded] = useState(false);
|
|
18
|
+
const containerStyle = fill ? {
|
|
19
|
+
position: "absolute",
|
|
20
|
+
top: 0,
|
|
21
|
+
left: 0,
|
|
22
|
+
right: 0,
|
|
23
|
+
bottom: 0,
|
|
24
|
+
width: "100%",
|
|
25
|
+
height: "100%",
|
|
26
|
+
overflow: "hidden"
|
|
27
|
+
} : {
|
|
28
|
+
position: "relative",
|
|
29
|
+
width: "100%",
|
|
30
|
+
overflow: "hidden",
|
|
31
|
+
...!fill && width && height ? { aspectRatio: `${width} / ${height}` } : {}
|
|
32
|
+
};
|
|
33
|
+
const imageStyle = {
|
|
34
|
+
position: "absolute",
|
|
35
|
+
top: 0,
|
|
36
|
+
left: 0,
|
|
37
|
+
right: 0,
|
|
38
|
+
bottom: 0,
|
|
39
|
+
width: "100%",
|
|
40
|
+
height: "100%",
|
|
41
|
+
objectFit: "cover"
|
|
42
|
+
};
|
|
43
|
+
const lqipStyle = {
|
|
44
|
+
position: "absolute",
|
|
45
|
+
top: 0,
|
|
46
|
+
left: 0,
|
|
47
|
+
right: 0,
|
|
48
|
+
bottom: 0,
|
|
49
|
+
width: "100%",
|
|
50
|
+
height: "100%",
|
|
51
|
+
objectFit: "cover",
|
|
52
|
+
transition: "opacity 300ms ease-in-out",
|
|
53
|
+
transform: "scale(1.1)",
|
|
54
|
+
opacity: isImageLoaded ? 0 : 1
|
|
55
|
+
};
|
|
56
|
+
return /* @__PURE__ */ jsxs("div", { className: className || void 0, style: containerStyle, children: [
|
|
57
|
+
/* @__PURE__ */ jsx(
|
|
58
|
+
"img",
|
|
59
|
+
{
|
|
60
|
+
...props,
|
|
61
|
+
src,
|
|
62
|
+
srcSet,
|
|
63
|
+
sizes,
|
|
64
|
+
onLoad: () => setIsImageLoaded(true),
|
|
65
|
+
style: imageStyle
|
|
66
|
+
}
|
|
67
|
+
),
|
|
68
|
+
lqipSrc && /* @__PURE__ */ jsx("img", { src: lqipSrc, alt: "", "aria-hidden": "true", style: lqipStyle })
|
|
69
|
+
] });
|
|
70
|
+
}
|
|
71
|
+
function customImage(options) {
|
|
72
|
+
return imagetools(options);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { CustomImage, customImage };
|
|
76
|
+
//# sourceMappingURL=index.js.map
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/react/CustomImage.tsx","../src/plugin/index.ts"],"names":[],"mappings":";;;;;AAuBO,SAAS,WAAA,CAAY;AAAA,EAC1B,GAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA,GAAO,KAAA;AAAA,EACP,KAAA,GAAQ,OAAA;AAAA,EACR,SAAA,GAAY,EAAA;AAAA,EACZ,GAAG;AACL,CAAA,EAAqB;AACnB,EAAA,MAAM,CAAC,aAAA,EAAe,gBAAgB,CAAA,GAAI,SAAS,KAAK,CAAA;AAGxD,EAAA,MAAM,iBAAgC,IAAA,GAClC;AAAA,IACE,QAAA,EAAU,UAAA;AAAA,IACV,GAAA,EAAK,CAAA;AAAA,IACL,IAAA,EAAM,CAAA;AAAA,IACN,KAAA,EAAO,CAAA;AAAA,IACP,MAAA,EAAQ,CAAA;AAAA,IACR,KAAA,EAAO,MAAA;AAAA,IACP,MAAA,EAAQ,MAAA;AAAA,IACR,QAAA,EAAU;AAAA,GACZ,GACA;AAAA,IACE,QAAA,EAAU,UAAA;AAAA,IACV,KAAA,EAAO,MAAA;AAAA,IACP,QAAA,EAAU,QAAA;AAAA,IACV,GAAI,CAAC,IAAA,IAAQ,KAAA,IAAS,MAAA,GAClB,EAAE,WAAA,EAAa,CAAA,EAAG,KAAK,CAAA,GAAA,EAAM,MAAM,CAAA,CAAA,KACnC;AAAC,GACP;AAGJ,EAAA,MAAM,UAAA,GAA4B;AAAA,IAChC,QAAA,EAAU,UAAA;AAAA,IACV,GAAA,EAAK,CAAA;AAAA,IACL,IAAA,EAAM,CAAA;AAAA,IACN,KAAA,EAAO,CAAA;AAAA,IACP,MAAA,EAAQ,CAAA;AAAA,IACR,KAAA,EAAO,MAAA;AAAA,IACP,MAAA,EAAQ,MAAA;AAAA,IACR,SAAA,EAAW;AAAA,GACb;AAGA,EAAA,MAAM,SAAA,GAA2B;AAAA,IAC/B,QAAA,EAAU,UAAA;AAAA,IACV,GAAA,EAAK,CAAA;AAAA,IACL,IAAA,EAAM,CAAA;AAAA,IACN,KAAA,EAAO,CAAA;AAAA,IACP,MAAA,EAAQ,CAAA;AAAA,IACR,KAAA,EAAO,MAAA;AAAA,IACP,MAAA,EAAQ,MAAA;AAAA,IACR,SAAA,EAAW,OAAA;AAAA,IACX,UAAA,EAAY,2BAAA;AAAA,IACZ,SAAA,EAAW,YAAA;AAAA,IACX,OAAA,EAAS,gBAAgB,CAAA,GAAI;AAAA,GAC/B;AAEA,EAAA,4BACG,KAAA,EAAA,EAAI,SAAA,EAAW,SAAA,IAAa,MAAA,EAAW,OAAO,cAAA,EAC7C,QAAA,EAAA;AAAA,oBAAA,GAAA;AAAA,MAAC,KAAA;AAAA,MAAA;AAAA,QACE,GAAG,KAAA;AAAA,QACJ,GAAA;AAAA,QACA,MAAA;AAAA,QACA,KAAA;AAAA,QACA,MAAA,EAAQ,MAAM,gBAAA,CAAiB,IAAI,CAAA;AAAA,QACnC,KAAA,EAAO;AAAA;AAAA,KACT;AAAA,IACC,OAAA,oBACC,GAAA,CAAC,KAAA,EAAA,EAAI,GAAA,EAAK,OAAA,EAAS,KAAI,EAAA,EAAG,aAAA,EAAY,MAAA,EAAO,KAAA,EAAO,SAAA,EAAW;AAAA,GAAA,EAEnE,CAAA;AAEJ;ACpEO,SAAS,YACd,OAAA,EACQ;AACR,EAAA,OAAO,WAAW,OAAO,CAAA;AAC3B","file":"index.js","sourcesContent":["import { useState, type ImgHTMLAttributes, type CSSProperties } from \"react\";\n\ninterface BaseImageProps\n extends Omit<ImgHTMLAttributes<HTMLImageElement>, \"width\" | \"height\"> {\n src: string;\n srcSet?: string;\n lqipSrc?: string;\n}\n\ninterface FillImageProps extends BaseImageProps {\n fill: true;\n width?: never;\n height?: never;\n}\n\ninterface StandardImageProps extends BaseImageProps {\n fill: false | undefined;\n width: number;\n height: number;\n}\n\nexport type CustomImageProps = FillImageProps | StandardImageProps;\n\nexport function CustomImage({\n src,\n srcSet,\n lqipSrc,\n width,\n height,\n fill = false,\n sizes = \"100vw\",\n className = \"\",\n ...props\n}: CustomImageProps) {\n const [isImageLoaded, setIsImageLoaded] = useState(false);\n\n // 컨테이너 스타일\n const containerStyle: CSSProperties = fill\n ? {\n position: \"absolute\",\n top: 0,\n left: 0,\n right: 0,\n bottom: 0,\n width: \"100%\",\n height: \"100%\",\n overflow: \"hidden\",\n }\n : {\n position: \"relative\",\n width: \"100%\",\n overflow: \"hidden\",\n ...(!fill && width && height\n ? { aspectRatio: `${width} / ${height}` }\n : {}),\n };\n\n // 실제 이미지 스타일\n const imageStyle: CSSProperties = {\n position: \"absolute\",\n top: 0,\n left: 0,\n right: 0,\n bottom: 0,\n width: \"100%\",\n height: \"100%\",\n objectFit: \"cover\",\n };\n\n // LQIP 이미지 스타일\n const lqipStyle: CSSProperties = {\n position: \"absolute\",\n top: 0,\n left: 0,\n right: 0,\n bottom: 0,\n width: \"100%\",\n height: \"100%\",\n objectFit: \"cover\",\n transition: \"opacity 300ms ease-in-out\",\n transform: \"scale(1.1)\",\n opacity: isImageLoaded ? 0 : 1,\n };\n\n return (\n <div className={className || undefined} style={containerStyle}>\n <img\n {...props}\n src={src}\n srcSet={srcSet}\n sizes={sizes}\n onLoad={() => setIsImageLoaded(true)}\n style={imageStyle}\n />\n {lqipSrc && (\n <img src={lqipSrc} alt=\"\" aria-hidden=\"true\" style={lqipStyle} />\n )}\n </div>\n );\n}\n\n","import type { Plugin } from \"vite\";\nimport { imagetools } from \"vite-imagetools\";\n\nexport type CustomImagePluginOptions = Parameters<typeof imagetools>[0];\n\n/**\n * Vite plugin for image optimization using vite-imagetools\n * \n * @param options - Options to pass to vite-imagetools\n * @returns Vite plugin\n * \n * @example\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { customImage } from '@son426/vite-image/plugin';\n * \n * export default defineConfig({\n * plugins: [\n * customImage({\n * defaultDirectives: (url) => {\n * if (url.searchParams.has('webp')) {\n * return new URLSearchParams('format=webp');\n * }\n * return new URLSearchParams();\n * },\n * }),\n * ],\n * });\n * ```\n */\nexport function customImage(\n options?: CustomImagePluginOptions\n): Plugin {\n return imagetools(options);\n}\n\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { imagetools } from 'vite-imagetools';
|
|
3
|
+
|
|
4
|
+
type CustomImagePluginOptions = Parameters<typeof imagetools>[0];
|
|
5
|
+
/**
|
|
6
|
+
* Vite plugin for image optimization using vite-imagetools
|
|
7
|
+
*
|
|
8
|
+
* @param options - Options to pass to vite-imagetools
|
|
9
|
+
* @returns Vite plugin
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // vite.config.ts
|
|
14
|
+
* import { defineConfig } from 'vite';
|
|
15
|
+
* import { customImage } from '@son426/vite-image/plugin';
|
|
16
|
+
*
|
|
17
|
+
* export default defineConfig({
|
|
18
|
+
* plugins: [
|
|
19
|
+
* customImage({
|
|
20
|
+
* defaultDirectives: (url) => {
|
|
21
|
+
* if (url.searchParams.has('webp')) {
|
|
22
|
+
* return new URLSearchParams('format=webp');
|
|
23
|
+
* }
|
|
24
|
+
* return new URLSearchParams();
|
|
25
|
+
* },
|
|
26
|
+
* }),
|
|
27
|
+
* ],
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare function customImage(options?: CustomImagePluginOptions): Plugin;
|
|
32
|
+
|
|
33
|
+
export { type CustomImagePluginOptions, customImage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/plugin/index.ts"],"names":[],"mappings":";;;AA+BO,SAAS,YACd,OAAA,EACQ;AACR,EAAA,OAAO,WAAW,OAAO,CAAA;AAC3B","file":"index.js","sourcesContent":["import type { Plugin } from \"vite\";\nimport { imagetools } from \"vite-imagetools\";\n\nexport type CustomImagePluginOptions = Parameters<typeof imagetools>[0];\n\n/**\n * Vite plugin for image optimization using vite-imagetools\n * \n * @param options - Options to pass to vite-imagetools\n * @returns Vite plugin\n * \n * @example\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { customImage } from '@son426/vite-image/plugin';\n * \n * export default defineConfig({\n * plugins: [\n * customImage({\n * defaultDirectives: (url) => {\n * if (url.searchParams.has('webp')) {\n * return new URLSearchParams('format=webp');\n * }\n * return new URLSearchParams();\n * },\n * }),\n * ],\n * });\n * ```\n */\nexport function customImage(\n options?: CustomImagePluginOptions\n): Plugin {\n return imagetools(options);\n}\n\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ImgHTMLAttributes } from 'react';
|
|
3
|
+
|
|
4
|
+
interface BaseImageProps extends Omit<ImgHTMLAttributes<HTMLImageElement>, "width" | "height"> {
|
|
5
|
+
src: string;
|
|
6
|
+
srcSet?: string;
|
|
7
|
+
lqipSrc?: string;
|
|
8
|
+
}
|
|
9
|
+
interface FillImageProps extends BaseImageProps {
|
|
10
|
+
fill: true;
|
|
11
|
+
width?: never;
|
|
12
|
+
height?: never;
|
|
13
|
+
}
|
|
14
|
+
interface StandardImageProps extends BaseImageProps {
|
|
15
|
+
fill: false | undefined;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
}
|
|
19
|
+
type CustomImageProps = FillImageProps | StandardImageProps;
|
|
20
|
+
declare function CustomImage({ src, srcSet, lqipSrc, width, height, fill, sizes, className, ...props }: CustomImageProps): react_jsx_runtime.JSX.Element;
|
|
21
|
+
|
|
22
|
+
export { CustomImage, type CustomImageProps };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
+
|
|
4
|
+
// src/react/CustomImage.tsx
|
|
5
|
+
function CustomImage({
|
|
6
|
+
src,
|
|
7
|
+
srcSet,
|
|
8
|
+
lqipSrc,
|
|
9
|
+
width,
|
|
10
|
+
height,
|
|
11
|
+
fill = false,
|
|
12
|
+
sizes = "100vw",
|
|
13
|
+
className = "",
|
|
14
|
+
...props
|
|
15
|
+
}) {
|
|
16
|
+
const [isImageLoaded, setIsImageLoaded] = useState(false);
|
|
17
|
+
const containerStyle = fill ? {
|
|
18
|
+
position: "absolute",
|
|
19
|
+
top: 0,
|
|
20
|
+
left: 0,
|
|
21
|
+
right: 0,
|
|
22
|
+
bottom: 0,
|
|
23
|
+
width: "100%",
|
|
24
|
+
height: "100%",
|
|
25
|
+
overflow: "hidden"
|
|
26
|
+
} : {
|
|
27
|
+
position: "relative",
|
|
28
|
+
width: "100%",
|
|
29
|
+
overflow: "hidden",
|
|
30
|
+
...!fill && width && height ? { aspectRatio: `${width} / ${height}` } : {}
|
|
31
|
+
};
|
|
32
|
+
const imageStyle = {
|
|
33
|
+
position: "absolute",
|
|
34
|
+
top: 0,
|
|
35
|
+
left: 0,
|
|
36
|
+
right: 0,
|
|
37
|
+
bottom: 0,
|
|
38
|
+
width: "100%",
|
|
39
|
+
height: "100%",
|
|
40
|
+
objectFit: "cover"
|
|
41
|
+
};
|
|
42
|
+
const lqipStyle = {
|
|
43
|
+
position: "absolute",
|
|
44
|
+
top: 0,
|
|
45
|
+
left: 0,
|
|
46
|
+
right: 0,
|
|
47
|
+
bottom: 0,
|
|
48
|
+
width: "100%",
|
|
49
|
+
height: "100%",
|
|
50
|
+
objectFit: "cover",
|
|
51
|
+
transition: "opacity 300ms ease-in-out",
|
|
52
|
+
transform: "scale(1.1)",
|
|
53
|
+
opacity: isImageLoaded ? 0 : 1
|
|
54
|
+
};
|
|
55
|
+
return /* @__PURE__ */ jsxs("div", { className: className || void 0, style: containerStyle, children: [
|
|
56
|
+
/* @__PURE__ */ jsx(
|
|
57
|
+
"img",
|
|
58
|
+
{
|
|
59
|
+
...props,
|
|
60
|
+
src,
|
|
61
|
+
srcSet,
|
|
62
|
+
sizes,
|
|
63
|
+
onLoad: () => setIsImageLoaded(true),
|
|
64
|
+
style: imageStyle
|
|
65
|
+
}
|
|
66
|
+
),
|
|
67
|
+
lqipSrc && /* @__PURE__ */ jsx("img", { src: lqipSrc, alt: "", "aria-hidden": "true", style: lqipStyle })
|
|
68
|
+
] });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { CustomImage };
|
|
72
|
+
//# sourceMappingURL=index.js.map
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/react/CustomImage.tsx"],"names":[],"mappings":";;;;AAuBO,SAAS,WAAA,CAAY;AAAA,EAC1B,GAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,IAAA,GAAO,KAAA;AAAA,EACP,KAAA,GAAQ,OAAA;AAAA,EACR,SAAA,GAAY,EAAA;AAAA,EACZ,GAAG;AACL,CAAA,EAAqB;AACnB,EAAA,MAAM,CAAC,aAAA,EAAe,gBAAgB,CAAA,GAAI,SAAS,KAAK,CAAA;AAGxD,EAAA,MAAM,iBAAgC,IAAA,GAClC;AAAA,IACE,QAAA,EAAU,UAAA;AAAA,IACV,GAAA,EAAK,CAAA;AAAA,IACL,IAAA,EAAM,CAAA;AAAA,IACN,KAAA,EAAO,CAAA;AAAA,IACP,MAAA,EAAQ,CAAA;AAAA,IACR,KAAA,EAAO,MAAA;AAAA,IACP,MAAA,EAAQ,MAAA;AAAA,IACR,QAAA,EAAU;AAAA,GACZ,GACA;AAAA,IACE,QAAA,EAAU,UAAA;AAAA,IACV,KAAA,EAAO,MAAA;AAAA,IACP,QAAA,EAAU,QAAA;AAAA,IACV,GAAI,CAAC,IAAA,IAAQ,KAAA,IAAS,MAAA,GAClB,EAAE,WAAA,EAAa,CAAA,EAAG,KAAK,CAAA,GAAA,EAAM,MAAM,CAAA,CAAA,KACnC;AAAC,GACP;AAGJ,EAAA,MAAM,UAAA,GAA4B;AAAA,IAChC,QAAA,EAAU,UAAA;AAAA,IACV,GAAA,EAAK,CAAA;AAAA,IACL,IAAA,EAAM,CAAA;AAAA,IACN,KAAA,EAAO,CAAA;AAAA,IACP,MAAA,EAAQ,CAAA;AAAA,IACR,KAAA,EAAO,MAAA;AAAA,IACP,MAAA,EAAQ,MAAA;AAAA,IACR,SAAA,EAAW;AAAA,GACb;AAGA,EAAA,MAAM,SAAA,GAA2B;AAAA,IAC/B,QAAA,EAAU,UAAA;AAAA,IACV,GAAA,EAAK,CAAA;AAAA,IACL,IAAA,EAAM,CAAA;AAAA,IACN,KAAA,EAAO,CAAA;AAAA,IACP,MAAA,EAAQ,CAAA;AAAA,IACR,KAAA,EAAO,MAAA;AAAA,IACP,MAAA,EAAQ,MAAA;AAAA,IACR,SAAA,EAAW,OAAA;AAAA,IACX,UAAA,EAAY,2BAAA;AAAA,IACZ,SAAA,EAAW,YAAA;AAAA,IACX,OAAA,EAAS,gBAAgB,CAAA,GAAI;AAAA,GAC/B;AAEA,EAAA,4BACG,KAAA,EAAA,EAAI,SAAA,EAAW,SAAA,IAAa,MAAA,EAAW,OAAO,cAAA,EAC7C,QAAA,EAAA;AAAA,oBAAA,GAAA;AAAA,MAAC,KAAA;AAAA,MAAA;AAAA,QACE,GAAG,KAAA;AAAA,QACJ,GAAA;AAAA,QACA,MAAA;AAAA,QACA,KAAA;AAAA,QACA,MAAA,EAAQ,MAAM,gBAAA,CAAiB,IAAI,CAAA;AAAA,QACnC,KAAA,EAAO;AAAA;AAAA,KACT;AAAA,IACC,OAAA,oBACC,GAAA,CAAC,KAAA,EAAA,EAAI,GAAA,EAAK,OAAA,EAAS,KAAI,EAAA,EAAG,aAAA,EAAY,MAAA,EAAO,KAAA,EAAO,SAAA,EAAW;AAAA,GAAA,EAEnE,CAAA;AAEJ","file":"index.js","sourcesContent":["import { useState, type ImgHTMLAttributes, type CSSProperties } from \"react\";\n\ninterface BaseImageProps\n extends Omit<ImgHTMLAttributes<HTMLImageElement>, \"width\" | \"height\"> {\n src: string;\n srcSet?: string;\n lqipSrc?: string;\n}\n\ninterface FillImageProps extends BaseImageProps {\n fill: true;\n width?: never;\n height?: never;\n}\n\ninterface StandardImageProps extends BaseImageProps {\n fill: false | undefined;\n width: number;\n height: number;\n}\n\nexport type CustomImageProps = FillImageProps | StandardImageProps;\n\nexport function CustomImage({\n src,\n srcSet,\n lqipSrc,\n width,\n height,\n fill = false,\n sizes = \"100vw\",\n className = \"\",\n ...props\n}: CustomImageProps) {\n const [isImageLoaded, setIsImageLoaded] = useState(false);\n\n // 컨테이너 스타일\n const containerStyle: CSSProperties = fill\n ? {\n position: \"absolute\",\n top: 0,\n left: 0,\n right: 0,\n bottom: 0,\n width: \"100%\",\n height: \"100%\",\n overflow: \"hidden\",\n }\n : {\n position: \"relative\",\n width: \"100%\",\n overflow: \"hidden\",\n ...(!fill && width && height\n ? { aspectRatio: `${width} / ${height}` }\n : {}),\n };\n\n // 실제 이미지 스타일\n const imageStyle: CSSProperties = {\n position: \"absolute\",\n top: 0,\n left: 0,\n right: 0,\n bottom: 0,\n width: \"100%\",\n height: \"100%\",\n objectFit: \"cover\",\n };\n\n // LQIP 이미지 스타일\n const lqipStyle: CSSProperties = {\n position: \"absolute\",\n top: 0,\n left: 0,\n right: 0,\n bottom: 0,\n width: \"100%\",\n height: \"100%\",\n objectFit: \"cover\",\n transition: \"opacity 300ms ease-in-out\",\n transform: \"scale(1.1)\",\n opacity: isImageLoaded ? 0 : 1,\n };\n\n return (\n <div className={className || undefined} style={containerStyle}>\n <img\n {...props}\n src={src}\n srcSet={srcSet}\n sizes={sizes}\n onLoad={() => setIsImageLoaded(true)}\n style={imageStyle}\n />\n {lqipSrc && (\n <img src={lqipSrc} alt=\"\" aria-hidden=\"true\" style={lqipStyle} />\n )}\n </div>\n );\n}\n\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@son426/vite-image",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A Vite plugin and React component for optimized images with LQIP support using vite-imagetools",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./react": {
|
|
15
|
+
"types": "./dist/react/index.d.ts",
|
|
16
|
+
"import": "./dist/react/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./plugin": {
|
|
19
|
+
"types": "./dist/plugin/index.d.ts",
|
|
20
|
+
"import": "./dist/plugin/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"dev": "tsup --watch",
|
|
31
|
+
"prepublishOnly": "pnpm run build"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"vite",
|
|
35
|
+
"vite-plugin",
|
|
36
|
+
"image",
|
|
37
|
+
"optimization",
|
|
38
|
+
"lqip",
|
|
39
|
+
"react",
|
|
40
|
+
"imagetools",
|
|
41
|
+
"responsive-images"
|
|
42
|
+
],
|
|
43
|
+
"author": "",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": ""
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
51
|
+
"vite": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0",
|
|
52
|
+
"vite-imagetools": "^9.0.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "^20.0.0",
|
|
56
|
+
"@types/react": "^18.0.0 || ^19.0.0",
|
|
57
|
+
"react": "^19.1.0",
|
|
58
|
+
"tsup": "^8.0.0",
|
|
59
|
+
"typescript": "^5.0.0",
|
|
60
|
+
"vite": "^7.0.4",
|
|
61
|
+
"vite-imagetools": "^9.0.2"
|
|
62
|
+
},
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
}
|
|
66
|
+
}
|