@open-xchange/vite-plugin-icon-sprite 1.1.1 → 1.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## `1.3.0` – 2026-Apr-09
4
+
5
+ - chore: switch to `tsdown` for building packages
6
+
7
+ ## `1.2.0` – 2026-Mar-13
8
+
9
+ - added: support for Vite 8
10
+
3
11
  ## `1.1.1` – 2025-Sep-09
4
12
 
5
13
  - chore: bump dependencies
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Open-Xchange GmbH, Germany <info@open-xchange.com>
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 CHANGED
@@ -16,7 +16,7 @@ Common configuration options for SVG sprites and PNG sprites.
16
16
 
17
17
  #### Option `format`
18
18
 
19
- - Type: `"svg" | "png"`
19
+ - Type: `'svg' | 'png'`
20
20
  - _required_
21
21
 
22
22
  The file format specifier of the icon sprite, and the source images. Depending on the value of this option, the plugin expects various file-format specific options (see below).
@@ -44,16 +44,16 @@ Example how to consume the SVG sprite in source code:
44
44
  ```ts
45
45
  // path/to/source.ts
46
46
 
47
- import svgSprite from "virtual:svg/my-icons.svg"
47
+ import svgSprite from 'virtual:svg/my-icons.svg'
48
48
 
49
49
  // insert the SVG sprite into the DOM to be able to refer to the <symbol> elements
50
- document.createElement("div").innerHTML = svgSprite
50
+ document.createElement('div').innerHTML = svgSprite
51
51
 
52
52
  // create an SVG element referring to an icon in the sprite
53
53
  function createIcon(id: string): SVGElement {
54
- const useEl = document.createElementNS("http://www.w3.org/2000/svg", "use")
55
- useEl.setAttribute("href", "#my-icon")
56
- const iconEl = document.createElementNS("http://www.w3.org/2000/svg", "svg")
54
+ const useEl = document.createElementNS('http://www.w3.org/2000/svg', 'use')
55
+ useEl.setAttribute('href', '#my-icon')
56
+ const iconEl = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
57
57
  iconEl.append(useEl)
58
58
  return iconEl
59
59
  }
@@ -64,9 +64,9 @@ function createIcon(id: string): SVGElement {
64
64
  - Type: `string`
65
65
  - _required_
66
66
 
67
- The module name of the SVG sprite to be generated (with ".svg" extension).
67
+ The module name of the SVG sprite to be generated (with '.svg' extension).
68
68
 
69
- The SVG markup of the sprite can be imported from the virtual module path `"virtual:svg/[spriteName]"`.
69
+ The SVG markup of the sprite can be imported from the virtual module path `'virtual:svg/[spriteName]'`.
70
70
 
71
71
  #### Option `idPrefix`
72
72
 
@@ -80,17 +80,17 @@ A prefix to be added to all icon identifiers declared in the mapping file. By de
80
80
  ```ts
81
81
  // vite.config.ts
82
82
 
83
- import { defineConfig } from "vite" // or "vitest/config"
84
- import spritePlugin from "@open-xchange/vite-plugin-icon-sprite"
83
+ import { defineConfig } from 'vite' // or 'vitest/config'
84
+ import spritePlugin from '@open-xchange/vite-plugin-icon-sprite'
85
85
 
86
86
  export default defineConfig(() => {
87
87
  plugins: [
88
88
  spritePlugin({
89
- format: "svg",
90
- imagesPath: "src/icons/images",
91
- mappingPath: "src/icons/svg-mapping.yaml",
92
- spriteName: "icons.svg",
93
- idPrefix: "svg-",
89
+ format: 'svg',
90
+ imagesPath: 'src/icons/images',
91
+ mappingPath: 'src/icons/svg-mapping.yaml',
92
+ spriteName: 'icons.svg',
93
+ idPrefix: 'svg-',
94
94
  }),
95
95
  ],
96
96
  })
