vite-plugin-svg-spritegen 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/dist/index.js +11 -7
- package/dist/index.mjs +11 -7
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -141,6 +141,7 @@ const svgoDefault = {
|
|
|
141
141
|
}
|
|
142
142
|
]
|
|
143
143
|
};
|
|
144
|
+
const defaultMatchPattern = /:\s*(?:"|'|`)(?<icon>.+?)(?:"|'|`)/g;
|
|
144
145
|
const inputConfigDefaults = {
|
|
145
146
|
pattern: "**/*.svg",
|
|
146
147
|
baseDir: "./",
|
|
@@ -153,7 +154,8 @@ const inputConfigDefaults = {
|
|
|
153
154
|
const stripUnusedDefaults = {
|
|
154
155
|
enabled: true,
|
|
155
156
|
srcInclude: "**/*.[jt]sx",
|
|
156
|
-
srcExclude: []
|
|
157
|
+
srcExclude: [],
|
|
158
|
+
whitelist: []
|
|
157
159
|
};
|
|
158
160
|
function svgSpritegen(config) {
|
|
159
161
|
const inputConfigs = Array.isArray(config.input) ? config.input : [config.input ?? inputConfigDefaults];
|
|
@@ -214,12 +216,14 @@ function svgSpritegen(config) {
|
|
|
214
216
|
watcher = chokidar.watch(inputPaths, { ignoreInitial: true }).on("add", onWatch).on("change", onWatch).on("unlink", onWatch);
|
|
215
217
|
},
|
|
216
218
|
moduleParsed(info) {
|
|
217
|
-
var _a;
|
|
218
219
|
if (!isBuild || !stripUnusedResolved.enabled || !srcFilter(info.id) || !info.code) return;
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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;
|
|
223
227
|
const svgPath = allSvgFiles.get(icon);
|
|
224
228
|
if (!svgPath) continue;
|
|
225
229
|
referencedSvgFiles.set(icon, svgPath);
|
|
@@ -229,7 +233,7 @@ function svgSpritegen(config) {
|
|
|
229
233
|
if (stripUnusedResolved.enabled) {
|
|
230
234
|
const spriteContent = await writeSprite(spriteFilePath, referencedSvgFiles);
|
|
231
235
|
Object.values(bundle).forEach((file) => {
|
|
232
|
-
if (file.type === "asset" && file.
|
|
236
|
+
if (file.type === "asset" && file.names.includes(spriteNameResolved)) {
|
|
233
237
|
file.source = spriteContent;
|
|
234
238
|
}
|
|
235
239
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -139,6 +139,7 @@ const svgoDefault = {
|
|
|
139
139
|
}
|
|
140
140
|
]
|
|
141
141
|
};
|
|
142
|
+
const defaultMatchPattern = /:\s*(?:"|'|`)(?<icon>.+?)(?:"|'|`)/g;
|
|
142
143
|
const inputConfigDefaults = {
|
|
143
144
|
pattern: "**/*.svg",
|
|
144
145
|
baseDir: "./",
|
|
@@ -151,7 +152,8 @@ const inputConfigDefaults = {
|
|
|
151
152
|
const stripUnusedDefaults = {
|
|
152
153
|
enabled: true,
|
|
153
154
|
srcInclude: "**/*.[jt]sx",
|
|
154
|
-
srcExclude: []
|
|
155
|
+
srcExclude: [],
|
|
156
|
+
whitelist: []
|
|
155
157
|
};
|
|
156
158
|
function svgSpritegen(config) {
|
|
157
159
|
const inputConfigs = Array.isArray(config.input) ? config.input : [config.input ?? inputConfigDefaults];
|
|
@@ -212,12 +214,14 @@ function svgSpritegen(config) {
|
|
|
212
214
|
watcher = watch(inputPaths, { ignoreInitial: true }).on("add", onWatch).on("change", onWatch).on("unlink", onWatch);
|
|
213
215
|
},
|
|
214
216
|
moduleParsed(info) {
|
|
215
|
-
var _a;
|
|
216
217
|
if (!isBuild || !stripUnusedResolved.enabled || !srcFilter(info.id) || !info.code) return;
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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;
|
|
221
225
|
const svgPath = allSvgFiles.get(icon);
|
|
222
226
|
if (!svgPath) continue;
|
|
223
227
|
referencedSvgFiles.set(icon, svgPath);
|
|
@@ -227,7 +231,7 @@ function svgSpritegen(config) {
|
|
|
227
231
|
if (stripUnusedResolved.enabled) {
|
|
228
232
|
const spriteContent = await writeSprite(spriteFilePath, referencedSvgFiles);
|
|
229
233
|
Object.values(bundle).forEach((file) => {
|
|
230
|
-
if (file.type === "asset" && file.
|
|
234
|
+
if (file.type === "asset" && file.names.includes(spriteNameResolved)) {
|
|
231
235
|
file.source = spriteContent;
|
|
232
236
|
}
|
|
233
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