@son426/vite-image 0.1.8 → 0.2.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 +1 -0
- package/README.md +91 -7
- package/dist/index.d.ts +2 -2
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -21,13 +21,51 @@ Simply add the plugin to your config, and start using the `<Image />` component
|
|
|
21
21
|
|
|
22
22
|
Add it to `vite.config.ts`, and use it like this:
|
|
23
23
|
|
|
24
|
+
**Option 1: Using query string (default)**
|
|
25
|
+
|
|
24
26
|
```tsx
|
|
25
27
|
// vite.config.ts
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
import { defineConfig } from "vite";
|
|
29
|
+
import { viteImage } from "@son426/vite-image/plugin";
|
|
30
|
+
|
|
31
|
+
export default defineConfig({
|
|
32
|
+
plugins: [
|
|
33
|
+
viteImage(), // Default breakpoints: [640, 1024, 1920]
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Component
|
|
38
|
+
import Image from "@son426/vite-image/react";
|
|
39
|
+
import myBg from "./assets/background.jpg?vite-image";
|
|
40
|
+
|
|
41
|
+
export default function Page() {
|
|
42
|
+
return (
|
|
43
|
+
<Image
|
|
44
|
+
src={myBg}
|
|
45
|
+
alt="Optimized Background"
|
|
46
|
+
fill
|
|
47
|
+
priority
|
|
48
|
+
placeholder="blur"
|
|
49
|
+
/>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Option 2: Auto-apply without query string**
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
// vite.config.ts
|
|
58
|
+
import { defineConfig } from "vite";
|
|
59
|
+
import { viteImage } from "@son426/vite-image/plugin";
|
|
60
|
+
|
|
61
|
+
export default defineConfig({
|
|
62
|
+
plugins: [
|
|
63
|
+
viteImage({
|
|
64
|
+
autoApply: {
|
|
65
|
+
extensions: [".jpg"],
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
],
|
|
31
69
|
});
|
|
32
70
|
|
|
33
71
|
// Component
|
|
@@ -47,6 +85,8 @@ export default function Page() {
|
|
|
47
85
|
}
|
|
48
86
|
```
|
|
49
87
|
|
|
88
|
+
> **Note**: For auto-apply, you'll need to add type declarations. See the [TypeScript Setup](#typescript-setup) section below.
|
|
89
|
+
|
|
50
90
|
## Installation
|
|
51
91
|
|
|
52
92
|
Install the package. `vite-imagetools` and `@rollup/pluginutils` are included as dependencies.
|
|
@@ -67,6 +107,31 @@ Just a standard Vite + React project.
|
|
|
67
107
|
- react (>= 18.0.0)
|
|
68
108
|
- react-dom (>= 18.0.0)
|
|
69
109
|
|
|
110
|
+
## TypeScript Setup
|
|
111
|
+
|
|
112
|
+
**⚠️ Temporary Setup Required**: Currently, you need to add type declarations manually. This will be automated in a future update.
|
|
113
|
+
|
|
114
|
+
Add the following to your project's type definition file (e.g., `src/vite-env.d.ts`):
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
/// <reference types="vite/client" />
|
|
118
|
+
|
|
119
|
+
interface ResponsiveImageData {
|
|
120
|
+
src: string;
|
|
121
|
+
width: number;
|
|
122
|
+
height: number;
|
|
123
|
+
srcSet?: string;
|
|
124
|
+
blurDataURL?: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
declare module "*?vite-image" {
|
|
128
|
+
const content: ResponsiveImageData;
|
|
129
|
+
export default content;
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
This ensures TypeScript recognizes `?vite-image` imports. Sorry for the temporary manual step—we're working on automating this.
|
|
134
|
+
|
|
70
135
|
## Usage
|
|
71
136
|
|
|
72
137
|
### 1. Setup Vite Plugin
|
|
@@ -159,12 +224,29 @@ viteImage({
|
|
|
159
224
|
},
|
|
160
225
|
});
|
|
161
226
|
|
|
227
|
+
// Add type declarations for autoApply extensions
|
|
228
|
+
// In your project's vite-env.d.ts or a custom .d.ts file:
|
|
229
|
+
// (Make sure you've already added the ResponsiveImageData interface from the TypeScript Setup section above)
|
|
230
|
+
|
|
231
|
+
declare module "*.jpg" {
|
|
232
|
+
const imageData: ResponsiveImageData;
|
|
233
|
+
export default imageData;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
declare module "*.png" {
|
|
237
|
+
const imageData: ResponsiveImageData;
|
|
238
|
+
export default imageData;
|
|
239
|
+
}
|
|
240
|
+
|
|
162
241
|
// Component
|
|
163
242
|
import bgImage from "@/assets/background.jpg"; // No query needed
|
|
164
243
|
<Image src={bgImage} alt="Background" />;
|
|
165
244
|
```
|
|
166
245
|
|
|
167
|
-
**Important**:
|
|
246
|
+
**Important**:
|
|
247
|
+
|
|
248
|
+
- The `src` prop must receive the imported object directly. String URLs are not supported.
|
|
249
|
+
- When using `autoApply`, you need to add type declarations for the extensions you're using in your project's type definition file (e.g., `vite-env.d.ts`).
|
|
168
250
|
|
|
169
251
|
The `?vite-image` query (or autoApply) automatically generates:
|
|
170
252
|
|
|
@@ -293,9 +375,11 @@ Type definitions are included. The package also extends vite-imagetools types fo
|
|
|
293
375
|
|
|
294
376
|
```typescript
|
|
295
377
|
import Image from "@son426/vite-image/react";
|
|
296
|
-
import type { ImageProps
|
|
378
|
+
import type { ImageProps } from "@son426/vite-image/react";
|
|
297
379
|
```
|
|
298
380
|
|
|
381
|
+
**Note**: After setting up the type declarations in the "TypeScript Setup" section above, you can use `ResponsiveImageData` directly in your code without importing it.
|
|
382
|
+
|
|
299
383
|
## How It Works
|
|
300
384
|
|
|
301
385
|
1. **Image Processing**: When you import an image with `?vite-image` query or via `autoApply`, the plugin automatically generates:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { ImageProps } from './react/index.js';
|
|
2
1
|
export { A as AutoApplyConfig, R as ResponsiveImageData, V as ViteImageConfig } from './types-B08JIQxT.js';
|
|
2
|
+
export { ImageProps } from './react/index.js';
|
|
3
3
|
export { ViteImagePluginOptions, viteImage } from './plugin/index.js';
|
|
4
|
+
import 'vite-imagetools';
|
|
4
5
|
import 'react/jsx-runtime';
|
|
5
6
|
import 'react';
|
|
6
|
-
import 'vite-imagetools';
|
|
7
7
|
import 'vite';
|
package/package.json
CHANGED