vite-plugin-svg-spritegen 2.0.2 → 2.0.4

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/README.md CHANGED
@@ -36,6 +36,15 @@ export default {
36
36
  };
37
37
  ```
38
38
 
39
+ ### Generated Files
40
+
41
+ The plugin writes two files to `outputDir`:
42
+
43
+ - `sprite.svg` — the generated spritesheet. This is a build artifact, so the plugin adds it to a `.gitignore` inside `outputDir` (disable this with `gitignore: false`).
44
+ - `types.ts` — the `IconName` type and `iconNames` array, always generated from the **full** set of source SVGs (`stripUnused` never affects it). **Commit this file** so typechecking works on a fresh clone without running a build first. It regenerates automatically on every dev-server start and build, so it can't drift for long.
45
+
46
+ > **Upgrading from v2.0.1 or earlier?** Previous versions added `types.ts` to the generated `.gitignore`. The plugin removes that stale entry the next time it runs — after that, `git add` your `types.ts` to start tracking it.
47
+
39
48
  ### Example Icon Component
40
49
 
41
50
  ```jsx
package/dist/index.js CHANGED
@@ -81,32 +81,47 @@ var writeIfChanged = (filePath, content) => {
81
81
  var writeTypes = async (filePath, svgMap) => {
82
82
  await writeIfChanged(filePath, [
83
83
  "/* eslint-disable */",
84
+ "// biome-ignore-all format: autogenerated file — kept byte-stable across rebuilds",
85
+ "// biome-ignore-all lint: autogenerated file — kept byte-stable across rebuilds",
84
86
  "",
85
- "// This file is autogenerated. Do not commit to source control.",
87
+ "// This file is autogenerated. Keep it committed so typechecking works without a build.",
86
88
  "",
87
89
  "export type IconName = (typeof iconNames)[number];",
88
90
  "",
89
91
  "export const iconNames = [",
90
- ...[...svgMap.keys()].map((name) => ` '${name}',`),
92
+ ...[...svgMap.keys()].sort().map((name) => `\t'${name}',`),
91
93
  "] as const;",
92
94
  ""
93
95
  ].join("\n"));
94
96
  };
95
97
  //#endregion
96
98
  //#region src/write-gitignore.ts
97
- var writeGitignore = async (filePath, spriteName, typesName) => {
98
- const content = [
99
- "# svg spritegen",
100
- spriteName,
101
- typesName
102
- ].join("\n");
99
+ var SECTION_HEADER = "# svg spritegen";
100
+ var writeGitignore = async (filePath, spriteName) => {
101
+ const section = [SECTION_HEADER, spriteName].join("\n");
102
+ let currentContent;
103
103
  try {
104
- const currentContent = await node_fs.promises.readFile(filePath, "utf8");
105
- if (currentContent.includes(content)) return;
106
- else await node_fs.promises.writeFile(filePath, currentContent + "\n" + content, "utf8");
104
+ currentContent = await node_fs.promises.readFile(filePath, "utf8");
107
105
  } catch (e) {
108
- await node_fs.promises.writeFile(filePath, content, "utf8");
106
+ await node_fs.promises.writeFile(filePath, section + "\n", "utf8");
107
+ return;
109
108
  }
109
+ const kept = [];
110
+ let inSection = false;
111
+ for (const line of currentContent.split("\n")) {
112
+ if (line.trim() === SECTION_HEADER) {
113
+ inSection = true;
114
+ continue;
115
+ }
116
+ if (inSection) {
117
+ if (line.trim() !== "" && !line.startsWith("#")) continue;
118
+ inSection = false;
119
+ }
120
+ kept.push(line);
121
+ }
122
+ const preserved = kept.join("\n").trim();
123
+ const newContent = (preserved ? preserved + "\n\n" : "") + section + "\n";
124
+ if (newContent !== currentContent) await node_fs.promises.writeFile(filePath, newContent, "utf8");
110
125
  };
111
126
  //#endregion
112
127
  //#region src/write-sprite.ts
@@ -227,7 +242,7 @@ function svgSpritegen(config) {
227
242
  async buildStart() {
228
243
  allSvgs = await buildSvgMap(inputConfigsResolved);
229
244
  await writeTypes(typesFilePath, allSvgs);
230
- if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, "sprite.svg", "types.ts");
245
+ if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, spriteNameResolved);
231
246
  if (isBuild) {
232
247
  writeIfChanged(spriteFilePath);
233
248
  return;
package/dist/index.mjs CHANGED
@@ -53,32 +53,47 @@ var writeIfChanged = (filePath, content) => {
53
53
  var writeTypes = async (filePath, svgMap) => {
54
54
  await writeIfChanged(filePath, [
55
55
  "/* eslint-disable */",
56
+ "// biome-ignore-all format: autogenerated file — kept byte-stable across rebuilds",
57
+ "// biome-ignore-all lint: autogenerated file — kept byte-stable across rebuilds",
56
58
  "",
57
- "// This file is autogenerated. Do not commit to source control.",
59
+ "// This file is autogenerated. Keep it committed so typechecking works without a build.",
58
60
  "",
59
61
  "export type IconName = (typeof iconNames)[number];",
60
62
  "",
61
63
  "export const iconNames = [",
62
- ...[...svgMap.keys()].map((name) => ` '${name}',`),
64
+ ...[...svgMap.keys()].sort().map((name) => `\t'${name}',`),
63
65
  "] as const;",
64
66
  ""
65
67
  ].join("\n"));
66
68
  };
67
69
  //#endregion
68
70
  //#region src/write-gitignore.ts
69
- var writeGitignore = async (filePath, spriteName, typesName) => {
70
- const content = [
71
- "# svg spritegen",
72
- spriteName,
73
- typesName
74
- ].join("\n");
71
+ var SECTION_HEADER = "# svg spritegen";
72
+ var writeGitignore = async (filePath, spriteName) => {
73
+ const section = [SECTION_HEADER, spriteName].join("\n");
74
+ let currentContent;
75
75
  try {
76
- const currentContent = await promises.readFile(filePath, "utf8");
77
- if (currentContent.includes(content)) return;
78
- else await promises.writeFile(filePath, currentContent + "\n" + content, "utf8");
76
+ currentContent = await promises.readFile(filePath, "utf8");
79
77
  } catch (e) {
80
- await promises.writeFile(filePath, content, "utf8");
78
+ await promises.writeFile(filePath, section + "\n", "utf8");
79
+ return;
81
80
  }
81
+ const kept = [];
82
+ let inSection = false;
83
+ for (const line of currentContent.split("\n")) {
84
+ if (line.trim() === SECTION_HEADER) {
85
+ inSection = true;
86
+ continue;
87
+ }
88
+ if (inSection) {
89
+ if (line.trim() !== "" && !line.startsWith("#")) continue;
90
+ inSection = false;
91
+ }
92
+ kept.push(line);
93
+ }
94
+ const preserved = kept.join("\n").trim();
95
+ const newContent = (preserved ? preserved + "\n\n" : "") + section + "\n";
96
+ if (newContent !== currentContent) await promises.writeFile(filePath, newContent, "utf8");
82
97
  };
83
98
  //#endregion
84
99
  //#region src/write-sprite.ts
@@ -199,7 +214,7 @@ function svgSpritegen(config) {
199
214
  async buildStart() {
200
215
  allSvgs = await buildSvgMap(inputConfigsResolved);
201
216
  await writeTypes(typesFilePath, allSvgs);
202
- if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, "sprite.svg", "types.ts");
217
+ if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, spriteNameResolved);
203
218
  if (isBuild) {
204
219
  writeIfChanged(spriteFilePath);
205
220
  return;
@@ -1 +1 @@
1
- export declare const writeGitignore: (filePath: string, spriteName: string, typesName: string) => Promise<void>;
1
+ export declare const writeGitignore: (filePath: string, spriteName: string) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-svg-spritegen",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
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",