@son426/vite-image 0.1.4 → 0.1.6

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 CHANGED
@@ -20,3 +20,4 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
22
 
23
+
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  - **Bring the power of Next.js's automatic image optimization to your Vite projects.**
6
6
  - **Dedicated to the Vite + React ecosystem.**
7
7
 
8
- BSimply add the plugin to your config, and start using the `<Image />` component immediately. No complex setups, just performant images.
8
+ Simply add the plugin to your config, and start using the `<Image />` component immediately. No complex setups, just performant images.
9
9
 
10
10
  ## ✨ Why use this?
11
11
 
@@ -54,8 +54,9 @@ yarn add @son426/vite-image
54
54
 
55
55
  Just a standard Vite + React project.
56
56
 
57
- vite (>= 4.0.0)
58
- react (>= 18.0.0)
57
+ - vite (>= 4.0.0)
58
+ - react (>= 18.0.0)
59
+ - react-dom (>= 18.0.0)
59
60
 
60
61
  ## Usage
61
62
 
@@ -86,14 +87,7 @@ import Image from "@son426/vite-image/react";
86
87
  import bgImage from "@/assets/image.webp?vite-image";
87
88
 
88
89
  function MyComponent() {
89
- return (
90
- <Image
91
- src={bgImage}
92
- fill={false}
93
- sizes="(max-width: 640px) 100vw, (max-width: 1024px) 100vw, 1920px"
94
- alt="Description"
95
- />
96
- );
90
+ return <Image src={bgImage} fill={false} alt="Description" />;
97
91
  }
98
92
  ```
99
93
 
@@ -103,16 +97,82 @@ The `?vite-image` query automatically generates:
103
97
 
104
98
  - `src`: Optimized image URL
105
99
  - `srcSet`: Responsive srcSet string
106
- - `lqipSrc`: Low Quality Image Placeholder (base64 inline)
100
+ - `blurDataURL`: Low Quality Image Placeholder (base64 inline)
107
101
  - `width` and `height`: Image dimensions
108
102
 
109
- ### Fill Mode
103
+ #### Usage Examples
110
104
 
111
- For images that fill their container (similar to Next.js Image):
105
+ **Basic usage:**
112
106
 
113
- ```typescript
107
+ ```tsx
108
+ import Image from "@son426/vite-image/react";
109
+ import heroImage from "@/assets/hero.jpg?vite-image";
110
+
111
+ <Image src={heroImage} alt="Hero" />;
112
+ ```
113
+
114
+ **Fill mode (container-filling images):**
115
+
116
+ ```tsx
114
117
  <div style={{ position: "relative", width: "100%", height: "400px" }}>
115
- <Image src={bgImage} fill={true} sizes="100vw" alt="Description" />
118
+ <Image src={bgImage} fill alt="Background" />
119
+ </div>
120
+ ```
121
+
122
+ **With priority (LCP images):**
123
+
124
+ ```tsx
125
+ <Image src={heroImage} alt="Hero" priority />
126
+ ```
127
+
128
+ **With blur placeholder:**
129
+
130
+ ```tsx
131
+ <Image src={heroImage} alt="Hero" placeholder="blur" />
132
+ ```
133
+
134
+ **Without placeholder:**
135
+
136
+ ```tsx
137
+ <Image src={heroImage} alt="Hero" placeholder="empty" />
138
+ ```
139
+
140
+ **Custom data URL placeholder:**
141
+
142
+ ```tsx
143
+ <Image src={heroImage} alt="Hero" placeholder="data:image/jpeg;base64,..." />
144
+ ```
145
+
146
+ **Custom sizes:**
147
+
148
+ ```tsx
149
+ <Image src={heroImage} alt="Hero" sizes="(max-width: 768px) 100vw, 50vw" />
150
+ ```
151
+
152
+ **With onLoad and onError callbacks:**
153
+
154
+ ```tsx
155
+ <Image
156
+ src={heroImage}
157
+ alt="Hero"
158
+ onLoad={(e) => console.log("Image loaded", e)}
159
+ onError={(e) => console.error("Image failed to load", e)}
160
+ />
161
+ ```
162
+
163
+ **Combined usage:**
164
+
165
+ ```tsx
166
+ <div style={{ position: "relative", width: "100%", height: "600px" }}>
167
+ <Image
168
+ src={heroImage}
169
+ alt="Hero"
170
+ fill
171
+ priority
172
+ placeholder="blur"
173
+ className="rounded-lg"
174
+ onLoad={(e) => console.log("Loaded")}
175
+ />
116
176
  </div>
117
177
  ```
118
178
 
@@ -120,16 +180,25 @@ For images that fill their container (similar to Next.js Image):
120
180
 
121
181
  ### Image Props
122
182
 
