vite-plugin-svg-spritegen 2.0.1 → 2.0.3
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 +9 -0
- package/dist/index.js +30 -14
- package/dist/index.mjs +30 -14
- package/dist/write-gitignore.d.ts +1 -1
- package/package.json +1 -1
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
|
@@ -82,31 +82,44 @@ var writeTypes = async (filePath, svgMap) => {
|
|
|
82
82
|
await writeIfChanged(filePath, [
|
|
83
83
|
"/* eslint-disable */",
|
|
84
84
|
"",
|
|
85
|
-
"// This file is autogenerated.
|
|
85
|
+
"// This file is autogenerated. Keep it committed so typechecking works without a build.",
|
|
86
86
|
"",
|
|
87
87
|
"export type IconName = (typeof iconNames)[number];",
|
|
88
88
|
"",
|
|
89
89
|
"export const iconNames = [",
|
|
90
|
-
...[...svgMap.keys()].map((name) => ` '${name}',`),
|
|
90
|
+
...[...svgMap.keys()].sort().map((name) => ` '${name}',`),
|
|
91
91
|
"] as const;",
|
|
92
92
|
""
|
|
93
93
|
].join("\n"));
|
|
94
94
|
};
|
|
95
95
|
//#endregion
|
|
96
96
|
//#region src/write-gitignore.ts
|
|
97
|
-
var
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
typesName
|
|
102
|
-
].join("\n");
|
|
97
|
+
var SECTION_HEADER = "# svg spritegen";
|
|
98
|
+
var writeGitignore = async (filePath, spriteName) => {
|
|
99
|
+
const section = [SECTION_HEADER, spriteName].join("\n");
|
|
100
|
+
let currentContent;
|
|
103
101
|
try {
|
|
104
|
-
|
|
105
|
-
if (currentContent.includes(content)) return;
|
|
106
|
-
else await node_fs.promises.writeFile(filePath, currentContent + "\n" + content, "utf8");
|
|
102
|
+
currentContent = await node_fs.promises.readFile(filePath, "utf8");
|
|
107
103
|
} catch (e) {
|
|
108
|
-
await node_fs.promises.writeFile(filePath,
|
|
104
|
+
await node_fs.promises.writeFile(filePath, section + "\n", "utf8");
|
|
105
|
+
return;
|
|
109
106
|
}
|
|
107
|
+
const kept = [];
|
|
108
|
+
let inSection = false;
|
|
109
|
+
for (const line of currentContent.split("\n")) {
|
|
110
|
+
if (line.trim() === SECTION_HEADER) {
|
|
111
|
+
inSection = true;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (inSection) {
|
|
115
|
+
if (line.trim() !== "" && !line.startsWith("#")) continue;
|
|
116
|
+
inSection = false;
|
|
117
|
+
}
|
|
118
|
+
kept.push(line);
|
|
119
|
+
}
|
|
120
|
+
const preserved = kept.join("\n").trim();
|
|
121
|
+
const newContent = (preserved ? preserved + "\n\n" : "") + section + "\n";
|
|
122
|
+
if (newContent !== currentContent) await node_fs.promises.writeFile(filePath, newContent, "utf8");
|
|
110
123
|
};
|
|
111
124
|
//#endregion
|
|
112
125
|
//#region src/write-sprite.ts
|
|
@@ -227,8 +240,11 @@ function svgSpritegen(config) {
|
|
|
227
240
|
async buildStart() {
|
|
228
241
|
allSvgs = await buildSvgMap(inputConfigsResolved);
|
|
229
242
|
await writeTypes(typesFilePath, allSvgs);
|
|
230
|
-
if (config.gitignore !== false) await writeGitignore(gitignoreFilePath,
|
|
231
|
-
if (isBuild)
|
|
243
|
+
if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, spriteNameResolved);
|
|
244
|
+
if (isBuild) {
|
|
245
|
+
writeIfChanged(spriteFilePath);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
232
248
|
writeSprite(spriteFilePath, allSvgs);
|
|
233
249
|
const onWatch = (changedPath) => {
|
|
234
250
|
if (changedPath === spriteFilePath) return;
|
package/dist/index.mjs
CHANGED
|
@@ -54,31 +54,44 @@ var writeTypes = async (filePath, svgMap) => {
|
|
|
54
54
|
await writeIfChanged(filePath, [
|
|
55
55
|
"/* eslint-disable */",
|
|
56
56
|
"",
|
|
57
|
-
"// This file is autogenerated.
|
|
57
|
+
"// This file is autogenerated. Keep it committed so typechecking works without a build.",
|
|
58
58
|
"",
|
|
59
59
|
"export type IconName = (typeof iconNames)[number];",
|
|
60
60
|
"",
|
|
61
61
|
"export const iconNames = [",
|
|
62
|
-
...[...svgMap.keys()].map((name) => ` '${name}',`),
|
|
62
|
+
...[...svgMap.keys()].sort().map((name) => ` '${name}',`),
|
|
63
63
|
"] as const;",
|
|
64
64
|
""
|
|
65
65
|
].join("\n"));
|
|
66
66
|
};
|
|
67
67
|
//#endregion
|
|
68
68
|
//#region src/write-gitignore.ts
|
|
69
|
-
var
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
typesName
|
|
74
|
-
].join("\n");
|
|
69
|
+
var SECTION_HEADER = "# svg spritegen";
|
|
70
|
+
var writeGitignore = async (filePath, spriteName) => {
|
|
71
|
+
const section = [SECTION_HEADER, spriteName].join("\n");
|
|
72
|
+
let currentContent;
|
|
75
73
|
try {
|
|
76
|
-
|
|
77
|
-
if (currentContent.includes(content)) return;
|
|
78
|
-
else await promises.writeFile(filePath, currentContent + "\n" + content, "utf8");
|
|
74
|
+
currentContent = await promises.readFile(filePath, "utf8");
|
|
79
75
|
} catch (e) {
|
|
80
|
-
await promises.writeFile(filePath,
|
|
76
|
+
await promises.writeFile(filePath, section + "\n", "utf8");
|
|
77
|
+
return;
|
|
81
78
|
}
|
|
79
|
+
const kept = [];
|
|
80
|
+
let inSection = false;
|
|
81
|
+
for (const line of currentContent.split("\n")) {
|
|
82
|
+
if (line.trim() === SECTION_HEADER) {
|
|
83
|
+
inSection = true;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (inSection) {
|
|
87
|
+
if (line.trim() !== "" && !line.startsWith("#")) continue;
|
|
88
|
+
inSection = false;
|
|
89
|
+
}
|
|
90
|
+
kept.push(line);
|
|
91
|
+
}
|
|
92
|
+
const preserved = kept.join("\n").trim();
|
|
93
|
+
const newContent = (preserved ? preserved + "\n\n" : "") + section + "\n";
|
|
94
|
+
if (newContent !== currentContent) await promises.writeFile(filePath, newContent, "utf8");
|
|
82
95
|
};
|
|
83
96
|
//#endregion
|
|
84
97
|
//#region src/write-sprite.ts
|
|
@@ -199,8 +212,11 @@ function svgSpritegen(config) {
|
|
|
199
212
|
async buildStart() {
|
|
200
213
|
allSvgs = await buildSvgMap(inputConfigsResolved);
|
|
201
214
|
await writeTypes(typesFilePath, allSvgs);
|
|
202
|
-
if (config.gitignore !== false) await writeGitignore(gitignoreFilePath,
|
|
203
|
-
if (isBuild)
|
|
215
|
+
if (config.gitignore !== false) await writeGitignore(gitignoreFilePath, spriteNameResolved);
|
|
216
|
+
if (isBuild) {
|
|
217
|
+
writeIfChanged(spriteFilePath);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
204
220
|
writeSprite(spriteFilePath, allSvgs);
|
|
205
221
|
const onWatch = (changedPath) => {
|
|
206
222
|
if (changedPath === spriteFilePath) return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const writeGitignore: (filePath: string, spriteName: string
|
|
1
|
+
export declare const writeGitignore: (filePath: string, spriteName: string) => Promise<void>;
|
package/package.json
CHANGED