vite-plugin-svg-spritegen 1.1.1 → 1.2.1
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 +13 -8
- package/dist/index.mjs +13 -8
- 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 = /(?:(?:name)|(?:iconName)|(?:icon)): "(?<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,22 +216,25 @@ 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
|
+
const matchPattern = config.matchPattern ? new RegExp(config.matchPattern, "g") : defaultMatchPattern;
|
|
221
|
+
const matches = [...info.code.matchAll(matchPattern)].flatMap(
|
|
222
|
+
({ groups }) => (groups == null ? void 0 : groups.icon) ?? []
|
|
223
|
+
);
|
|
224
|
+
matches.push(...stripUnusedResolved.whitelist);
|
|
225
|
+
console.log(matches);
|
|
220
226
|
for (const match of matches) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const svgPath = allSvgFiles.get(icon);
|
|
227
|
+
if (referencedSvgFiles.has(match)) continue;
|
|
228
|
+
const svgPath = allSvgFiles.get(match);
|
|
224
229
|
if (!svgPath) continue;
|
|
225
|
-
referencedSvgFiles.set(
|
|
230
|
+
referencedSvgFiles.set(match, svgPath);
|
|
226
231
|
}
|
|
227
232
|
},
|
|
228
233
|
async generateBundle(_options, bundle) {
|
|
229
234
|
if (stripUnusedResolved.enabled) {
|
|
230
235
|
const spriteContent = await writeSprite(spriteFilePath, referencedSvgFiles);
|
|
231
236
|
Object.values(bundle).forEach((file) => {
|
|
232
|
-
if (file.type === "asset" && file.
|
|
237
|
+
if (file.type === "asset" && file.names.includes(spriteNameResolved)) {
|
|
233
238
|
file.source = spriteContent;
|
|
234
239
|
}
|
|
235
240
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -139,6 +139,7 @@ const svgoDefault = {
|
|
|
139
139
|
}
|
|
140
140
|
]
|
|
141
141
|
};
|
|
142
|
+
const defaultMatchPattern = /(?:(?:name)|(?:iconName)|(?:icon)): "(?<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,22 +214,25 @@ 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
|
+
const matchPattern = config.matchPattern ? new RegExp(config.matchPattern, "g") : defaultMatchPattern;
|
|
219
|
+
const matches = [...info.code.matchAll(matchPattern)].flatMap(
|
|
220
|
+
({ groups }) => (groups == null ? void 0 : groups.icon) ?? []
|
|
221
|
+
);
|
|
222
|
+
matches.push(...stripUnusedResolved.whitelist);
|
|
223
|
+
console.log(matches);
|
|
218
224
|
for (const match of matches) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const svgPath = allSvgFiles.get(icon);
|
|
225
|
+
if (referencedSvgFiles.has(match)) continue;
|
|
226
|
+
const svgPath = allSvgFiles.get(match);
|
|
222
227
|
if (!svgPath) continue;
|
|
223
|
-
referencedSvgFiles.set(
|
|
228
|
+
referencedSvgFiles.set(match, svgPath);
|
|
224
229
|
}
|
|
225
230
|
},
|
|
226
231
|
async generateBundle(_options, bundle) {
|
|
227
232
|
if (stripUnusedResolved.enabled) {
|
|
228
233
|
const spriteContent = await writeSprite(spriteFilePath, referencedSvgFiles);
|
|
229
234
|
Object.values(bundle).forEach((file) => {
|
|
230
|
-
if (file.type === "asset" && file.
|
|
235
|
+
if (file.type === "asset" && file.names.includes(spriteNameResolved)) {
|
|
231
236
|
file.source = spriteContent;
|
|
232
237
|
}
|
|
233
238
|
});
|
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