123
- | Prop | Type | Required | Description |
124
- | ----------- | --------------------- | -------- | ------------------------------------------ |
125
- | `src` | `ResponsiveImageData` | Yes | Image data object from `?vite-image` query |
126
- | `fill` | `boolean` | No | Fill container mode (default: `false`) |
127
- | `sizes` | `string` | No | Sizes attribute (default: `"100vw"`) |
128
- | `className` | `string` | No | Additional CSS classes |
129
- | `style` | `CSSProperties` | No | Additional inline styles |
130
- | `...props` | `ImgHTMLAttributes` | No | All standard img element attributes |
131
-
132
- **Note**: The `src` prop must be an object imported from `?vite-image` query. String URLs are not supported. The `width` and `height` are automatically extracted from the `src` object.
183
+ | Prop | Type | Required | Default | Description |
184
+ | ------------- | --------------------------------------------------------- | -------- | --------- | ----------------------------------------------------------------------- |
185
+ | `src` | `ResponsiveImageData` | Yes | - | Image data object from `?vite-image` query |
186
+ | `fill` | `boolean` | No | `false` | Fill container mode (requires parent with `position: relative`) |
187
+ | `sizes` | `string` | No | auto | Sizes attribute (auto-calculated from srcSet if not provided) |
188
+ | `priority` | `boolean` | No | `false` | High priority loading (preload + eager + fetchPriority high) |
189
+ | `placeholder` | `'empty' \| 'blur' \| string` | No | `'empty'` | Placeholder type: `'empty'` (none), `'blur'` (blurDataURL), or data URL |
190
+ | `onLoad` | `(event: React.SyntheticEvent<HTMLImageElement>) => void` | No | - | Callback fired when image loads successfully |
191
+ | `onError` | `(event: React.SyntheticEvent<HTMLImageElement>) => void` | No | - | Callback fired when image fails to load |
192
+ | `className` | `string` | No | - | Additional CSS classes |
193
+ | `style` | `CSSProperties` | No | - | Additional inline styles |
194
+ | `...props` | `ImgHTMLAttributes` | No | - | All standard img element attributes |
195
+
196
+ **Notes**:
197
+
198
+ - The `src` prop must be an object imported from `?vite-image` query. String URLs are not supported.
199
+ - The `width` and `height` are automatically extracted from the `src` object.
200
+ - When `priority={true}`, the image is preloaded using `react-dom`'s `preload` API and loaded with `loading="eager"` and `fetchPriority="high"`.
201
+ - When `sizes` is not provided, it's automatically calculated from `srcSet` breakpoints.
133
202
 
134
203
  ### ResponsiveImageData
135
204
 
@@ -141,7 +210,7 @@ interface ResponsiveImageData {
141
210
  width: number;
142
211
  height: number;
143
212
  srcSet?: string;
144
- lqipSrc?: string;
213
+ blurDataURL?: string; // Base64 encoded blur placeholder (Next.js Image compatible)
145
214
  }
146
215
  ```
147
216
 
@@ -160,10 +229,12 @@ import type { ImageProps, ResponsiveImageData } from "@son426/vite-image/react";
160
229
 
161
230
  - Responsive srcSet (640px, 1024px, 1920px widths)
162
231
  - Image metadata (1920px width)
163
- - LQIP (20px width, blurred, low quality, inline base64)
232
+ - Blur placeholder (20px width, blurred, low quality, inline base64 as `blurDataURL`)
164
233
 
165
234
  2. **Image Component**: The `<Image />` component handles:
166
- - LQIP display while the main image loads
235
+ - Automatic `sizes` calculation from `srcSet` breakpoints
236
+ - Placeholder display (`blur`, `empty`, or custom data URL)
237
+ - Priority loading with `react-dom`'s `preload` API when `priority={true}`
167
238
  - Responsive image loading with srcSet
168
239
  - Proper aspect ratio maintenance
169
240
  - Fill mode for container-filling images
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import 'react';
2
+ import 'react-dom';
2
3
  import 'react/jsx-runtime';
3
4
  import { imagetools } from 'vite-imagetools';
4
5
 
@@ -17,14 +18,14 @@ function viteImage(options) {
17
18
  return `
18
19
  import srcSet from "${basePath}?${srcSetParams}";
19
20
  import meta from "${basePath}?${metaParams}";
20
- import lqipSrc from "${basePath}?${lqipParams}";
21
+ import blurDataURL from "${basePath}?${lqipParams}";
21
22
 
22
23
  export default {
23
24
  src: meta.src,
24
25
  width: meta.width,
25
26
  height: meta.height,
26
27
  srcSet: srcSet,
27
- lqipSrc: lqipSrc
28
+ blurDataURL: blurDataURL
28
29
  };
