@open-xchange/vite-plugin-icon-sprite 1.1.1 → 1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## `1.2.0` – 2026-Mar-13
4
+
5
+ - added: support for Vite 8
6
+
3
7
  ## `1.1.1` – 2025-Sep-09
4
8
 
5
9
  - 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`.
package/dist/helper.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { type PluginHelperConfig, PluginHelper } from "@open-xchange/vite-helper";
1
+ import { type PluginHelperConfig, PluginHelper } from '@open-xchange/vite-helper';
2
2
  /**
3
3
  * Common configuration options (independent from the icon file format) for the
4
- * plugin "@open-xchange/vite-plugin-icon-sprite".
4
+ * plugin '@open-xchange/vite-plugin-icon-sprite'.
5
5
  *
6
6
  * @template FormatT
7
7
  * The file format specifier of the icon sprite, and the source images.
@@ -16,15 +16,15 @@ export interface SpritePluginBaseOptions<FormatT extends string> {
16
16
  */
17
17
  imagesPath: string;
18
18
  /**
19
- * Path to the JSON or YAML configuration file containing the mapping
20
- * between icon identifiers and SVG source file names.
19
+ * Path to the JSON or YAML configuration file containing the mapping between
20
+ * icon identifiers and SVG source file names.
21
21
  */
22
22
  mappingPath: string;
23
23
  }
24
24
  /**
25
25
  * Internal configuration of a `SpritePluginHelper` instance.
26
26
  */
27
- export type SpritePluginConfig = Required<Pick<PluginHelperConfig, "pluginIndex" | "virtualModules">>;
27
+ export type SpritePluginConfig = Required<Pick<PluginHelperConfig, 'pluginIndex' | 'virtualModules'>>;
28
28
  /**
29
29
  * Represents a single entry in an icon mapping configuration file (a mapping
30
30
  * between icon identifier, path to source image, and language code for
package/dist/helper.js CHANGED
@@ -1,6 +1,6 @@
1
- import { dictEntries } from "@open-xchange/vite-helper/utils";
2
- import { resolvePath } from "@open-xchange/vite-helper/file";
3
- import { PluginHelper } from "@open-xchange/vite-helper";
1
+ import { dictEntries } from '@open-xchange/vite-helper/utils';
2
+ import { resolvePath } from '@open-xchange/vite-helper/file';
3
+ import { PluginHelper } from '@open-xchange/vite-helper';
4
4
  // class SpritePluginHelper ===================================================
5
5
  /**
6
6
  * Plugin helper for all icon file formats.
@@ -17,8 +17,8 @@ export class SpritePluginHelper extends PluginHelper {
17
17
  ...config,
18
18
  virtualPrefix: options.format,
19
19
  loggerPrefix: options.format,
20
- logLevelEnvVar: "PLUGIN_ICON_SPRITE_LOGLEVEL",
21
- cacheSrcFiles: [options.imagesPath + "/**/*." + options.format, options.mappingPath],
20
+ logLevelEnvVar: 'PLUGIN_ICON_SPRITE_LOGLEVEL',
21
+ cacheSrcFiles: [options.imagesPath + '/**/*.' + options.format, options.mappingPath],
22
22
  });
23
23
  this.options = options;
24
24
  }
@@ -34,15 +34,15 @@ export class SpritePluginHelper extends PluginHelper {
34
34
  * The entries of the configuration file.
35
35
  */
36
36
  async readIconMapping() {
37
- const schemaPath = resolvePath("./mapping-schema.json", import.meta.url);
37
+ const schemaPath = resolvePath('./mapping-schema.json', import.meta.url);
38
38
  const mappingDict = await this.readConfig(this.options.mappingPath, { schema: schemaPath });
39
39
  // convert to array of `IconMappingEntry`
40
40
  const iconMapping = [];
41
41
  for (const [iconId, entry] of dictEntries(mappingDict)) {
42
- const iconDict = (typeof entry === "string") ? { "*": entry } : entry;
42
+ const iconDict = (typeof entry === 'string') ? { '*': entry } : entry;
43
43
  for (const [languages, iconPath] of dictEntries(iconDict)) {
44
- for (const iconLang of languages.split(",")) {
45
- if (iconLang === "*") {
44
+ for (const iconLang of languages.split(',')) {
45
+ if (iconLang === '*') {
46
46
  iconMapping.push({ iconId, iconPath });
47
47
  }
48
48
  else {
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { Plugin } from "vite";
2
- import { type SvgSpritePluginOptions } from "./plugin-svg.js";
3
- import { type PngSpritePluginOptions } from "./plugin-png.js";
1
+ import type { Plugin } from 'vite';
2
+ import { type SvgSpritePluginOptions } from './plugin-svg.js';
3
+ import { type PngSpritePluginOptions } from './plugin-png.js';
4
4
  /**
5
5
  * Configuration options for generating icon sprites from SVG or PNG source
6
6
  * images.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { svgSpritePlugin } from "./plugin-svg.js";
2
- import { pngSpritePlugin } from "./plugin-png.js";
1
+ import { svgSpritePlugin } from './plugin-svg.js';
2
+ import { pngSpritePlugin } from './plugin-png.js';
3
3
  // plugin =====================================================================
4
4
  /**
5
5
  * A plugin for Vite to generate an icon sprite file from multiple SVG or PNG
@@ -13,7 +13,7 @@ import { pngSpritePlugin } from "./plugin-png.js";
13
13
  */
14
14
  export default function iconSpritePlugin(options) {
15
15
  switch (options.format) {
16
- case "svg": return svgSpritePlugin(import.meta.url, options);
17
- case "png": return pngSpritePlugin(import.meta.url, options);
16
+ case 'svg': return svgSpritePlugin(import.meta.url, options);
17
+ case 'png': return pngSpritePlugin(import.meta.url, options);
18
18
  }
19
19
  }
@@ -1,32 +1,32 @@
1
1
  {
2
- "$schema": "http://json-schema.org/draft-07/schema",
3
- "title": "Icon mapping for SVG or PNG icon sprites",
4
- "description": "A dictionary that maps icon identifiers to source SVG or PNG image files.",
5
- "type": "object",
6
- "properties": {
7
- "$schema": {
8
- "type": "string",
9
- "format": "uri-reference"
2
+ "$schema": "http://json-schema.org/draft-07/schema",
3
+ "title": "Icon mapping for SVG or PNG icon sprites",
4
+ "description": "A dictionary that maps icon identifiers to source SVG or PNG image files.",
5
+ "type": "object",
6
+ "properties": {
7
+ "$schema": {
8
+ "type": "string",
9
+ "format": "uri-reference"
10
+ }
11
+ },
12
+ "patternProperties": {
13
+ "^[a-z0-9]+(-[a-z0-9]+)*$": {
14
+ "anyOf": [
15
+ {
16
+ "type": "string"
17
+ },
18
+ {
19
+ "type": "object",
20
+ "patternProperties": {
21
+ "^(\\*|[a-z]+(,[a-z]+)*)$": {
22
+ "type": "string"
23
+ }
24
+ },
25
+ "required": ["*"],
26
+ "additionalProperties": false
10
27
  }
11
- },
12
- "patternProperties": {
13
- "^[a-z0-9]+(-[a-z0-9]+)*$": {
14
- "anyOf": [
15
- {
16
- "type": "string"
17
- },
18
- {
19
- "type": "object",
20
- "patternProperties": {
21
- "^(\\*|[a-z]+(,[a-z]+)*)$": {
22
- "type": "string"
23
- }
24
- },
25
- "required": ["*"],
26
- "additionalProperties": false
27
- }
28
- ]
29
- }
30
- },
31
- "additionalProperties": false
28
+ ]
29
+ }
30
+ },
31
+ "additionalProperties": false
32
32
  }
@@ -1,13 +1,13 @@
1
- import type { Plugin } from "vite";
2
- import { type Dict } from "@open-xchange/vite-helper/utils";
3
- import { type SpritePluginBaseOptions } from "./helper.js";
1
+ import type { Plugin } from 'vite';
2
+ import { type Dict } from '@open-xchange/vite-helper/utils';
3
+ import { type SpritePluginBaseOptions } from './helper.js';
4
4
  /**
5
5
  * Configuration options for generating PNG sprites from PNG source images.
6
6
  */
7
- export interface PngSpritePluginOptions extends SpritePluginBaseOptions<"png"> {
7
+ export interface PngSpritePluginOptions extends SpritePluginBaseOptions<'png'> {
8
8
  /**
9
- * Module name for the CSS file (with ".css" extension). The generated CSS
10
- * markup can be imported from `"virtual:png/[cssName]"`.
9
+ * Module name for the CSS file (with '.css' extension). The generated CSS
10
+ * markup can be imported from `'virtual:png/[cssName]'`.
11
11
  */
12
12
  cssName: string;
13
13
  /**
@@ -15,8 +15,8 @@ export interface PngSpritePluginOptions extends SpritePluginBaseOptions<"png"> {
15
15
  */
16
16
  cssIconSize: number;
17
17
  /**
18
- * Additional padding around all icons to be generated in the sprites, in
19
- * CSS pixels.
18
+ * Additional padding around all icons to be generated in the sprites, in CSS
19
+ * pixels.
20
20
  *
21
21
  * @default 0
22
22
  */
@@ -25,56 +25,55 @@ export interface PngSpritePluginOptions extends SpritePluginBaseOptions<"png"> {
25
25
  * The CSS selector for a PNG icon element to be used in all generated CSS
26
26
  * rules.
27
27
  *
28
- * @default "i.png-icon"
28
+ * @default 'i.png-icon'
29
29
  */
30
30
  cssIconSelector?: string;
31
31
  /**
32
32
  * Name of the root element's attribute containing the locale identifier.
33
33
  * Needed to generate CSS selectors for localized icons.
34
34
  *
35
- * @default "lang"
35
+ * @default 'lang'
36
36
  */
37
37
  rootLocaleAttr?: string;
38
38
  /**
39
39
  * Specifies how to generate the sprite PNG files.
40
40
  *
41
- * - `"source"`: The source PNGs will be copied into the generated sprites
42
- * unmodified. They will contain three color channels and an alpha
43
- * channel.
44
- * - `"monochrome"`: The generated sprites will be converted to monochrome.
41
+ * - `'source'`: The source PNGs will be copied into the generated sprites
42
+ * unmodified. They will contain three color channels and an alpha channel.
43
+ * - `'monochrome'`: The generated sprites will be converted to monochrome.
45
44
  * They will contain a gray channel and an alpha channel.
46
- * - `"alpha"`: Only the alpha channels of the source PNGs will be copied
45
+ * - `'alpha'`: Only the alpha channels of the source PNGs will be copied
47
46
  * into the generated sprites. They will contain a single gray channel
48
47
  * representing the original alpha channels.
49
48
  *
50
- * @default "source"
49
+ * @default 'source'
51
50
  */
52
- spriteColorType?: "source" | "monochrome" | "alpha";
51
+ spriteColorType?: 'source' | 'monochrome' | 'alpha';
53
52
  /**
54
53
  * Specifies how the sprites are supposed to be used in CSS rules.
55
54
  *
56
- * - `"background"`: The sprites will be attached via "background-image".
57
- * - `"mask"`: The sprites will be attached via "mask-image".
55
+ * - `'background'`: The sprites will be attached via 'background-image'.
56
+ * - `'mask'`: The sprites will be attached via 'mask-image'.
58
57
  *
59
- * All related CSS properties (e.g. "(background|mask)-position" etc.) will
58
+ * All related CSS properties (e.g. '(background|mask)-position' etc.) will
60
59
  * be generated accordingly.
61
60
  *
62
- * @default "background"
61
+ * @default 'background'
63
62
  */
64
- spriteFillType?: "background" | "mask";
63
+ spriteFillType?: 'background' | 'mask';
65
64
  /**
66
65
  * List of all icon sprites to be generated.
67
66
  *
68
- * - The keys of the dictionary are the module names of the PNG sprites
69
- * (with ".png" extension). The generated sprite PNG file can be imported
70
- * from `"virtual:png/[key]"`.
71
- * - "factor" is the scaling factor (a multiplier for "cssIconSize"). All
67
+ * - The keys of the dictionary are the module names of the PNG sprites (with
68
+ * '.png' extension). The generated sprite PNG file can be imported from
69
+ * `'virtual:png/[key]'`.
70
+ * - 'factor' is the scaling factor (a multiplier for 'cssIconSize'). All
72
71
  * source PNG files must have the effective pixel size (`cssIconSize *