@@ -98,8 +98,8 @@ export default defineConfig(() => {
98
98
 
99
99
  - Collects all SVG files in the directory `src/icons/images`.
100
100
  - Uses the icon mapping in `src/icons/svg-mapping.yaml`.
101
- - Creates a virtual import `"virtual:svg/icons.svg"`.
102
- - Prefixes all icon identifiers with `svg-`, e.g. the icon key `my-icon` in the mapping file will result in the icon identifier `"svg-my-icon"` in source code.
101
+ - Creates a virtual import `'virtual:svg/icons.svg'`.
102
+ - Prefixes all icon identifiers with `svg-`, e.g. the icon key `my-icon` in the mapping file will result in the icon identifier `'svg-my-icon'` in source code.
103
103
 
104
104
  ### PNG Sprites
105
105
 
@@ -110,7 +110,7 @@ The plugin collects multiple PNG files, and generates a virtual imports for one
110
110
  - Type: `string`
111
111
  - _required_
112
112
 
113
- The module name for the CSS file (with ".css" extension). The generated CSS markup can be imported from `"virtual:png/[cssName]"`.
113
+ The module name for the CSS file (with '.css' extension). The generated CSS markup can be imported from `'virtual:png/[cssName]'`.
114
114
 
115
115
  #### Option `cssIconSize`
116
116
 
@@ -129,40 +129,41 @@ Additional padding around all icons to be generated in the sprites, in CSS pixel
129
129
  #### Option `cssIconSelector`
130
130
 
131
131
  - Type: `string`
132
- - Default: `"i.png-icon"`
132
+ - Default: `'i.png-icon'`
133
133
 
134
134
  The CSS selector for a PNG icon element to be used in all generated CSS rules.
135
135
 
136
136
  #### Option `rootLocaleAttr`
137
137
 
138
138
  - Type: `string`
139
- - Default: `"lang"`
139
+ - Default: `'lang'`
140
140
 
141
141
  Name of the root element's attribute containing the locale identifier. Needed to generate CSS selectors for localized icons.
142
142
 
143
143
  #### Option `spriteColorType`
144
144
 
145
- - Type: `"source" | "monochrome" | "alpha"`
146
- - Default: `"source"`
145
+ - Type: `'source' | 'monochrome' | 'alpha'`
146
+ - Default: `'source'`
147
147
 
148
148
  Specifies how to generate the sprite PNG files.
149
149
 
150
150
  | Value | Description |
151
151
  | - | - |
152
- | `"source"` | The source PNGs will be copied into the generated sprites unmodified. They will contain three color channels, and an alpha channel. |
153
- | `"monochrome"` | The generated sprites will be converted to monochrome. They will contain a gray channel and an alpha channel. |
154
- | `"alpha"` | Only the alpha channels of the source PNGs will be copied into the generated sprites. They will contain a single gray channel representing the original alpha channels. |
152
+ | `'source'` | The source PNGs will be copied into the generated sprites unmodified. They will contain three color channels, and an alpha channel. |
153
+ | `'monochrome'` | The generated sprites will be converted to monochrome. They will contain a gray channel and an alpha channel. |
154
+ | `'alpha'` | Only the alpha channels of the source PNGs will be copied into the generated sprites. They will contain a single gray channel representing the original alpha channels. |
155
155
 
156
156
  #### Option `spriteFillType`
157
157
 
158
- - Type: `"background" | "mask"`
159
- - Default: `"background"`
158
+ - Type: `'background' | 'mask'`
159
+ - Default: `'background'`
160
160
 
161
161
  Specifies how the sprites are supposed to be used in CSS rules.
162
162
 
163
163
  | Value | Description |
164
- | `"background"` | The sprites will be attached via "background-image". |
165
- | `"mask"` | The sprites will be attached via "mask-image". |
164
+ | - | - |
165
+ | `'background'` | The sprites will be attached via 'background-image'. |
166
+ | `'mask'` | The sprites will be attached via 'mask-image'. |
166
167
 
167
168
  All related CSS properties (e.g. `background-position` vs. `mask-position` etc.) will be generated accordingly.
168
169
 
@@ -173,7 +174,7 @@ All related CSS properties (e.g. `background-position` vs. `mask-position` etc.)
173
174
 
174
175
  List of all icon sprites with different icon sizes to be generated.
175
176
 
176
- - The keys of the dictionary are the module names of the PNG sprites (with ".png" extension). The generated PNG sprite can be imported from `"virtual:png/[key]"`.
177
+ - The keys of the dictionary are the module names of the PNG sprites (with '.png' extension). The generated PNG sprite can be imported from `'virtual:png/[key]'`.
177
178
 
178
179
  - The values of the dictionary contain configuration options for the PNG sprite:
179
180
 
@@ -187,25 +188,25 @@ List of all icon sprites with different icon sizes to be generated.
187
188
  ```ts
188
189
  // vite.config.ts
189
190
 
190
- import { defineConfig } from "vite" // or "vitest/config"
191
- import spritePlugin from "@open-xchange/vite-plugin-icon-sprite"
191
+ import { defineConfig } from 'vite' // or 'vitest/config'
192
+ import spritePlugin from '@open-xchange/vite-plugin-icon-sprite'
192
193
 
193
194
  export default defineConfig(() => {
194
195
  plugins: [
195
196
  spritePlugin({
196
- format: "png",
197
- imagesPath: "src/icons/images",
198
- mappingPath: "src/icons/png-mapping.yaml",
199
- cssName: "icons.css",
197
+ format: 'png',
198
+ imagesPath: 'src/icons/images',
199
+ mappingPath: 'src/icons/png-mapping.yaml',
200
+ cssName: 'icons.css',
200
201
  cssIconSize: 16,
201
202
  cssIconPadding: 1,
202
- cssIconSelector: "i.my-icon",
203
- rootLocaleAttr: "data-icon-locale",
204
- spriteColorType: "alpha",
205
- spriteFillType: "mask",
203
+ cssIconSelector: 'i.my-icon',
204
+ rootLocaleAttr: 'data-icon-locale',
205
+ spriteColorType: 'alpha',
206
+ spriteFillType: 'mask',
206
207
  sprites: {
207
- "icons1.png": { factor: 1, src: "[path]_16.png" },
208
- "icons2.png": { factor: 2, src: "[path]_32.png" },
208
+ 'icons1.png': { factor: 1, src: '[path]_16.png' },
209
+ 'icons2.png': { factor: 2, src: '[path]_32.png' },
209
210
  },
210
211
  }),
211
212
  ]
@@ -214,10 +215,10 @@ export default defineConfig(() => {
214
215
 
215
216
  - Collects all PNG files in the directory `src/icons/images`. The images must exist in two sizes (16px and 32px), their file names must end with `_16.png` and `_32.png` respectively (according to the options `cssIconSize` and `sprites->factor`).
216
217
  - Uses the icon mapping in `src/icons/png-mapping.yaml`.
217
- - Creates the virtual imports `"virtual:svg/icons.css"`, `"virtual:svg/icons1.png"`, and `"virtual:svg/icons2.png"`.
218
+ - Creates the virtual imports `'virtual:svg/icons.css'`, `'virtual:svg/icons1.png'`, and `'virtual:svg/icons2.png'`.
218
219
  - Adds one pixel padding around all icons in the PNG sprites.
219
220
  - Generates CSS selectors for `<i>` elements with CSS class `my-icon`.
220
- - Generates `:root[data-icon-locale]` CSS selectors for localized icons (i.e., the UI locale code must be stored in the root element's attribute "data-icon-locale").
221
+ - Generates `:root[data-icon-locale]` CSS selectors for localized icons (i.e., the UI locale code must be stored in the root element's attribute 'data-icon-locale').
221
222
  - Generates PNG sprites consisting of an 8-bit alpha channel only.
222
223
  - Generates CSS rules using CSS mask (instead of background).
223
224
 
@@ -235,7 +236,7 @@ The plugin expects an icon mapping file (plugin option `mappingPath`) which is a
235
236
 
236
237
  - Only the base name of the source image file must be specified relative to the configured root directory of the image files. It must not contain the image size suffix (_PNG only_), nor a file extension (`.svg` or `.png`). See examples below.
237
238
 
238
- - Localized icons will be described by a dictionary mapping the ISO language identifiers (as comma-separated strings) to the image base names (as described above). The special locale code `"*"` is mandatory, and defines a default icon for unlisted locales. See examples below.
239
+ - Localized icons will be described by a dictionary mapping the ISO language identifiers (as comma-separated strings) to the image base names (as described above). The special locale code `'*'` is mandatory, and defines a default icon for unlisted locales. See examples below.
239
240
 
240
241
  #### Icon Mapping Examples
241
242
 
@@ -253,7 +254,7 @@ my-icon: commons/icon1
253
254
  other-icon: commons/icon2
254
255
  ```
255
256
 
256
- In source code, the icons can be used with the identifiers `"my-icon"` and `"other-icon"`.
257
+ In source code, the icons can be used with the identifiers `'my-icon'` and `'other-icon'`.
257
258
 
258
259
  ##### Example 2: Simple PNG Icons
259
260
 
@@ -281,7 +282,7 @@ other-icon: commons/icon2
281
282
  # svg-mapping.yaml
282
283
 
283
284
  my-icon:
284
- "*": commons/icon1
285
+ '*': commons/icon1
285
286
  de,fr: commons/icon2
286
287
  sv: commons/icon3
287
288
  ```
@@ -294,7 +295,7 @@ This package provides a JSON schema that can be used for validation in editors.
294
295
 
295
296
  ##### JSON Mapping File
296
297
 
297
- Add the path to the schema file as property `"$schema"` to the mapping file:
298
+ Add the path to the schema file as property `'$schema'` to the mapping file:
298
299
 
299
300
  ```json
300
301
  // mapping.json
@@ -325,4 +326,4 @@ In VS Code, the plugin [redhat.vscode-yaml](https://marketplace.visualstudio.com
325
326
 
326
327
  ### Logging
327
328
 
328
- By default, warning messages and error messages will be logged to the shell. The environment variable `PLUGIN_REPLACE_ICON_SPRITE` can be used to change the log level of this plugin. Possible values are `info`, `warn`, `error`, and `silent`.
329
+ By default, warning messages and error messages will be logged to the shell. The environment variable `PLUGIN_ICON_SPRITE_LOGLEVEL` can be used to change the log level of this plugin. Possible values are `info`, `warn`, `error`, and `silent`.
@@ -0,0 +1,147 @@
1
+ import { Dict } from "@open-xchange/vite-helper/utils";
2
+ import { PluginHelper } from "@open-xchange/vite-helper";
3
+ import { Plugin } from "vite";
4
+
5
+ //#region src/helper.d.ts
6
+ /**
7
+ * Common configuration options (independent from the icon file format) for the
8
+ * plugin '@open-xchange/vite-plugin-icon-sprite'.
9
+ *
10
+ * @template FormatT
11
+ * The file format specifier of the icon sprite, and the source images.
12
+ */
13
+ interface SpritePluginBaseOptions<FormatT extends string> {
14
+ /**
15
+ * The file format specifier of the icon sprite, and the source images.
16
+ */
17
+ format: FormatT;
18
+ /**
19
+ * Path to root directory with all image resource files to be processed.
20
+ */
21
+ imagesPath: string;
22
+ /**
23
+ * Path to the JSON or YAML configuration file containing the mapping between
24
+ * icon identifiers and SVG source file names.
25
+ */
26
+ mappingPath: string;
27
+ }
28
+ //#endregion
29
+ //#region src/plugin-svg.d.ts
30
+ /**
31
+ * Configuration options for generating an SVG sprite from SVG source images.
32
+ */
33
+ interface SvgSpritePluginOptions extends SpritePluginBaseOptions<'svg'> {
34
+ /**
35
+ * Module name of the SVG sprite to be generated (with '.svg' extension). The
36
+ * SVG markup of the sprite can be imported from the virtual module path
37
+ * `'virtual:svg/[spriteName]'`.
38
+ */
39
+ spriteName: string;
40
+ /**
41
+ * A prefix to be added to all icon identifiers declared in the mapping file.
42
+ * By default, no prefix will be added.
43
+ */
44
+ idPrefix?: string;
45
+ }
46
+ //#endregion
47
+ //#region src/plugin-png.d.ts
48
+ /**
49
+ * Configuration options for generating PNG sprites from PNG source images.
50
+ */
51
+ interface PngSpritePluginOptions extends SpritePluginBaseOptions<'png'> {
52
+ /**
53
+ * Module name for the CSS file (with '.css' extension). The generated CSS
54
+ * markup can be imported from `'virtual:png/[cssName]'`.
55
+ */
56
+ cssName: string;
57
+ /**
58
+ * Base size of all icons, in CSS pixels.
59
+ */
60
+ cssIconSize: number;
61
+ /**
62
+ * Additional padding around all icons to be generated in the sprites, in CSS
63
+ * pixels.
64
+ *
65
+ * @default 0
66
+ */
67
+ cssIconPadding?: number;
68
+ /**
69
+ * The CSS selector for a PNG icon element to be used in all generated CSS
70
+ * rules.
71
+ *
72
+ * @default 'i.png-icon'
73
+ */
74
+ cssIconSelector?: string;
75
+ /**
76
+ * Name of the root element's attribute containing the locale identifier.
77
+ * Needed to generate CSS selectors for localized icons.
78
+ *
79
+ * @default 'lang'
80
+ */
81
+ rootLocaleAttr?: string;
82
+ /**
83
+ * Specifies how to generate the sprite PNG files.
84
+ *
85
+ * - `'source'`: The source PNGs will be copied into the generated sprites
86
+ * unmodified. They will contain three color channels and an alpha channel.
87
+ * - `'monochrome'`: The generated sprites will be converted to monochrome.
88
+ * They will contain a gray channel and an alpha channel.
89
+ * - `'alpha'`: Only the alpha channels of the source PNGs will be copied
90
+ * into the generated sprites. They will contain a single gray channel
91
+ * representing the original alpha channels.
92
+ *
93
+ * @default 'source'
94
+ */
95
+ spriteColorType?: 'source' | 'monochrome' | 'alpha';
96
+ /**
97
+ * Specifies how the sprites are supposed to be used in CSS rules.
98
+ *
99
+ * - `'background'`: The sprites will be attached via 'background-image'.
100
+ * - `'mask'`: The sprites will be attached via 'mask-image'.
101
+ *
102
+ * All related CSS properties (e.g. '(background|mask)-position' etc.) will
103
+ * be generated accordingly.
104
+ *
105
+ * @default 'background'
106
+ */
107
+ spriteFillType?: 'background' | 'mask';
108
+ /**
109
+ * List of all icon sprites to be generated.
110
+ *
111
+ * - The keys of the dictionary are the module names of the PNG sprites (with
112
+ * '.png' extension). The generated sprite PNG file can be imported from
113
+ * `'virtual:png/[key]'`.
114
+ * - 'factor' is the scaling factor (a multiplier for 'cssIconSize'). All
115
+ * source PNG files must have the effective pixel size (`cssIconSize *
116
+ * factor`).
117
+ * - 'src' specifies the pattern used to build the path of the source PNG
118
+ * files. MUST contain the placeholder '[path]' that will be replaced with
119
+ * the base paths contained in the icon mapping file referred in the option
120
+ * 'mappingFile'.
121
+ */
122
+ sprites: Dict<{
123
+ factor: number;
124
+ src: string;
125
+ }>;
126
+ }
127
+ //#endregion
128
+ //#region src/index.d.ts
129
+ /**
130
+ * Configuration options for generating icon sprites from SVG or PNG source
131
+ * images.
132
+ */
133
+ type IconSpritePluginOptions = SvgSpritePluginOptions | PngSpritePluginOptions;
134
+ /**
135
+ * A plugin for Vite to generate an icon sprite file from multiple SVG or PNG
136
+ * source files.
137
+ *
138
+ * @param options
139
+ * Plugin configuration options.
140
+ *
141
+ * @returns
142
+ * The plugin instance.
143
+ */
144
+ declare function iconSpritePlugin(options: IconSpritePluginOptions): Plugin;
145
+ //#endregion
146
+ export { IconSpritePluginOptions, iconSpritePlugin as default };
147
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/helper.ts","../src/plugin-svg.ts","../src/plugin-png.ts","../src/index.ts"],"mappings":";;;;;;;;;AAcA;;;UAAiB,uBAAA;EAAwB;;;EAKvC,MAAA,EAAQ,OAAA;EAWR;;;EANA,UAAA;;;ACRF;;EDcE,WAAA;AAAA;;;;;;UCde,sBAAA,SAA+B,uBAAA;EDFR;;;;;ECStC,UAAA;EDCA;;;;ECKA,QAAA;AAAA;;;;;ADfF;UECiB,sBAAA,SAA+B,uBAAA;EFDR;;;;EEOtC,OAAA;EFGA;;;EEEA,WAAA;;;;ADVF;;;ECkBE,cAAA;EDlB8C;;;;;;EC0B9C,eAAA;;AA3BF;;;;;EAmCE,cAAA;EAxBA;;;;;;;;;;;;;EAuCA,eAAA;;ACrDF;;;;;AAAqF;;;;;EDkEnF,cAAA;ECpD0E;;;;;;;;;;;;;;EDoE1E,OAAA,EAAS,IAAA;IAAO,MAAA;IAAgB,GAAA;EAAA;AAAA;;;;;AFhFlC;;KGFY,uBAAA,GAA0B,sBAAA,GAAyB,sBAAA;;;;;;;;;;;iBAcvC,gBAAA,CAAiB,OAAA,EAAS,uBAAA,GAA0B,MAAA"}