29
30
  `;
30
31
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/plugin/index.ts"],"names":[],"mappings":";;;;;AAyBO,SAAS,UAAU,OAAA,EAAkD;AAE1E,EAAA,MAAM,cAAA,GAA+B;AAAA,IACnC,IAAA,EAAM,8BAAA;AAAA,IACN,OAAA,EAAS,KAAA;AAAA,IACT,MAAM,KAAK,EAAA,EAAY;AACrB,MAAA,MAAM,CAAC,QAAA,EAAU,MAAM,CAAA,GAAI,EAAA,CAAG,MAAM,GAAG,CAAA;AACvC,MAAA,MAAM,MAAA,GAAS,IAAI,eAAA,CAAgB,MAAM,CAAA;AAEzC,MAAA,IAAI,MAAA,CAAO,GAAA,CAAI,YAAY,CAAA,EAAG;AAC5B,QAAA,MAAM,YAAA,GAAe,uCAAA;AACrB,QAAA,MAAM,UAAA,GAAa,4BAAA;AACnB,QAAA,MAAM,UAAA,GAAa,2CAAA;AAEnB,QAAA,OAAO;AAAA,8BAAA,EACiB,QAAQ,IAAI,YAAY,CAAA;AAAA,4BAAA,EAC1B,QAAQ,IAAI,UAAU,CAAA;AAAA,+BAAA,EACnB,QAAQ,IAAI,UAAU,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAAA,CAAA;AAAA,MAUjD;AAEA,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,GACF;AAEA,EAAA,OAAO,CAAC,cAAA,EAAgB,UAAA,CAAW,OAAO,CAAC,CAAA;AAC7C","file":"index.js","sourcesContent":["import type { PluginOption } from \"vite\";\nimport { imagetools } from \"vite-imagetools\";\n\nexport type ViteImagePluginOptions = Parameters<typeof imagetools>[0];\n\n/**\n * Vite plugin for image optimization using vite-imagetools\n * This plugin handles ?vite-image queries and uses imagetools for image processing\n *\n * @param options - Options to pass to vite-imagetools\n * @returns Array of Vite plugins (vite-image macro and imagetools)\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { viteImage } from '@son426/vite-image/plugin';\n *\n * export default defineConfig({\n * plugins: [\n * ...viteImage(),\n * ],\n * });\n * ```\n */\nexport function viteImage(options?: ViteImagePluginOptions): PluginOption[] {\n // 커스텀 플러그인: ?vite-image 쿼리를 처리\n const viteImageMacro: PluginOption = {\n name: \"vite-plugin-vite-image-macro\",\n enforce: \"pre\" as const,\n async load(id: string) {\n const [basePath, search] = id.split(\"?\");\n const params = new URLSearchParams(search);\n\n if (params.has(\"vite-image\")) {\n const srcSetParams = \"w=640;1024;1920&format=webp&as=srcset\";\n const metaParams = \"w=1920&format=webp&as=meta\";\n const lqipParams = \"w=20&blur=2&quality=20&format=webp&inline\";\n\n return `\n import srcSet from \"${basePath}?${srcSetParams}\";\n import meta from \"${basePath}?${metaParams}\";\n import lqipSrc from \"${basePath}?${lqipParams}\";\n \n export default {\n src: meta.src,\n width: meta.width,\n height: meta.height,\n srcSet: srcSet,\n lqipSrc: lqipSrc\n };\n `;\n }\n\n return null;\n },\n };\n\n return [viteImageMacro, imagetools(options)];\n}\n"]}
1
+ {"version":3,"sources":["../src/plugin/index.ts"],"names":[],"mappings":";;;;;;AAyBO,SAAS,UAAU,OAAA,EAAkD;AAE1E,EAAA,MAAM,cAAA,GAA+B;AAAA,IACnC,IAAA,EAAM,8BAAA;AAAA,IACN,OAAA,EAAS,KAAA;AAAA,IACT,MAAM,KAAK,EAAA,EAAY;AACrB,MAAA,MAAM,CAAC,QAAA,EAAU,MAAM,CAAA,GAAI,EAAA,CAAG,MAAM,GAAG,CAAA;AACvC,MAAA,MAAM,MAAA,GAAS,IAAI,eAAA,CAAgB,MAAM,CAAA;AAEzC,MAAA,IAAI,MAAA,CAAO,GAAA,CAAI,YAAY,CAAA,EAAG;AAC5B,QAAA,MAAM,YAAA,GAAe,uCAAA;AACrB,QAAA,MAAM,UAAA,GAAa,4BAAA;AACnB,QAAA,MAAM,UAAA,GAAa,2CAAA;AAEnB,QAAA,OAAO;AAAA,8BAAA,EACiB,QAAQ,IAAI,YAAY,CAAA;AAAA,4BAAA,EAC1B,QAAQ,IAAI,UAAU,CAAA;AAAA,mCAAA,EACf,QAAQ,IAAI,UAAU,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAAA,CAAA;AAAA,MAUrD;AAEA,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,GACF;AAEA,EAAA,OAAO,CAAC,cAAA,EAAgB,UAAA,CAAW,OAAO,CAAC,CAAA;AAC7C","file":"index.js","sourcesContent":["import type { PluginOption } from \"vite\";\nimport { imagetools } from \"vite-imagetools\";\n\nexport type ViteImagePluginOptions = Parameters<typeof imagetools>[0];\n\n/**\n * Vite plugin for image optimization using vite-imagetools\n * This plugin handles ?vite-image queries and uses imagetools for image processing\n *\n * @param options - Options to pass to vite-imagetools\n * @returns Array of Vite plugins (vite-image macro and imagetools)\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { viteImage } from '@son426/vite-image/plugin';\n *\n * export default defineConfig({\n * plugins: [\n * ...viteImage(),\n * ],\n * });\n * ```\n */\nexport function viteImage(options?: ViteImagePluginOptions): PluginOption[] {\n // 커스텀 플러그인: ?vite-image 쿼리를 처리\n const viteImageMacro: PluginOption = {\n name: \"vite-plugin-vite-image-macro\",\n enforce: \"pre\" as const,\n async load(id: string) {\n const [basePath, search] = id.split(\"?\");\n const params = new URLSearchParams(search);\n\n if (params.has(\"vite-image\")) {\n const srcSetParams = \"w=640;1024;1920&format=webp&as=srcset\";\n const metaParams = \"w=1920&format=webp&as=meta\";\n const lqipParams = \"w=20&blur=2&quality=20&format=webp&inline\";\n\n return `\n import srcSet from \"${basePath}?${srcSetParams}\";\n import meta from \"${basePath}?${metaParams}\";\n import blurDataURL from \"${basePath}?${lqipParams}\";\n \n export default {\n src: meta.src,\n width: meta.width,\n height: meta.height,\n srcSet: srcSet,\n blurDataURL: blurDataURL\n };\n `;\n }\n\n return null;\n },\n };\n\n return [viteImageMacro, imagetools(options)];\n}\n"]}
@@ -15,14 +15,14 @@ function viteImage(options) {
15
15
  return `
16
16
  import srcSet from "${basePath}?${srcSetParams}";
17
17
  import meta from "${basePath}?${metaParams}";
18
- import lqipSrc from "${basePath}?${lqipParams}";
18
+ import blurDataURL from "${basePath}?${lqipParams}";
19
19
 
20
20
  export default {
21
21
  src: meta.src,
22
22
  width: meta.width,
23
23
  height: meta.height,
24
24
  srcSet: srcSet,
25
- lqipSrc: lqipSrc
25
+ blurDataURL: blurDataURL
26
26
  };
27
27
  `;
28
28
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/plugin/index.ts"],"names":[],"mappings":";;;AAyBO,SAAS,UAAU,OAAA,EAAkD;AAE1E,EAAA,MAAM,cAAA,GAA+B;AAAA,IACnC,IAAA,EAAM,8BAAA;AAAA,IACN,OAAA,EAAS,KAAA;AAAA,IACT,MAAM,KAAK,EAAA,EAAY;AACrB,MAAA,MAAM,CAAC,QAAA,EAAU,MAAM,CAAA,GAAI,EAAA,CAAG,MAAM,GAAG,CAAA;AACvC,MAAA,MAAM,MAAA,GAAS,IAAI,eAAA,CAAgB,MAAM,CAAA;AAEzC,MAAA,IAAI,MAAA,CAAO,GAAA,CAAI,YAAY,CAAA,EAAG;AAC5B,QAAA,MAAM,YAAA,GAAe,uCAAA;AACrB,QAAA,MAAM,UAAA,GAAa,4BAAA;AACnB,QAAA,MAAM,UAAA,GAAa,2CAAA;AAEnB,QAAA,OAAO;AAAA,8BAAA,EACiB,QAAQ,IAAI,YAAY,CAAA;AAAA,4BAAA,EAC1B,QAAQ,IAAI,UAAU,CAAA;AAAA,+BAAA,EACnB,QAAQ,IAAI,UAAU,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAAA,CAAA;AAAA,MAUjD;AAEA,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,GACF;AAEA,EAAA,OAAO,CAAC,cAAA,EAAgB,UAAA,CAAW,OAAO,CAAC,CAAA;AAC7C","file":"index.js","sourcesContent":["import type { PluginOption } from \"vite\";\nimport { imagetools } from \"vite-imagetools\";\n\nexport type ViteImagePluginOptions = Parameters<typeof imagetools>[0];\n\n/**\n * Vite plugin for image optimization using vite-imagetools\n * This plugin handles ?vite-image queries and uses imagetools for image processing\n *\n * @param options - Options to pass to vite-imagetools\n * @returns Array of Vite plugins (vite-image macro and imagetools)\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { viteImage } from '@son426/vite-image/plugin';\n *\n * export default defineConfig({\n * plugins: [\n * ...viteImage(),\n * ],\n * });\n * ```\n */\nexport function viteImage(options?: ViteImagePluginOptions): PluginOption[] {\n // 커스텀 플러그인: ?vite-image 쿼리를 처리\n const viteImageMacro: PluginOption = {\n name: \"vite-plugin-vite-image-macro\",\n enforce: \"pre\" as const,\n async load(id: string) {\n const [basePath, search] = id.split(\"?\");\n const params = new URLSearchParams(search);\n\n if (params.has(\"vite-image\")) {\n const srcSetParams = \"w=640;1024;1920&format=webp&as=srcset\";\n const metaParams = \"w=1920&format=webp&as=meta\";\n const lqipParams = \"w=20&blur=2&quality=20&format=webp&inline\";\n\n return `\n import srcSet from \"${basePath}?${srcSetParams}\";\n import meta from \"${basePath}?${metaParams}\";\n import lqipSrc from \"${basePath}?${lqipParams}\";\n \n export default {\n src: meta.src,\n width: meta.width,\n height: meta.height,\n srcSet: srcSet,\n lqipSrc: lqipSrc\n };\n `;\n }\n\n return null;\n },\n };\n\n return [viteImageMacro, imagetools(options)];\n}\n"]}
1
+ {"version":3,"sources":["../../src/plugin/index.ts"],"names":[],"mappings":";;;AAyBO,SAAS,UAAU,OAAA,EAAkD;AAE1E,EAAA,MAAM,cAAA,GAA+B;AAAA,IACnC,IAAA,EAAM,8BAAA;AAAA,IACN,OAAA,EAAS,KAAA;AAAA,IACT,MAAM,KAAK,EAAA,EAAY;AACrB,MAAA,MAAM,CAAC,QAAA,EAAU,MAAM,CAAA,GAAI,EAAA,CAAG,MAAM,GAAG,CAAA;AACvC,MAAA,MAAM,MAAA,GAAS,IAAI,eAAA,CAAgB,MAAM,CAAA;AAEzC,MAAA,IAAI,MAAA,CAAO,GAAA,CAAI,YAAY,CAAA,EAAG;AAC5B,QAAA,MAAM,YAAA,GAAe,uCAAA;AACrB,QAAA,MAAM,UAAA,GAAa,4BAAA;AACnB,QAAA,MAAM,UAAA,GAAa,2CAAA;AAEnB,QAAA,OAAO;AAAA,8BAAA,EACiB,QAAQ,IAAI,YAAY,CAAA;AAAA,4BAAA,EAC1B,QAAQ,IAAI,UAAU,CAAA;AAAA,mCAAA,EACf,QAAQ,IAAI,UAAU,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAAA,CAAA;AAAA,MAUrD;AAEA,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,GACF;AAEA,EAAA,OAAO,CAAC,cAAA,EAAgB,UAAA,CAAW,OAAO,CAAC,CAAA;AAC7C","file":"index.js","sourcesContent":["import type { PluginOption } from \"vite\";\nimport { imagetools } from \"vite-imagetools\";\n\nexport type ViteImagePluginOptions = Parameters<typeof imagetools>[0];\n\n/**\n * Vite plugin for image optimization using vite-imagetools\n * This plugin handles ?vite-image queries and uses imagetools for image processing\n *\n * @param options - Options to pass to vite-imagetools\n * @returns Array of Vite plugins (vite-image macro and imagetools)\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { viteImage } from '@son426/vite-image/plugin';\n *\n * export default defineConfig({\n * plugins: [\n * ...viteImage(),\n * ],\n * });\n * ```\n */\nexport function viteImage(options?: ViteImagePluginOptions): PluginOption[] {\n // 커스텀 플러그인: ?vite-image 쿼리를 처리\n const viteImageMacro: PluginOption = {\n name: \"vite-plugin-vite-image-macro\",\n enforce: \"pre\" as const,\n async load(id: string) {\n const [basePath, search] = id.split(\"?\");\n const params = new URLSearchParams(search);\n\n if (params.has(\"vite-image\")) {\n const srcSetParams = \"w=640;1024;1920&format=webp&as=srcset\";\n const metaParams = \"w=1920&format=webp&as=meta\";\n const lqipParams = \"w=20&blur=2&quality=20&format=webp&inline\";\n\n return `\n import srcSet from \"${basePath}?${srcSetParams}\";\n import meta from \"${basePath}?${metaParams}\";\n import blurDataURL from \"${basePath}?${lqipParams}\";\n \n export default {\n src: meta.src,\n width: meta.width,\n height: meta.height,\n srcSet: srcSet,\n blurDataURL: blurDataURL\n };\n `;\n }\n\n return null;\n },\n };\n\n return [viteImageMacro, imagetools(options)];\n}\n"]}
@@ -9,12 +9,18 @@ interface ResponsiveImageData {
9
9
  width: number;
10
10
  height: number;
11
11
  srcSet?: string;
12
- lqipSrc?: string;
12
+ blurDataURL?: string;
13
13
  }
