vite-plugin-svg-spritegen 1.1.0 → 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/dist/index.js CHANGED
@@ -134,22 +134,28 @@ const svgoDefault = {
134
134
  }
135
135
  }
136
136
  },
137
- "removeDimensions"
137
+ "removeDimensions",
138
+ {
139
+ name: "removeAttrs",
140
+ params: { attrs: "(stroke-width|stroke-linecap|stroke-linejoin|class)" }
141
+ }
138
142
  ]
139
143
  };
144
+ const defaultMatchPattern = /:\s*(?:"|'|`)(?<icon>.+?)(?:"|'|`)/g;
140
145
  const inputConfigDefaults = {
141
146
  pattern: "**/*.svg",
142
147
  baseDir: "./",
143
148
  prefix: "",
144
- removeAttrs: ["stroke-width", "stroke-linecap", "stroke-linejoin", "class"],
145
- svgoPlugins: [],
149
+ removeAttrs: [],
146
150
  svgo: svgoDefault,
151
+ svgoPlugins: [],
147
152
  getSymbolId: (config) => config.prefix + config.matchPath.replace(/\.[^/.]+$/, "").replaceAll("/", "-").toLowerCase()
148
153
  };
149
154
  const stripUnusedDefaults = {
150
155
  enabled: true,
151
156
  srcInclude: "**/*.[jt]sx",
152
- srcExclude: []
157
+ srcExclude: [],
158
+ whitelist: []
153
159
  };
154
160
  function svgSpritegen(config) {
155
161
  const inputConfigs = Array.isArray(config.input) ? config.input : [config.input ?? inputConfigDefaults];
@@ -210,12 +216,14 @@ function svgSpritegen(config) {
210
216
  watcher = chokidar.watch(inputPaths, { ignoreInitial: true }).on("add", onWatch).on("change", onWatch).on("unlink", onWatch);
211
217
  },
212
218
  moduleParsed(info) {
213
- var _a;
214
219
  if (!isBuild || !stripUnusedResolved.enabled || !srcFilter(info.id) || !info.code) return;
215
- const matches = info.code.matchAll(/(?:(?:name)|(?:iconName)|(?:icon)): "(?<icon>.+?)"/g);
216
- for (const match of matches) {
217
- const icon = (_a = match.groups) == null ? void 0 : _a.icon;
218
- if (!icon || referencedSvgFiles.has(icon)) continue;
220
+ const matchPattern = config.matchPattern ? new RegExp(config.matchPattern) : defaultMatchPattern;
221
+ const icons = [...info.code.matchAll(matchPattern)].flatMap(
222
+ ({ groups }) => (groups == null ? void 0 : groups.icon) ?? []
223
+ );
224
+ icons.push(...stripUnusedResolved.whitelist);
225
+ for (const icon of icons) {
226
+ if (!referencedSvgFiles.has(icon)) continue;
219
227
  const svgPath = allSvgFiles.get(icon);
220
228
  if (!svgPath) continue;
221
229
  referencedSvgFiles.set(icon, svgPath);
@@ -225,7 +233,7 @@ function svgSpritegen(config) {
225
233
  if (stripUnusedResolved.enabled) {
226
234
  const spriteContent = await writeSprite(spriteFilePath, referencedSvgFiles);
227
235
  Object.values(bundle).forEach((file) => {
228
- if (file.type === "asset" && file.name === spriteNameResolved) {
236
+ if (file.type === "asset" && file.names.includes(spriteNameResolved)) {
229
237
  file.source = spriteContent;
230
238
  }
231
239
  });
package/dist/index.mjs CHANGED
@@ -132,22 +132,28 @@ const svgoDefault = {
132
132
  }
133
133
  }
134
134
  },
135
- "removeDimensions"
135
+ "removeDimensions",
136
+ {
137
+ name: "removeAttrs",
138
+ params: { attrs: "(stroke-width|stroke-linecap|stroke-linejoin|class)" }
139
+ }
136
140
  ]
137
141
  };
142
+ const defaultMatchPattern = /:\s*(?:"|'|`)(?<icon>.+?)(?:"|'|`)/g;
138
143
  const inputConfigDefaults = {
139
144
  pattern: "**/*.svg",
140
145
  baseDir: "./",
141
146
  prefix: "",
142
- removeAttrs: ["stroke-width", "stroke-linecap", "stroke-linejoin", "class"],
143
- svgoPlugins: [],
147
+ removeAttrs: [],
144
148
  svgo: svgoDefault,
149
+ svgoPlugins: [],
145
150
  getSymbolId: (config) => config.prefix + config.matchPath.replace(/\.[^/.]+$/, "").replaceAll("/", "-").toLowerCase()
146
151
  };
147
152
  const stripUnusedDefaults = {
148
153
  enabled: true,
149
154
  srcInclude: "**/*.[jt]sx",
150
- srcExclude: []
155
+ srcExclude: [],
156
+ whitelist: []
151
157
  };
152
158
  function svgSpritegen(config) {
153
159
  const inputConfigs = Array.isArray(config.input) ? config.input : [config.input ?? inputConfigDefaults];
@@ -208,12 +214,14 @@ function svgSpritegen(config) {
208
214
  watcher = watch(inputPaths, { ignoreInitial: true }).on("add", onWatch).on("change", onWatch).on("unlink", onWatch);
209
215
  },
210
216
  moduleParsed(info) {
211
- var _a;
212
217
  if (!isBuild || !stripUnusedResolved.enabled || !srcFilter(info.id) || !info.code) return;
213
- const matches = info.code.matchAll(/(?:(?:name)|(?:iconName)|(?:icon)): "(?<icon>.+?)"/g);
214
- for (const match of matches) {
215
- const icon = (_a = match.groups) == null ? void 0 : _a.icon;
216
- if (!icon || referencedSvgFiles.has(icon)) continue;
218
+ const matchPattern = config.matchPattern ? new RegExp(config.matchPattern) : defaultMatchPattern;
219
+ const icons = [...info.code.matchAll(matchPattern)].flatMap(
220
+ ({ groups }) => (groups == null ? void 0 : groups.icon) ?? []
221
+ );
222
+ icons.push(...stripUnusedResolved.whitelist);
223
+ for (const icon of icons) {
224
+ if (!referencedSvgFiles.has(icon)) continue;
217
225
  const svgPath = allSvgFiles.get(icon);
218
226
  if (!svgPath) continue;
219
227
  referencedSvgFiles.set(icon, svgPath);
@@ -223,7 +231,7 @@ function svgSpritegen(config) {
223
231
  if (stripUnusedResolved.enabled) {
224
232
  const spriteContent = await writeSprite(spriteFilePath, referencedSvgFiles);
225
233
  Object.values(bundle).forEach((file) => {
226
- if (file.type === "asset" && file.name === spriteNameResolved) {
234
+ if (file.type === "asset" && file.names.includes(spriteNameResolved)) {
227
235
  file.source = spriteContent;
228
236
  }
229
237
  });
package/dist/types.d.ts CHANGED
@@ -19,6 +19,7 @@ export interface StripUnusedConfig {
19
19
  enabled?: boolean;
20
20
  srcInclude?: FilterPattern;
21
21
  srcExclude?: FilterPattern;
22
+ whitelist?: string[];
22
23
  }
23
24
  export interface PluginConfig {
24
25
  outputDir: string;
@@ -27,4 +28,5 @@ export interface PluginConfig {
27
28
  typesFileName?: string;
28
29
  spriteFileName?: string;
29
30
  gitignore?: boolean;
31
+ matchPattern?: string | RegExp;
30
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svg-spritegen",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Vite plugin that generates an svg spritesheet, optionally removing unused svg files.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",