vite-plugin-svg-spritegen 2.0.7 → 2.0.8

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.
@@ -1,2 +1,7 @@
1
1
  import type { InputConfigWithDefaults, SvgMap } from './types';
2
- export declare const buildSvgMap: (inputConfigs: InputConfigWithDefaults[]) => Promise<SvgMap>;
2
+ /**
3
+ * Every input SVG by symbol id. `spritePath` is left out: when an input's `baseDir` is also the output directory, the
4
+ * generated sprite matches the input pattern too, and would otherwise turn up as an icon (in the types as well as the
5
+ * sprite) whenever it already exists from an earlier run.
6
+ */
7
+ export declare const buildSvgMap: (inputConfigs: InputConfigWithDefaults[], spritePath?: string) => Promise<SvgMap>;
package/dist/index.js CHANGED
@@ -34,13 +34,20 @@ fast_glob = __toESM(fast_glob);
34
34
  let node_html_parser = require("node-html-parser");
35
35
  let svgo = require("svgo");
36
36
  //#region src/build-svg-map.ts
37
- var buildSvgMap = async (inputConfigs) => {
37
+ /**
38
+ * Every input SVG by symbol id. `spritePath` is left out: when an input's `baseDir` is also the output directory, the
39
+ * generated sprite matches the input pattern too, and would otherwise turn up as an icon (in the types as well as the
40
+ * sprite) whenever it already exists from an earlier run.
41
+ */
42
+ var buildSvgMap = async (inputConfigs, spritePath) => {
38
43
  const svgMap = /* @__PURE__ */ new Map();
44
+ const excluded = spritePath && node_path.default.resolve(spritePath);
39
45
  await Promise.all(inputConfigs.map(async (inputConfig) => {
40
46
  const { pattern, baseDir, getSymbolId } = inputConfig;
41
47
  const matchedPaths = await (0, fast_glob.default)(pattern, { cwd: baseDir });
42
48
  await Promise.all(matchedPaths.map(async (matchPath) => {
43
49
  const filePath = node_path.default.join(baseDir, matchPath);
50
+ if (node_path.default.resolve(filePath) === excluded) return;
44
51
  const content = await node_fs.promises.readFile(filePath, "utf8");
45
52
  const resolvedInputConfig = {
46
53
  ...inputConfig,
@@ -125,12 +132,11 @@ var writeGitignore = async (filePath, spriteName) => {
125
132
  };
126
133
  //#endregion
127
134
  //#region src/write-sprite.ts
128
- var buildSpriteContent = (svgMap, spritePath) => {
135
+ var buildSpriteContent = (svgMap) => {
129
136
  const symbols = [];
130
137
  const definitions = [];
131
138
  const svgSorted = [...svgMap].sort((a, b) => String(a[0]).localeCompare(b[0])).map(([_, svg]) => svg);
132
139
  for (const svg of svgSorted) {
133
- if (spritePath && spritePath === svg.filePath) continue;
134
140
  let content = svg.content;
135
141
  if (svg.svgo !== false) {
136
142
  const svgoDefault = { plugins: [
@@ -181,7 +187,7 @@ var buildSpriteContent = (svgMap, spritePath) => {
181
187
  return lines.join("\n");
182
188
  };
183
189
  var writeSprite = (spritePath, svgMap) => {
184
- const content = buildSpriteContent(svgMap, spritePath);
190
+ const content = buildSpriteContent(svgMap);
185
191
  writeIfChanged(spritePath, content);
186
192
  return content;
187
193
  };
@@ -251,7 +257,7 @@ function svgSpritegen(config) {
251
257
  viteBase = config.base ?? "/";
252
258
  },
253
259
  async buildStart() {
254
- allSvgs = await buildSvgMap(inputConfigsResolved);
260
+ allSvgs = await buildSvgMap(inputConfigsResolved, spriteFilePath);
255
261
  await writeTypes(typesFilePath, allSvgs);
256
262
  if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, spriteNameResolved);
257
263
  if (isBuild) {
@@ -261,7 +267,7 @@ function svgSpritegen(config) {
261
267
  writeSprite(spriteFilePath, allSvgs);
262
268
  const onWatch = (changedPath) => {
263
269
  if (changedPath === spriteFilePath) return;
264
- buildSvgMap(inputConfigsResolved).then((newAllSvgFiles) => {
270
+ buildSvgMap(inputConfigsResolved, spriteFilePath).then((newAllSvgFiles) => {
265
271
  writeSprite(spriteFilePath, newAllSvgFiles);
266
272
  });
267
273
  };
@@ -311,7 +317,7 @@ function svgSpritegen(config) {
311
317
  spriteRefId = this.emitFile({
312
318
  type: "asset",
313
319
  name: spriteNameResolved,
314
- source: buildSpriteContent(sourceMap, spriteFilePath)
320
+ source: buildSpriteContent(sourceMap)
315
321
  });
316
322
  },
317
323
  renderChunk: {
package/dist/index.mjs CHANGED
@@ -6,13 +6,20 @@ import fastGlob from "fast-glob";
6
6
  import { parse } from "node-html-parser";
7
7
  import { optimize } from "svgo";
8
8
  //#region src/build-svg-map.ts
9
- var buildSvgMap = async (inputConfigs) => {
9
+ /**
10
+ * Every input SVG by symbol id. `spritePath` is left out: when an input's `baseDir` is also the output directory, the
11
+ * generated sprite matches the input pattern too, and would otherwise turn up as an icon (in the types as well as the
12
+ * sprite) whenever it already exists from an earlier run.
13
+ */
14
+ var buildSvgMap = async (inputConfigs, spritePath) => {
10
15
  const svgMap = /* @__PURE__ */ new Map();
16
+ const excluded = spritePath && path.resolve(spritePath);
11
17
  await Promise.all(inputConfigs.map(async (inputConfig) => {
12
18
  const { pattern, baseDir, getSymbolId } = inputConfig;
13
19
  const matchedPaths = await fastGlob(pattern, { cwd: baseDir });
14
20
  await Promise.all(matchedPaths.map(async (matchPath) => {
15
21
  const filePath = path.join(baseDir, matchPath);
22
+ if (path.resolve(filePath) === excluded) return;
16
23
  const content = await promises.readFile(filePath, "utf8");
17
24
  const resolvedInputConfig = {
18
25
  ...inputConfig,
@@ -97,12 +104,11 @@ var writeGitignore = async (filePath, spriteName) => {
97
104
  };
98
105
  //#endregion
99
106
  //#region src/write-sprite.ts
100
- var buildSpriteContent = (svgMap, spritePath) => {
107
+ var buildSpriteContent = (svgMap) => {
101
108
  const symbols = [];
102
109
  const definitions = [];
103
110
  const svgSorted = [...svgMap].sort((a, b) => String(a[0]).localeCompare(b[0])).map(([_, svg]) => svg);
104
111
  for (const svg of svgSorted) {
105
- if (spritePath && spritePath === svg.filePath) continue;
106
112
  let content = svg.content;
107
113
  if (svg.svgo !== false) {
108
114
  const svgoDefault = { plugins: [
@@ -153,7 +159,7 @@ var buildSpriteContent = (svgMap, spritePath) => {
153
159
  return lines.join("\n");
154
160
  };
155
161
  var writeSprite = (spritePath, svgMap) => {
156
- const content = buildSpriteContent(svgMap, spritePath);
162
+ const content = buildSpriteContent(svgMap);
157
163
  writeIfChanged(spritePath, content);
158
164
  return content;
159
165
  };
@@ -223,7 +229,7 @@ function svgSpritegen(config) {
223
229
  viteBase = config.base ?? "/";
224
230
  },
225
231
  async buildStart() {
226
- allSvgs = await buildSvgMap(inputConfigsResolved);
232
+ allSvgs = await buildSvgMap(inputConfigsResolved, spriteFilePath);
227
233
  await writeTypes(typesFilePath, allSvgs);
228
234
  if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, spriteNameResolved);
229
235
  if (isBuild) {
@@ -233,7 +239,7 @@ function svgSpritegen(config) {
233
239
  writeSprite(spriteFilePath, allSvgs);
234
240
  const onWatch = (changedPath) => {
235
241
  if (changedPath === spriteFilePath) return;
236
- buildSvgMap(inputConfigsResolved).then((newAllSvgFiles) => {
242
+ buildSvgMap(inputConfigsResolved, spriteFilePath).then((newAllSvgFiles) => {
237
243
  writeSprite(spriteFilePath, newAllSvgFiles);
238
244
  });
239
245
  };
@@ -283,7 +289,7 @@ function svgSpritegen(config) {
283
289
  spriteRefId = this.emitFile({
284
290
  type: "asset",
285
291
  name: spriteNameResolved,
286
- source: buildSpriteContent(sourceMap, spriteFilePath)
292
+ source: buildSpriteContent(sourceMap)
287
293
  });
288
294
  },
289
295
  renderChunk: {
@@ -1,3 +1,3 @@
1
1
  import type { SvgMap } from './types';
2
- export declare const buildSpriteContent: (svgMap: SvgMap, spritePath?: string) => string;
2
+ export declare const buildSpriteContent: (svgMap: SvgMap) => string;
3
3
  export declare const writeSprite: (spritePath: string, svgMap: SvgMap) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svg-spritegen",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
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",