14
14
 
15
+ type PlaceholderValue = "empty" | "blur" | `data:image/${string}`;
15
16
  interface BaseImageProps extends Omit<ImgHTMLAttributes<HTMLImageElement>, "src" | "srcSet" | "width" | "height"> {
16
17
  src: ResponsiveImageData;
17
18
  sizes?: string;
19
+ placeholder?: PlaceholderValue;
20
+ blurDataURL?: string;
21
+ priority?: boolean;
22
+ onLoad?: (event: React.SyntheticEvent<HTMLImageElement, Event>) => void;
23
+ onError?: (event: React.SyntheticEvent<HTMLImageElement, Event>) => void;
18
24
  }
19
25
  interface FillImageProps extends BaseImageProps {
20
26
  fill: true;
@@ -24,6 +30,9 @@ interface StandardImageProps extends BaseImageProps {
24
30
  }
25
31
  type ImageProps = FillImageProps | StandardImageProps;
26
32
  declare function Image({ src, // 이제 이 src는 객체입니다.
27
- fill, sizes, className, style, ...props }: ImageProps): react_jsx_runtime.JSX.Element;
33
+ fill, sizes, placeholder, // 기본값: empty (Next.js Image 호환)
34
+ blurDataURL: customBlurDataURL, // 사용자가 직접 제공한 blurDataURL (우선순위 높음)
35
+ priority, // 기본값: false (Next.js Image 호환)
36
+ className, style, onLoad, onError, ...props }: ImageProps): react_jsx_runtime.JSX.Element;
28
37
 
