vite-plugin-image-srcset 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nikita Barinov
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.
package/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # vite-plugin-image-srcset
2
+
3
+ Simple Vite plugin to generate `srcset` attributes for responsive images.
4
+
5
+ Import a single image and get `src` + `srcSet` for 1x/2x/3x pixel densities.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install -D vite-plugin-image-srcset
11
+ # or
12
+ pnpm add -D vite-plugin-image-srcset
13
+ ```
14
+
15
+ ## Setup
16
+
17
+ ### 1. Add plugin to Vite config
18
+
19
+ ```ts
20
+ // vite.config.ts
21
+ import { defineConfig } from 'vite';
22
+ import { srcSetPlugin } from 'vite-plugin-image-srcset';
23
+
24
+ export default defineConfig({
25
+ plugins: [srcSetPlugin()],
26
+ });
27
+ ```
28
+
29
+ ### 2. Add types (TypeScript)
30
+
31
+ Add to your `tsconfig.json`:
32
+
33
+ ```json
34
+ {
35
+ "compilerOptions": {
36
+ "types": ["vite-plugin-image-srcset/client"]
37
+ }
38
+ }
39
+ ```
40
+
41
+ Or add a reference in your `vite-env.d.ts`:
42
+
43
+ ```ts
44
+ /// <reference types="vite/client" />
45
+ + /// <reference types="vite-plugin-image-srcset/client" />
46
+ ```
47
+
48
+ ## Usage
49
+
50
+ ### File structure
51
+
52
+ The plugin expects images with `@2x` and `@3x` suffixes:
53
+
54
+ ```
55
+ assets/
56
+ icon.png # 1x (base)
57
+ icon@2x.png # 2x
58
+ icon@3x.png # 3x
59
+ ```
60
+
61
+ ### Import with `?srcset` query
62
+
63
+ ```tsx
64
+ import icon from './assets/icon.png?srcset';
65
+
66
+ // icon = { src: '/assets/icon.png', srcSet: '/assets/icon.png 1x, /assets/icon@2x.png 2x, /assets/icon@3x.png 3x' }
67
+ ```
68
+
69
+ ### React example
70
+
71
+ ```tsx
72
+ import icon from './assets/icon.png?srcset';
73
+
74
+ function App() {
75
+ return <img {...icon} alt="Icon" />;
76
+ }
77
+ ```
78
+
79
+ ### HTML example
80
+
81
+ ```ts
82
+ import icon from './assets/icon.png?srcset';
83
+
84
+ document.body.innerHTML = `<img src="${icon.src}" srcset="${icon.srcSet}" alt="Icon" />`;
85
+ ```
86
+
87
+ ## API
88
+
89
+ The plugin transforms imports with `?srcset` query into an object:
90
+
91
+ ```ts
92
+ interface ImageSrcSet {
93
+ src: string; // URL to 1x image
94
+ srcSet: string; // srcset string: "url1 1x, url2 2x, url3 3x"
95
+ }
96
+ ```
97
+
98
+ ## Supported formats
99
+
100
+ - `.png`
101
+ - `.jpg`
102
+
103
+ ## License
104
+
105
+ MIT
package/client.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ declare module '*.png?srcset' {
2
+ type ImageSrcSet = import('./dist/index').ImageSrcSet;
3
+ const srcset: ImageSrcSet;
4
+
5
+ export default srcset;
6
+ }
7
+
8
+ declare module '*.jpg?srcset' {
9
+ type ImageSrcSet = import('./dist/index').ImageSrcSet;
10
+ const srcset: ImageSrcSet;
11
+
12
+ export default srcset;
13
+ }
@@ -0,0 +1,9 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface ImageSrcSet {
4
+ srcSet: string;
5
+ src: string;
6
+ }
7
+ declare function srcSetPlugin(): Plugin;
8
+
9
+ export { type ImageSrcSet, srcSetPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ // src/index.ts
2
+ import path from "path";
3
+ function srcSetPlugin() {
4
+ return {
5
+ name: "vite-plugin-image-srcset",
6
+ enforce: "pre",
7
+ transform(_, id) {
8
+ if (!id.includes("?srcset")) {
9
+ return null;
10
+ }
11
+ const [filePath] = id.split("?");
12
+ const ext = path.extname(filePath);
13
+ const baseName = path.basename(filePath, ext);
14
+ const dir = path.dirname(filePath);
15
+ const x1 = filePath;
16
+ const x2 = `${dir}/${baseName}@2x${ext}`;
17
+ const x3 = `${dir}/${baseName}@3x${ext}`;
18
+ return {
19
+ code: `
20
+ import x1 from '${x1}?url';
21
+ import x2 from '${x2}?url';
22
+ import x3 from '${x3}?url';
23
+
24
+ export default {
25
+ srcSet: \`\${x1} 1x, \${x2} 2x, \${x3} 3x\`,
26
+ src: x1,
27
+ };
28
+ `,
29
+ map: null
30
+ };
31
+ }
32
+ };
33
+ }
34
+ export {
35
+ srcSetPlugin
36
+ };
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import path from 'node:path';\nimport { type Plugin } from 'vite';\n\ninterface ImageSrcSet {\n srcSet: string;\n src: string;\n}\n\nfunction srcSetPlugin(): Plugin {\n return {\n name: 'vite-plugin-image-srcset',\n enforce: 'pre',\n transform(_, id) {\n if (!id.includes('?srcset')) {\n return null;\n }\n\n const [filePath] = id.split('?');\n const ext = path.extname(filePath);\n const baseName = path.basename(filePath, ext);\n const dir = path.dirname(filePath);\n\n const x1 = filePath;\n const x2 = `${dir}/${baseName}@2x${ext}`;\n const x3 = `${dir}/${baseName}@3x${ext}`;\n\n return {\n code: `\n import x1 from '${x1}?url';\n import x2 from '${x2}?url';\n import x3 from '${x3}?url';\n\n export default {\n srcSet: \\`\\${x1} 1x, \\${x2} 2x, \\${x3} 3x\\`,\n src: x1,\n };\n `,\n map: null,\n };\n },\n };\n}\n\nexport { srcSetPlugin };\nexport type { ImageSrcSet };\n"],"mappings":";AAAA,OAAO,UAAU;AAQjB,SAAS,eAAuB;AAC9B,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IACT,UAAU,GAAG,IAAI;AACf,UAAI,CAAC,GAAG,SAAS,SAAS,GAAG;AAC3B,eAAO;AAAA,MACT;AAEA,YAAM,CAAC,QAAQ,IAAI,GAAG,MAAM,GAAG;AAC/B,YAAM,MAAM,KAAK,QAAQ,QAAQ;AACjC,YAAM,WAAW,KAAK,SAAS,UAAU,GAAG;AAC5C,YAAM,MAAM,KAAK,QAAQ,QAAQ;AAEjC,YAAM,KAAK;AACX,YAAM,KAAK,GAAG,GAAG,IAAI,QAAQ,MAAM,GAAG;AACtC,YAAM,KAAK,GAAG,GAAG,IAAI,QAAQ,MAAM,GAAG;AAEtC,aAAO;AAAA,QACL,MAAM;AAAA,4BACc,EAAE;AAAA,4BACF,EAAE;AAAA,4BACF,EAAE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAOtB,KAAK;AAAA,MACP;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "vite-plugin-image-srcset",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "description": "Simple Vite plugin to generate srcset attributes for images",
6
+ "author": "Nikita Barinov <nikitabarinov2301@gmail.com>",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/nbarinov/vite-plugin-image-srcset#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/nbarinov/vite-plugin-image-srcset.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/nbarinov/vite-plugin-image-srcset/issues"
15
+ },
16
+ "peerDependencies": {
17
+ "vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^22.0.0",
21
+ "prettier": "^3.8.0",
22
+ "tsup": "^8.5.1",
23
+ "typescript": "^5.9.3"
24
+ },
25
+ "main": "./dist/index.mjs",
26
+ "types": "./dist/index.d.mts",
27
+ "typings": "./dist/index.d.mts",
28
+ "module": "./dist/index.mjs",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.mts",
32
+ "import": "./dist/index.mjs"
33
+ },
34
+ "./client": {
35
+ "types": "./client.d.ts"
36
+ }
37
+ },
38
+ "keywords": [
39
+ "vite",
40
+ "vite-plugin",
41
+ "image",
42
+ "srcset",
43
+ "responsive",
44
+ "retina",
45
+ "hdpi"
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "files": [
51
+ "dist",
52
+ "client.d.ts"
53
+ ],
54
+ "engines": {
55
+ "node": ">=18.0.0"
56
+ },
57
+ "scripts": {
58
+ "typecheck": "tsc --noEmit",
59
+ "dev": "tsup",
60
+ "build": "tsup",
61
+ "format": "prettier --write .",
62
+ "test": "echo \"Error: no test specified\" && exit 1"
63
+ }
64
+ }