73
72
  * factor`).
74
- * - "src" specifies the pattern used to build the path of the source PNG
75
- * files. MUST contain the placeholder "[path]" that will be replaced
76
- * with the base paths contained in the icon mapping file referred in the
77
- * option "mappingFile".
73
+ * - 'src' specifies the pattern used to build the path of the source PNG
74
+ * files. MUST contain the placeholder '[path]' that will be replaced with
75
+ * the base paths contained in the icon mapping file referred in the option
76
+ * 'mappingFile'.
78
77
  */
79
78
  sprites: Dict<{
80
79
  factor: number;
@@ -86,7 +85,7 @@ export interface PngSpritePluginOptions extends SpritePluginBaseOptions<"png"> {
86
85
  * files.
87
86
  *
88
87
  * @param index
89
- * URL of the index source file of this plugin.
88
+ * URL of the index source file of this plugin (`import.meta.url`).
90
89
  *
91
90
  * @param options
92
91
  * Plugin configuration options.
@@ -1,11 +1,11 @@
1
- import { Jimp, PNGColorType } from "jimp";
2
- import { onceFn } from "@open-xchange/vite-helper/utils";
3
- import { Cache } from "@open-xchange/vite-helper/cache";
4
- import { SpritePluginHelper } from "./helper.js";
1
+ import { Jimp, PNGColorType } from 'jimp';
2
+ import { onceFn } from '@open-xchange/vite-helper/utils';
3
+ import { Cache } from '@open-xchange/vite-helper/cache';
4
+ import { SpritePluginHelper } from './helper.js';
5
5
  // constants ==================================================================
6
6
  const FILL_VENDOR_PREFIXES = {
7
7
  background: [],
8
- mask: ["webkit"],
8
+ mask: ['webkit'],
9
9
  };
10
10
  // functions ==================================================================
11
11
  /**
@@ -15,12 +15,15 @@ const FILL_VENDOR_PREFIXES = {
15
15
  * The length to be emitted.
16
16
  *
17
17
  * @returns
18
- * The passed length with "px" unit.
18
+ * The passed length with 'px' unit.
19
19
  */
20
20
  function px(value) {
21
- return value ? `${value}px` : "0";
21
+ return value ? `${value}px` : '0';
22
22
  }
23
23
  // class PngSpritePluginHelper ================================================
24
+ /**
25
+ * Implementation helper for PNG to sprite conversion.
26
+ */
24
27
  class PngSpritePluginHelper extends SpritePluginHelper {
25
28
  #parseMappingOnce;
26
29
  // constructor ------------------------------------------------------------
@@ -30,10 +33,10 @@ class PngSpritePluginHelper extends SpritePluginHelper {
30
33
  virtualModules: [options.cssName, ...Object.keys(options.sprites)],
31
34
  }, {
32
35
  cssIconPadding: 0,
33
- cssIconSelector: "i.png-icon",
34
- rootLocaleAttr: "lang",
35
- spriteColorType: "source",
36
- spriteFillType: "background",
36
+ cssIconSelector: 'i.png-icon',
37
+ rootLocaleAttr: 'lang',
38
+ spriteColorType: 'source',
39
+ spriteFillType: 'background',
37
40
  ...options,
38
41
  });
39
42
  this.#parseMappingOnce = onceFn(() => this.#parseMapping());
@@ -50,10 +53,10 @@ class PngSpritePluginHelper extends SpritePluginHelper {
50
53
  const { cssName } = this.options;
51
54
  // try to resolve cached version of generated file
52
55
  return await this.generateModule(cssName, async () => {
53
- this.info("generating CSS markup %f", cssName);
56
+ this.info('generating CSS markup %f', cssName);
54
57
  // parse mapping file which collects icon paths, CSS selectors, and entry positions
55
58
  const cssMarkup = (await this.#parseMappingOnce()).cssMarkup;
56
- this.info("CSS markup %f generated successfully", cssName);
59
+ this.info('CSS markup %f generated successfully', cssName);
57
60
  return cssMarkup;
58
61
  });
59
62
  }
@@ -77,11 +80,11 @@ class PngSpritePluginHelper extends SpritePluginHelper {
77
80
  // shortcuts to plugin options
78
81
  const { imagesPath, cssIconSize, cssIconPadding, spriteColorType } = this.options;
79
82
  // check configuration
80
- this.ensure(factor > 0, "invalid scaling factor in configuration for sprite %f", spriteName);
81
- this.ensure(srcPattern.includes("[path]"), "placeholder [path] expected in configuration for sprite %f", spriteName);
83
+ this.ensure(factor > 0, 'invalid scaling factor in configuration for sprite %f', spriteName);
84
+ this.ensure(srcPattern.includes('[path]'), 'placeholder [path] expected in configuration for sprite %f', spriteName);
82
85
  // try to resolve cached version of generated file
83
86
  return await this.generateModule(spriteName, async () => {
84
- this.info("generating sprite %f", spriteName);
87
+ this.info('generating sprite %f', spriteName);
85
88
  // parse mapping file which collects icon paths, CSS selectors, and entry positions
86
89
  const { cssSpriteWidth, cssSpriteHeight, entries } = await this.#parseMappingOnce();
87
90
  // new image data is not clean out-of-the-box, explicitly fill with zeros
@@ -91,17 +94,17 @@ class PngSpritePluginHelper extends SpritePluginHelper {
91
94
  // expected pixel size for the current scaling factor
92
95
  const size = cssIconSize * factor;
93
96
  // load and parse the source image file
94
- const path = imagesPath + "/" + srcPattern.replace("[path]", iconPath);
97
+ const path = imagesPath + '/' + srcPattern.replace('[path]', iconPath);
95
98
  const buffer = await this.readBuffer(path);
96
99
  const image = await Jimp.fromBuffer(buffer);
97
100
  // validate the image size
98
101
  const { width, height } = image.bitmap;
99
- this.ensure((width === size) && (height === size), "wrong image width in %f (expected %s but got %s)", path, `${size}x${size}`, `${width}x${height}`);
102
+ this.ensure((width === size) && (height === size), 'wrong image width in %f (expected %s but got %s)', path, `${size}x${size}`, `${width}x${height}`);
100
103
  // insert source image into the sprite
101
104
  sprite.blit({ src: image, x: (x + cssIconPadding) * factor, y: (y + cssIconPadding) * factor });
102
105
  }
103
106
  // copy alpha of all pixels to RGB channels (alpha channel will be exported as greyscale without alpha)
104
- if (spriteColorType === "alpha") {
107
+ if (spriteColorType === 'alpha') {
105
108
  for (let d = sprite.bitmap.data, i = 0, l = d.length; i < l; i += 4) {
106
109
  d.fill(d[i + 3], i, i + 3); // copy A to RGB
107
110
  d[i + 3] = 255; // set A to full opacity
@@ -113,11 +116,11 @@ class PngSpritePluginHelper extends SpritePluginHelper {
113
116
  monochrome: PNGColorType.GRAYSCALE_ALPHA,
114
117
  alpha: PNGColorType.GRAYSCALE,
115
118
  }[spriteColorType];
116
- const spriteBuffer = await sprite.getBuffer("image/png", { colorType });
119
+ const spriteBuffer = await sprite.getBuffer('image/png', { colorType });
117
120
  // convert to base64 encoded data URL
118
- const spriteDataUrl = `data:image/png;base64,${spriteBuffer.toString("base64")}`;
121
+ const spriteDataUrl = `data:image/png;base64,${spriteBuffer.toString('base64')}`;
119
122
  const moduleSource = `export default ${JSON.stringify(spriteDataUrl)};`;
120
- this.info("sprite %f generated successfully", spriteName);
123
+ this.info('sprite %f generated successfully', spriteName);
121
124
  return moduleSource;
122
125
  });
123
126
  }
@@ -154,32 +157,32 @@ class PngSpritePluginHelper extends SpritePluginHelper {
154
157
  // generates a background/mask CSS property definition with correct vendor prefixes
155
158
  const fillProp = (name, value) => {
156
159
  const prop = `${spriteFillType}-${name}: ${value};`;
157
- return prefixes.map(prefix => `-${prefix}-${prop} `).join("") + prop;
160
+ return prefixes.map(prefix => `-${prefix}-${prop} `).join('') + prop;
158
161
  };
159
162
  // the contents of the resulting CSS file with icon definitions
160
163
  const cssLines = [
161
164
  `${cssIconSelector} {`,
162
- " display: inline-block;",
165
+ ' display: inline-block;',
163
166
  ` width: ${px(cssTileSize)};`,
164
167
  ` height: ${px(cssTileSize)};`,
165
- " flex: 0 0 auto;",
166
- ` ${fillProp("size", `${px(cssSpriteWidth)} ${px(cssSpriteHeight)}`)}`,
167
- ` ${fillProp("origin", "content-box")}`,
168
- "}",
168
+ ' flex: 0 0 auto;',
169
+ ` ${fillProp('size', `${px(cssSpriteWidth)} ${px(cssSpriteHeight)}`)}`,
170
+ ` ${fillProp('origin', 'content-box')}`,
171
+ '}',
169
172
  `${cssIconSelector}[data-icon-id="none"] { visibility: hidden; }`,
170
173
  ];
171
174
  // generate all sprite entries as an array
172
175
  const entries = Array.from(selectorMap, ([iconPath, selectors], index) => {
173
176
  const x = cssTileSize * (index % rowLength);
174
177
  const y = cssTileSize * Math.floor(index / rowLength);
175
- const prop = fillProp("position", `${px(-x)} ${px(-y)}`);
178
+ const prop = fillProp('position', `${px(-x)} ${px(-y)}`);
176
179
  for (const selector of selectors) {
177
180
  cssLines.push(`${selector} { ${prop} }`);
178
181
  }
179
182
  return { iconPath, x, y };
180
183
  });
181
184
  // create the resulting CSS markup as string
182
- const cssMarkup = cssLines.join("\n") + "\n";
185
+ const cssMarkup = cssLines.join('\n') + '\n';
183
186
  return { cssMarkup, cssSpriteWidth, cssSpriteHeight, entries };
184
187
  }
185
188
  }
@@ -189,7 +192,7 @@ class PngSpritePluginHelper extends SpritePluginHelper {
189
192
  * files.
190
193
  *
191
194
  * @param index
192
- * URL of the index source file of this plugin.
195
+ * URL of the index source file of this plugin (`import.meta.url`).
193
196
  *
194
197
  * @param options
195
198
  * Plugin configuration options.
@@ -204,7 +207,7 @@ export function pngSpritePlugin(index, options) {
204
207
  const helper = new PngSpritePluginHelper(index, options);
205
208
  // create and return the plugin object
206
209
  return {
207
- name: "@open-xchange/vite-plugin-icon-sprite/png",
210
+ name: '@open-xchange/vite-plugin-icon-sprite/png',
208
211
  // initialize file system cache for generated modules
209
212
  async configResolved(viteConfig) {
210
213
  await helper.initializeCache(viteConfig);
@@ -215,7 +218,7 @@ export function pngSpritePlugin(index, options) {
215
218
  },
216
219
  // load the PNG icon files, generate final PNG sprites and CSS file
217
220
  async load(moduleId) {
218
- // only handle the target modules specified in "resolveId"
221
+ // only handle the target modules specified in 'resolveId'
219
222
  const target = helper.matchVirtualModuleId(moduleId);
220
223
  if (!target) {
221
224
  return;
@@ -230,7 +233,7 @@ export function pngSpritePlugin(index, options) {
230
233
  return await helper.generateSpriteModule(target, factor, src);
231
234
  }
232
235
  // invalid module identifier
233
- helper.fail("unknown output file %f", target);
236
+ helper.fail('unknown output file %f', target);
234
237
  return undefined;
235
238
  },
236
239
  };
@@ -1,18 +1,18 @@
1
- import type { Plugin } from "vite";
2
- import { type SpritePluginBaseOptions } from "./helper.js";
1
+ import type { Plugin } from 'vite';
2
+ import { type SpritePluginBaseOptions } from './helper.js';
3
3
  /**
4
4
  * Configuration options for generating an SVG sprite from SVG source images.
5
5
  */
6
- export interface SvgSpritePluginOptions extends SpritePluginBaseOptions<"svg"> {
6
+ export interface SvgSpritePluginOptions extends SpritePluginBaseOptions<'svg'> {
7
7
  /**
8
- * Module name of the SVG sprite to be generated (with ".svg" extension).
9
- * The SVG markup of the sprite can be imported from the virtual module
10
- * path `"virtual:svg/[spriteName]"`.
8
+ * Module name of the SVG sprite to be generated (with '.svg' extension). The
9
+ * SVG markup of the sprite can be imported from the virtual module path
10
+ * `'virtual:svg/[spriteName]'`.
11
11
  */
12
12
  spriteName: string;
13
13
  /**
14
- * A prefix to be added to all icon identifiers declared in the mapping
15
- * file. By default, no prefix will be added.
14
+ * A prefix to be added to all icon identifiers declared in the mapping file.
15
+ * By default, no prefix will be added.
16
16
  */
17
17
  idPrefix?: string;
18
18
  }
@@ -21,7 +21,7 @@ export interface SvgSpritePluginOptions extends SpritePluginBaseOptions<"svg"> {
21
21
  * files.
22
22
  *
23
23
  * @param index
24
- * URL of the index source file of this plugin.
24
+ * URL of the index source file of this plugin (`import.meta.url`).
25
25
  *
26
26
  * @param options
27
27
  * Plugin configuration options.
@@ -1,14 +1,17 @@
1
- import { basename } from "node:path";
2
- import SVGSprite from "svg-sprite";
3
- import { SpritePluginHelper } from "./helper.js";
1
+ import { basename } from 'node:path';
2
+ import SVGSprite from 'svg-sprite';
3
+ import { SpritePluginHelper } from './helper.js';
4
4
  // class SvgSpritePluginHelper ================================================
5
+ /**
6
+ * Implementation helper for SVG to sprite conversion.
7
+ */
5
8
  class SvgSpritePluginHelper extends SpritePluginHelper {
6
9
  constructor(index, options) {
7
10
  super({
8
11
  pluginIndex: index,
9
12
  virtualModules: options.spriteName,
10
13
  }, {
11
- idPrefix: "",
14
+ idPrefix: '',
12
15
  ...options,
13
16
  });
14
17
  }
@@ -24,17 +27,17 @@ class SvgSpritePluginHelper extends SpritePluginHelper {
24
27
  const { imagesPath, idPrefix, spriteName } = this.options;
25
28
  // try to resolve cached version of generated file
26
29
  return await this.generateModule(spriteName, async () => {
27
- this.info("compiling sprite %f", spriteName);
30
+ this.info('compiling sprite %f', spriteName);
28
31
  // create a reverse map from icon filenames to icon identifiers
29
32
  const iconIdMap = new Map();
30
33
  // create the core SVG spriter
31
34
  const spriter = new SVGSprite({
32
35
  shape: {
33
36
  id: {
34
- generator: name => iconIdMap.get(name) ?? "",
37
+ generator: name => iconIdMap.get(name) ?? '',
35
38
  },
36
39
  spacing: {
37
- box: "icon",
40
+ box: 'icon',
38
41
  },
39
42
  dimension: {
40
43
  maxWidth: 16,
@@ -45,10 +48,10 @@ class SvgSpritePluginHelper extends SpritePluginHelper {
45
48
  xmlDeclaration: false,
46
49
  // - remove all <style> elements declaring a class with fill color
47
50
  // - remove all class attribute referring to the class from <style>
48
- // - add "fill" attribute to all <symbol> elements
51
+ // - add 'fill' attribute to all <symbol> elements
49
52
  transform: markup => markup
50
- .replace(/<style>.*?<\/style>/g, "")
51
- .replace(/class=".*?" ?/g, "")
53
+ .replace(/<style>.*?<\/style>/g, '')
54
+ .replace(/class=".*?" ?/g, '')
52
55
  .replace(/<symbol /g, '<symbol fill="currentColor" '),
53
56
  },
54
57
  mode: {
@@ -63,7 +66,7 @@ class SvgSpritePluginHelper extends SpritePluginHelper {
63
66
  for (const { iconId, iconPath, iconLang } of await this.readIconMapping()) {
64
67
  const path = `${imagesPath}/${iconPath}.svg`;
65
68
  // icon identifier with configured prefix and language suffix
66
- const idSuffix = iconLang ? `;${iconLang}` : "";
69
+ const idSuffix = iconLang ? `;${iconLang}` : '';
67
70
  iconIdMap.set(basename(path), idPrefix + iconId + idSuffix);
68
71
  // read SVG source image, insert into spriter instance
69
72
  const markup = await this.readText(path);
@@ -71,9 +74,9 @@ class SvgSpritePluginHelper extends SpritePluginHelper {
71
74
  }
72
75
  // compile the SVG sprite
73
76
  const result = (await spriter.compileAsync()).result;
74
- const spriteMarkup = result.symbol.sprite.contents.toString("utf8");
77
+ const spriteMarkup = result.symbol.sprite.contents.toString('utf8');
75
78
  const moduleCode = `export default ${JSON.stringify(spriteMarkup)};`;
76
- this.info("sprite %f generated successfully", spriteName);
79
+ this.info('sprite %f generated successfully', spriteName);
77
80
  return moduleCode;
78
81
  });
79
82
  }
@@ -84,7 +87,7 @@ class SvgSpritePluginHelper extends SpritePluginHelper {
84
87
  * files.
85
88
  *
86
89
  * @param index
87
- * URL of the index source file of this plugin.
90
+ * URL of the index source file of this plugin (`import.meta.url`).
88
91
  *
89
92
  * @param options
90
93
  * Plugin configuration options.
@@ -97,7 +100,7 @@ export function svgSpritePlugin(index, options) {
97
100
  const helper = new SvgSpritePluginHelper(index, options);
98
101
  // create and return the plugin object
99
102
  return {
100
- name: "@open-xchange/vite-plugin-icon-sprite/svg",
103
+ name: '@open-xchange/vite-plugin-icon-sprite/svg',
101
104
  // initialize file system cache for generated modules
102
105
  async configResolved(viteConfig) {
103
106
  await helper.initializeCache(viteConfig);
@@ -108,7 +111,7 @@ export function svgSpritePlugin(index, options) {
108
111
  },
109
112
  // load the SVG icon files, generate final SVG sprite
110
113
  async load(moduleId) {
111
- // only handle the output file specified in "resolveId"
114
+ // only handle the output file specified in 'resolveId'
112
115
  if (!helper.matchVirtualModuleId(moduleId)) {
113
116
  return null;
114
117
  }
@@ -1,7 +1,7 @@
1
1
  {
2
- "$schema": "https://json.schemastore.org/tsconfig",
3
- "extends": "../../../tsconfig.shared.json",
4
- "compilerOptions": {
5
- "outDir": "../dist"
6
- }
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "../../../tsconfig.shared.json",
4
+ "compilerOptions": {
5
+ "outDir": "../dist"
6
+ }
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-xchange/vite-plugin-icon-sprite",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Vite plugin that builds icon sprites from SVG or PNG icon files",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,38 +11,30 @@
11
11
  "engines": {
12
12
  "node": ">=20.18"
13
13
  },
14
- "packageManager": "yarn@4.9.4",
15
14
  "type": "module",
16
15
  "exports": "./dist/index.js",
17
16
  "files": [
18
17
  "dist",
19
18
  "CHANGELOG.*"
20
19
  ],
21
- "scripts": {
22
- "prepack": "yarn build && yarn lint",
23
- "clean": "premove dist",
24
- "build": "yarn clean && tsc --project src/tsconfig.json && cpx \"src/*.json\" dist/",
25
- "lint": "tsc && tsc --project src/tsconfig.json --noEmit && eslint ."
26
- },
27
20
  "dependencies": {
28
- "@open-xchange/vite-helper": "^1.1.1",
29
21
  "jimp": "^1.6.0",
30
- "svg-sprite": "^2.0.4"
22
+ "svg-sprite": "^2.0.4",
23
+ "@open-xchange/vite-helper": "^1.2.2"
31
24
  },
32
25
  "devDependencies": {
33
- "@open-xchange/linter-presets": "^1.9.0",
34
- "@types/node": "^24.3.1",
35
26
  "@types/pngjs": "^6.0.5",
36
27
  "@types/svg-sprite": "^0.0.39",
37
28
  "@types/vinyl": "^2.0.12",
38
- "cpx2": "^8.0.0",
39
- "eslint": "^9.35.0",
40
- "jiti": "^2.5.1",
41
- "premove": "^4.0.0",
42
- "typescript": "^5.9.2",
43
- "vite": "^7.1.5"
29
+ "vite": "^8.0.0",
30
+ "@open-xchange/linter-presets": "^1.18.4"
44
31
  },
45
32
  "peerDependencies": {
46
- "vite": "^6.0.0 || ^7.0.0"
33
+ "vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
34
+ },
35
+ "scripts": {
36
+ "build": "pnpm clean && tsc --project src/tsconfig.json && cpx \"src/*.json\" dist/",
37
+ "clean": "premove dist",
38
+ "lint": "tsc && tsc --project src/tsconfig.json --noEmit && eslint ."
47
39
  }
48
40
  }