29
38
  export { type ImageProps, type ResponsiveImageData, Image as default };
@@ -1,24 +1,81 @@
1
1
  import { useState } from 'react';
2
+ import { preload } from 'react-dom';
2
3
  import { jsxs, jsx } from 'react/jsx-runtime';
3
4
 
4
5
  // src/react/Image.tsx
6
+ function generateSizesFromSrcSet(srcSet) {
7
+ if (!srcSet) {
8
+ return "100vw";
9
+ }
10
+ const widthMatches = srcSet.match(/(\d+)w/g);
11
+ if (!widthMatches || widthMatches.length === 0) {
12
+ return "100vw";
13
+ }
14
+ const breakpoints = widthMatches.map((match) => parseInt(match.replace("w", ""), 10)).sort((a, b) => a - b);
15
+ if (breakpoints.length === 0) {
16
+ return "100vw";
17
+ }
18
+ const sizeParts = [];
19
+ for (let i = 0; i < breakpoints.length; i++) {
20
+ const breakpoint = breakpoints[i];
21
+ if (i === breakpoints.length - 1) {
22
+ sizeParts.push(`${breakpoint}px`);
23
+ } else {
24
+ sizeParts.push(`(max-width: ${breakpoint}px) 100vw`);
25
+ }
26
+ }
27
+ return sizeParts.join(", ");
28
+ }
5
29
  function Image({
6
30
  src,
7
31
  // 이제 이 src는 객체입니다.
8
32
  fill = false,
9
- sizes = "100vw",
33
+ sizes,
34
+ placeholder = "empty",
35
+ // 기본값: empty (Next.js Image 호환)
36
+ blurDataURL: customBlurDataURL,
37
+ // 사용자가 직접 제공한 blurDataURL (우선순위 높음)
38
+ priority = false,
39
+ // 기본값: false (Next.js Image 호환)
10
40
  className = "",
11
41
  style,
42
+ onLoad,
43
+ onError,
12
44
  ...props
13
45
  }) {
14
46
  const [isImageLoaded, setIsImageLoaded] = useState(false);
15
47
  const {
16
48
  src: currentSrc,
17
49
  srcSet: currentSrcSet,
18
- lqipSrc: currentLqip,
50
+ blurDataURL: srcBlurDataURL,
51
+ // 번들러가 생성한 blurDataURL
19
52
  width: currentWidth,
20
53
  height: currentHeight
21
54
  } = src;
55
+ const blurDataURL = customBlurDataURL ?? srcBlurDataURL;
56
+ const computedSizes = sizes ?? (fill ? "100vw" : generateSizesFromSrcSet(currentSrcSet));
57
+ if (priority && currentSrc) {
58
+ preload(currentSrc, {
59
+ as: "image",
60
+ fetchPriority: "high",
61
+ ...currentSrcSet ? { imageSrcSet: currentSrcSet } : {},
62
+ ...computedSizes ? { imageSizes: computedSizes } : {}
63
+ });
64
+ }
65
+ const getPlaceholderSrc = () => {
66
+ if (placeholder === "empty") {
67
+ return void 0;
68
+ }
69
+ if (placeholder === "blur") {
70
+ return blurDataURL;
71
+ }
72
+ if (placeholder.startsWith("data:image/")) {
73
+ return placeholder;
74
+ }
75
+ return void 0;
76
+ };
77
+ const placeholderSrc = getPlaceholderSrc();
78
+ const hasShowPlaceholder = !!placeholderSrc;
22
79
  const containerStyle = fill ? {
23
80
  position: "absolute",
24
81
  top: 0,
@@ -46,13 +103,17 @@ function Image({
46
103
  height: "100%",
47
104
  objectFit: "cover"
48
105
  };
49
- const lqipStyle = {
106
+ const placeholderStyle = {
50
107
  ...imgStyle,
51
- filter: "blur(20px)",
52
- transform: "scale(1.1)",
53
108
  transition: "opacity 500ms ease-out",
54
109
  opacity: isImageLoaded ? 0 : 1,
55
- zIndex: 1
110
+ zIndex: 1,
111
+ pointerEvents: "none",
112
+ // blur placeholder일 때만 blur 효과 적용
113
+ ...placeholder === "blur" ? {
114
+ filter: "blur(20px)",
115
+ transform: "scale(1.1)"
116
+ } : {}
56
117
  };
57
118
  return /* @__PURE__ */ jsxs("div", { className, style: mergedContainerStyle, children: [
58
119
  /* @__PURE__ */ jsx(
@@ -61,14 +122,28 @@ function Image({
61
122
  ...props,
62
123
  src: currentSrc,
63
124
  srcSet: currentSrcSet,
64
- sizes,
125
+ sizes: computedSizes,
65
126
  width: fill ? void 0 : currentWidth,
66
127
  height: fill ? void 0 : currentHeight,
67
- onLoad: () => setIsImageLoaded(true),
128
+ loading: priority ? "eager" : "lazy",
129
+ fetchPriority: priority ? "high" : void 0,
130
+ onLoad: (e) => {
131
+ setIsImageLoaded(true);
132
+ onLoad?.(e);
133
+ },
134
+ onError,
68
135
  style: { ...imgStyle, zIndex: 0 }
69
136
  }
70
137
  ),
71
- currentLqip && /* @__PURE__ */ jsx("img", { src: currentLqip, alt: "", "aria-hidden": "true", style: lqipStyle })
138
+ hasShowPlaceholder && /* @__PURE__ */ jsx(
139
+ "img",
140
+ {
141
+ src: placeholderSrc,
142
+ alt: "",
143
+ "aria-hidden": "true",
144
+ style: placeholderStyle
145
+ }
146
+ )
72
147
  ] });
73
148
  }
74
149
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/react/Image.tsx"],"names":[],"mappings":";;;;AAwBe,SAAR,KAAA,CAAuB;AAAA,EAC5B,GAAA;AAAA;AAAA,EACA,IAAA,GAAO,KAAA;AAAA,EACP,KAAA,GAAQ,OAAA;AAAA,EACR,SAAA,GAAY,EAAA;AAAA,EACZ,KAAA;AAAA,EACA,GAAG;AACL,CAAA,EAAe;AACb,EAAA,MAAM,CAAC,aAAA,EAAe,gBAAgB,CAAA,GAAI,SAAS,KAAK,CAAA;AAGxD,EAAA,MAAM;AAAA,IACJ,GAAA,EAAK,UAAA;AAAA,IACL,MAAA,EAAQ,aAAA;AAAA,IACR,OAAA,EAAS,WAAA;AAAA,IACT,KAAA,EAAO,YAAA;AAAA,IACP,MAAA,EAAQ;AAAA,GACV,GAAI,GAAA;AAGJ,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;AAAA,IAEV,GAAI,YAAA,IAAgB,aAAA,GAChB,EAAE,WAAA,EAAa,CAAA,EAAG,YAAY,CAAA,GAAA,EAAM,aAAa,CAAA,CAAA,EAAG,GACpD;AAAC,GACP;AAEJ,EAAA,MAAM,oBAAA,GAAuB,EAAE,GAAG,cAAA,EAAgB,GAAG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\"react\";\nimport type { ResponsiveImageData } from \"../types\";\n\n// ?vite-image import 결과 타입\ninterface BaseImageProps\n extends Omit<\n ImgHTMLAttributes<HTMLImageElement>,\n \"src\" | \"srcSet\" | \"width\" | \"height\"\n > {\n // 핵심 변경: src는 무조건 최적화된 이미지 객체만 받음\n src: ResponsiveImageData;\n sizes?: string;\n}\n\ninterface FillImageProps extends BaseImageProps {\n fill: true;\n}\n\ninterface StandardImageProps extends BaseImageProps {\n fill?: false | undefined;\n}\n\nexport type ImageProps = FillImageProps | StandardImageProps;\n\nexport default function Image({\n src, // 이제 이 src는 객체입니다.\n fill = false,\n sizes = \"100vw\",\n className = \"\",\n style,\n ...props\n}: ImageProps) {\n const [isImageLoaded, setIsImageLoaded] = useState(false);\n\n // 1. 데이터 추출: src 객체에서 바로 꺼내씀 (병합 로직 제거)\n const {\n src: currentSrc,\n srcSet: currentSrcSet,\n lqipSrc: currentLqip,\n width: currentWidth,\n height: currentHeight,\n } = src;\n\n // 2. 컨테이너 스타일 계산 (기존 로직 유지)\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 // Standard 모드일 때 aspect-ratio 처리\n ...(currentWidth && currentHeight\n ? { aspectRatio: `${currentWidth} / ${currentHeight}` }\n : {}),\n };\n\n const mergedContainerStyle = { ...containerStyle, ...style };\n\n // 3. 실제 이미지 스타일 (기존 로직 유지)\n const imgStyle: 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 // 4. LQIP 스타일 (기존 로직 유지)\n const lqipStyle: CSSProperties = {\n ...imgStyle,\n filter: \"blur(20px)\",\n transform: \"scale(1.1)\",\n transition: \"opacity 500ms ease-out\",\n opacity: isImageLoaded ? 0 : 1,\n zIndex: 1,\n };\n\n return (\n <div className={className} style={mergedContainerStyle}>\n {/* 실제 이미지 */}\n <img\n {...props}\n src={currentSrc}\n srcSet={currentSrcSet}\n sizes={sizes}\n width={fill ? undefined : currentWidth}\n height={fill ? undefined : currentHeight}\n onLoad={() => setIsImageLoaded(true)}\n style={{ ...imgStyle, zIndex: 0 }}\n />\n\n {/* LQIP 레이어 */}\n {currentLqip && (\n <img src={currentLqip} alt=\"\" aria-hidden=\"true\" style={lqipStyle} />\n )}\n </div>\n );\n}\n"]}
1
+ {"version":3,"sources":["../../src/react/Image.tsx"],"names":[],"mappings":";;;;;AAsCA,SAAS,wBAAwB,MAAA,EAAyB;AACxD,EAAA,IAAI,CAAC,MAAA,EAAQ;AACX,IAAA,OAAO,OAAA;AAAA,EACT;AAGA,EAAA,MAAM,YAAA,GAAe,MAAA,CAAO,KAAA,CAAM,SAAS,CAAA;AAC3C,EAAA,IAAI,CAAC,YAAA,IAAgB,YAAA,CAAa,MAAA,KAAW,CAAA,EAAG;AAC9C,IAAA,OAAO,OAAA;AAAA,EACT;AAGA,EAAA,MAAM,cAAc,YAAA,CACjB,GAAA,CAAI,CAAC,KAAA,KAAU,QAAA,CAAS,MAAM,OAAA,CAAQ,GAAA,EAAK,EAAE,CAAA,EAAG,EAAE,CAAC,CAAA,CACnD,IAAA,CAAK,CAAC,CAAA,EAAG,CAAA,KAAM,IAAI,CAAC,CAAA;AAEvB,EAAA,IAAI,WAAA,CAAY,WAAW,CAAA,EAAG;AAC5B,IAAA,OAAO,OAAA;AAAA,EACT;AAIA,EAAA,MAAM,YAAsB,EAAC;AAE7B,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,WAAA,CAAY,QAAQ,CAAA,EAAA,EAAK;AAC3C,IAAA,MAAM,UAAA,GAAa,YAAY,CAAC,CAAA;AAChC,IAAA,IAAI,CAAA,KAAM,WAAA,CAAY,MAAA,GAAS,CAAA,EAAG;AAEhC,MAAA,SAA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A,QACZ,KAAA,EAAO;AAAA;AAAA;AACT,GAAA,EAEJ,CAAA;AAEJ","file":"index.js","sourcesContent":["import { useState, type ImgHTMLAttributes, type CSSProperties } from \"react\";\nimport { preload } from \"react-dom\";\nimport type { ResponsiveImageData } from \"../types\";\n\n// placeholder 타입 정의 (Next.js Image 호환)\ntype PlaceholderValue = \"empty\" | \"blur\" | `data:image/${string}`;\n\n// ?vite-image import 결과 타입\ninterface BaseImageProps\n extends Omit<\n ImgHTMLAttributes<HTMLImageElement>,\n \"src\" | \"srcSet\" | \"width\" | \"height\"\n > {\n // 핵심 변경: src는 무조건 최적화된 이미지 객체만 받음\n src: ResponsiveImageData;\n sizes?: string; // Optional: 제공되지 않으면 자동 계산\n placeholder?: PlaceholderValue; // Next.js Image 호환: 'empty' | 'blur' | 'data:image/...'\n blurDataURL?: string; // Optional: 커스텀 blur placeholder (src.blurDataURL보다 우선)\n priority?: boolean; // Next.js Image 호환: true일 경우 높은 우선순위로 preload\n onLoad?: (event: React.SyntheticEvent<HTMLImageElement, Event>) => void;\n onError?: (event: React.SyntheticEvent<HTMLImageElement, Event>) => void;\n}\n\ninterface FillImageProps extends BaseImageProps {\n fill: true;\n}\n\ninterface StandardImageProps extends BaseImageProps {\n fill?: false | undefined;\n}\n\nexport type ImageProps = FillImageProps | StandardImageProps;\n\n/**\n * srcSet에서 breakpoints를 추출하여 sizes 문자열을 자동 생성\n * @param srcSet - \"url1 640w, url2 1024w, url3 1920w\" 형식의 문자열\n * @returns sizes 속성 문자열\n */\nfunction generateSizesFromSrcSet(srcSet?: string): string {\n if (!srcSet) {\n return \"100vw\";\n }\n\n // srcSet에서 width 값들 추출 (예: \"640w\", \"1024w\", \"1920w\")\n const widthMatches = srcSet.match(/(\\d+)w/g);\n if (!widthMatches || widthMatches.length === 0) {\n return \"100vw\";\n }\n\n // width 값들을 숫자로 변환하고 정렬\n const breakpoints = widthMatches\n .map((match) => parseInt(match.replace(\"w\", \"\"), 10))\n .sort((a, b) => a - b);\n\n if (breakpoints.length === 0) {\n return \"100vw\";\n }\n\n // breakpoints를 기반으로 sizes 문자열 생성\n // 예: \"(max-width: 640px) 100vw, (max-width: 1024px) 100vw, 1920px\"\n const sizeParts: string[] = [];\n\n for (let i = 0; i < breakpoints.length; i++) {\n const breakpoint = breakpoints[i];\n if (i === breakpoints.length - 1) {\n // 마지막 breakpoint는 최대 크기로 설정\n sizeParts.push(`${breakpoint}px`);\n } else {\n // 중간 breakpoint들은 미디어 쿼리로 설정\n sizeParts.push(`(max-width: ${breakpoint}px) 100vw`);\n }\n }\n\n return sizeParts.join(\", \");\n}\n\nexport default function Image({\n src, // 이제 이 src는 객체입니다.\n fill = false,\n sizes,\n placeholder = \"empty\", // 기본값: empty (Next.js Image 호환)\n blurDataURL: customBlurDataURL, // 사용자가 직접 제공한 blurDataURL (우선순위 높음)\n priority = false, // 기본값: false (Next.js Image 호환)\n className = \"\",\n style,\n onLoad,\n onError,\n ...props\n}: ImageProps) {\n const [isImageLoaded, setIsImageLoaded] = useState(false);\n\n // 1. 데이터 추출: src 객체에서 바로 꺼내씀 (병합 로직 제거)\n const {\n src: currentSrc,\n srcSet: currentSrcSet,\n blurDataURL: srcBlurDataURL, // 번들러가 생성한 blurDataURL\n width: currentWidth,\n height: currentHeight,\n } = src;\n\n // blurDataURL 우선순위: prop으로 제공된 것 > src 객체의 것\n const blurDataURL = customBlurDataURL ?? srcBlurDataURL;\n\n // 2. sizes 자동 계산: 제공되지 않으면 srcSet 기반으로 자동 생성\n const computedSizes =\n sizes ?? (fill ? \"100vw\" : generateSizesFromSrcSet(currentSrcSet));\n\n if (priority && currentSrc) {\n preload(currentSrc, {\n as: \"image\",\n fetchPriority: \"high\",\n ...(currentSrcSet ? { imageSrcSet: currentSrcSet } : {}),\n ...(computedSizes ? { imageSizes: computedSizes } : {}),\n });\n }\n\n // 3. placeholder 처리 (Next.js Image 호환)\n const getPlaceholderSrc = (): string | undefined => {\n if (placeholder === \"empty\") {\n return undefined;\n }\n if (placeholder === \"blur\") {\n return blurDataURL;\n }\n // data:image/... 형식의 직접 제공된 placeholder\n if (placeholder.startsWith(\"data:image/\")) {\n return placeholder;\n }\n // 기본값: empty\n return undefined;\n };\n\n const placeholderSrc = getPlaceholderSrc();\n const hasShowPlaceholder = !!placeholderSrc;\n\n // 5. 컨테이너 스타일 계산 (기존 로직 유지)\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 // Standard 모드일 때 aspect-ratio 처리\n ...(currentWidth && currentHeight\n ? { aspectRatio: `${currentWidth} / ${currentHeight}` }\n : {}),\n };\n\n const mergedContainerStyle = { ...containerStyle, ...style };\n\n // 6. 실제 이미지 스타일 (기존 로직 유지)\n const imgStyle: 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 // 7. Placeholder 스타일 (blur 모드일 때만 blur 효과 적용)\n const placeholderStyle: CSSProperties = {\n ...imgStyle,\n transition: \"opacity 500ms ease-out\",\n opacity: isImageLoaded ? 0 : 1,\n zIndex: 1,\n pointerEvents: \"none\",\n\n // blur placeholder일 때만 blur 효과 적용\n ...(placeholder === \"blur\"\n ? {\n filter: \"blur(20px)\",\n transform: \"scale(1.1)\",\n }\n : {}),\n };\n\n return (\n <div className={className} style={mergedContainerStyle}>\n {/* 실제 이미지 */}\n <img\n {...props}\n src={currentSrc}\n srcSet={currentSrcSet}\n sizes={computedSizes}\n width={fill ? undefined : currentWidth}\n height={fill ? undefined : currentHeight}\n loading={priority ? \"eager\" : \"lazy\"}\n fetchPriority={priority ? \"high\" : undefined}\n onLoad={(e) => {\n setIsImageLoaded(true);\n onLoad?.(e);\n }}\n onError={onError}\n style={{ ...imgStyle, zIndex: 0 }}\n />\n\n {/* Placeholder 레이어 (placeholder prop에 따라 표시) */}\n {hasShowPlaceholder && (\n <img\n src={placeholderSrc}\n alt=\"\"\n aria-hidden=\"true\"\n style={placeholderStyle}\n />\n )}\n </div>\n );\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@son426/vite-image",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "A Vite plugin and React component for optimized images with LQIP support using vite-imagetools",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -52,12 +52,15 @@
52
52
  },
53
53
  "peerDependencies": {
54
54
  "react": "^18.0.0 || ^19.0.0",
55
+ "react-dom": "^18.0.0 || ^19.0.0",
55
56
  "vite": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@types/node": "^20.0.0",
59
60
  "@types/react": "^18.0.0 || ^19.0.0",
61
+ "@types/react-dom": "^19.2.3",
60
62
  "react": "^19.1.0",
63
+ "react-dom": "^19.1.0",
61
64
  "tsup": "^8.0.0",
62
65
  "typescript": "^5.0.0",
63
66
  "vite": "^7.0.4"