vite-awesome-svg-loader 1.4.0 → 1.4.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.
Files changed (4) hide show
  1. package/README.md +29 -3
  2. package/index.js +75 -56
  3. package/index.mjs +75 -56
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -99,14 +99,40 @@ viteAwesomeSvgLoader({
99
99
  // A list of files or directories to preserve line width of.
100
100
  preserveLineWidthList: [/config-demo\/preserve-line-width\//, /config-demo\/all\//],
101
101
 
102
+ // A list of files to skip while preserving line width. Overrides preserveLineWidthList.
103
+ skipPreserveLineWidthList: [/line-width-not-preserved\.svg/],
104
+
102
105
  // A list of files or directories to preserve color of
103
106
  setCurrentColorList: [/config-demo\/set-current-color\//, /config-demo\/all\//],
104
107
 
105
- // A list of files to skip while transforming
106
- skipTransformsList: [/skip-transforms\.svg/],
108
+ // A list of files to skip while replacing colors. Overrides setCurrentColorList.
109
+ skipSetCurrentColorList: [/colors-not-preserved\.svg/],
110
+
111
+ // A list of files to skip while transforming. File skip-transforms.svg is present in every directory.
112
+ skipTransformsList: [/skip-transforms\.svg/, /ignore-elements-orig\.svg/],
107
113
 
108
- // A list of files to skip loading of
114
+ // A list of files to skip loading of. File skip-loading.svg is present in every directory.
109
115
  skipFilesList: [/skip-loading\.svg/],
116
+
117
+ // A list of selectors to skip while preserving line width
118
+ skipPreserveLineWidthSelectors: [
119
+ // It can be a list of CSS selectors like this one. Every element in every file will be checked against it.
120
+ '*[data-original-line-width="true"], *[data-original-line-width="true"] *',
121
+
122
+ // Or it can be configured on per-file basis:
123
+ {
124
+ files: [/ignore-elements\.svg/, /some-other-file\.svg/],
125
+ selectors: ['*[data-original-line-width="true"], *[data-original-line-width="true"] *'],
126
+ },
127
+ ],
128
+
129
+ // These two options are not recommended due to architectural and performance considerations (see JSDoc):
130
+
131
+ // A list of selectors to skip while replacing colors. Same format as above.
132
+ skipSetCurrentColorSelectors: ['*[data-original-color="true"], *[data-original-color="true"] *'],
133
+
134
+ // A list of selectors to skip while transforming. Same format as above.
135
+ skipTransformsSelectors: ['*[data-no-transforms="true"], *[data-no-transforms="true"] *'],
110
136
  }),
111
137
  ```
112
138
 
package/index.js CHANGED
@@ -156,13 +156,19 @@ function viteAwesomeSvgLoader(options = {}) {
156
156
  const shouldSetCurrentColor = !shouldSkipTransforms && matchesQueryOrList(relPathWithSlash, query["set-current-color"], mergedOptions.setCurrentColorList) && !matchesQueryOrList(relPathWithSlash, void 0, mergedOptions.skipSetCurrentColorList);
157
157
  const skipPreserveLineWidthSelectors = selectorsToList(
158
158
  relPathWithSlash,
159
- mergedOptions.skipPreserveLineWidthSelectors
159
+ mergedOptions.skipPreserveLineWidthSelectors,
160
+ !shouldPreserveLineWidth
160
161
  );
161
162
  const skipSetCurrentColorSelectors = selectorsToList(
162
163
  relPathWithSlash,
163
- mergedOptions.skipSetCurrentColorSelectors
164
+ mergedOptions.skipSetCurrentColorSelectors,
165
+ !shouldSetCurrentColor
166
+ );
167
+ const skipTransformsSelectors = selectorsToList(
168
+ relPathWithSlash,
169
+ mergedOptions.skipTransformsSelectors,
170
+ shouldSkipTransforms
164
171
  );
165
- const skipTransformsSelectors = selectorsToList(relPathWithSlash, mergedOptions.skipTransformsSelectors);
166
172
  const nodesWithOrigColors = [];
167
173
  let joinedParamsStr = "";
168
174
  for (const param of [shouldSkipTransforms, shouldPreserveLineWidth, shouldSetCurrentColor]) {
@@ -175,6 +181,7 @@ function viteAwesomeSvgLoader(options = {}) {
175
181
  const fullPath = root + relPathWithSlash;
176
182
  let code = import_fs_extra.default.readFileSync(fullPath).toString();
177
183
  let isFillSetOnRoot = false;
184
+ let didTransform = false;
178
185
  code = (0, import_svgo.optimize)(code, {
179
186
  multipass: true,
180
187
  plugins: [
@@ -189,6 +196,10 @@ function viteAwesomeSvgLoader(options = {}) {
189
196
  {
190
197
  name: "awesome-svg-loader",
191
198
  fn: () => {
199
+ if (didTransform) {
200
+ return null;
201
+ }
202
+ didTransform = true;
192
203
  return {
193
204
  root: {
194
205
  enter: (root2) => {
@@ -275,8 +286,11 @@ function matchesPath(relPathWithSlash, matchers) {
275
286
  }
276
287
  return false;
277
288
  }
278
- function selectorsToList(relPathWithSlash, selectors) {
289
+ function selectorsToList(relPathWithSlash, selectors, returnEmptyList) {
279
290
  const resolvedSelectors = [];
291
+ if (returnEmptyList) {
292
+ return resolvedSelectors;
293
+ }
280
294
  for (const selector of selectors) {
281
295
  if (typeof selector === "string") {
282
296
  resolvedSelectors.push(selector);
@@ -389,65 +403,70 @@ function setCurrentColorCss(css, nodesWithOrigColors, isInline = false) {
389
403
  let currentColorSelectors = [];
390
404
  let didSplitSelectors = false;
391
405
  const ast = csstree.parse(css, { context });
392
- csstree.walk(ast, function(node) {
393
- var _a, _b, _c, _d, _e, _f, _g;
394
- if (node.__SKIP_SVG_LOADER__ || ((_a = this.rule) == null ? void 0 : _a.__SKIP_SVG_LOADER__)) {
395
- return;
396
- }
397
- if (shouldPreserveColors) {
398
- if (node.type === "SelectorList") {
399
- origColorSelectors = [];
400
- currentColorSelectors = [];
401
- didSplitSelectors = false;
406
+ csstree.walk(ast, {
407
+ // Ignore because of broken types in csstree:
408
+ // @ts-ignore
409
+ visit: shouldPreserveColors ? void 0 : "Declaration",
410
+ enter: function(node) {
411
+ var _a, _b, _c, _d, _e, _f, _g;
412
+ if (node.__SKIP_SVG_LOADER__ || ((_a = this.rule) == null ? void 0 : _a.__SKIP_SVG_LOADER__)) {
402
413
  return;
403
414
  }
404
- if (node.type === "Selector") {
405
- const selector = csstree.generate(node);
406
- let isOrigColor = false;
407
- for (const svgNode of nodesWithOrigColors) {
408
- if ((0, import_xast.matches)(svgNode, selector)) {
409
- isOrigColor = true;
410
- node.__ORIG_COLOR__ = true;
411
- break;
415
+ if (shouldPreserveColors) {
416
+ if (node.type === "SelectorList") {
417
+ origColorSelectors = [];
418
+ currentColorSelectors = [];
419
+ didSplitSelectors = false;
420
+ return;
421
+ }
422
+ if (node.type === "Selector") {
423
+ const selector = csstree.generate(node);
424
+ let isOrigColor = false;
425
+ for (const svgNode of nodesWithOrigColors) {
426
+ if ((0, import_xast.matches)(svgNode, selector)) {
427
+ isOrigColor = true;
428
+ node.__ORIG_COLOR__ = true;
429
+ break;
430
+ }
412
431
  }
432
+ (isOrigColor ? origColorSelectors : currentColorSelectors).push(selector);
433
+ return;
413
434
  }
414
- (isOrigColor ? origColorSelectors : currentColorSelectors).push(selector);
435
+ }
436
+ if (node.type !== "Declaration" || !COLOR_ATTRS_TO_REPLACE[node.property]) {
415
437
  return;
416
438
  }
439
+ const identifier = (_c = (_b = node.value) == null ? void 0 : _b.children) == null ? void 0 : _c.first;
440
+ const color = (identifier == null ? void 0 : identifier.value) || (identifier == null ? void 0 : identifier.name);
441
+ if (!color || IGNORE_COLORS[color]) {
442
+ return;
443
+ }
444
+ if (shouldPreserveColors && !didSplitSelectors && ((_d = this.rule) == null ? void 0 : _d.prelude.type) === "SelectorList") {
445
+ const origColorsRule = csstree.clone(this.rule);
446
+ origColorsRule.__SKIP_SVG_LOADER__ = true;
447
+ const origColorsSelectors = new csstree.List();
448
+ const selectors = this.rule.prelude.children;
449
+ selectors.forEach((node2, listItem) => {
450
+ if (node2.__ORIG_COLOR__) {
451
+ selectors.remove(listItem);
452
+ origColorsSelectors.push(node2);
453
+ }
454
+ });
455
+ origColorsRule.prelude.children = origColorsSelectors;
456
+ const parent = ((_f = (_e = this.atrule) == null ? void 0 : _e.block) == null ? void 0 : _f.children) || ((_g = this.stylesheet) == null ? void 0 : _g.children);
457
+ let insertBefore;
458
+ parent == null ? void 0 : parent.some((rule, listItem) => {
459
+ if (rule === this.rule) {
460
+ insertBefore = listItem;
461
+ return true;
462
+ }
463
+ return false;
464
+ });
465
+ insertBefore ? parent == null ? void 0 : parent.insertData(origColorsRule, insertBefore) : parent == null ? void 0 : parent.push(origColorsRule);
466
+ didSplitSelectors = true;
467
+ }
468
+ node.value = csstree.parse("currentColor", { context: "value" });
417
469
  }
418
- if (node.type !== "Declaration" || !COLOR_ATTRS_TO_REPLACE[node.property]) {
419
- return;
420
- }
421
- const identifier = (_c = (_b = node.value) == null ? void 0 : _b.children) == null ? void 0 : _c.first;
422
- const color = (identifier == null ? void 0 : identifier.value) || (identifier == null ? void 0 : identifier.name);
423
- if (!color || IGNORE_COLORS[color]) {
424
- return;
425
- }
426
- if (shouldPreserveColors && !didSplitSelectors && ((_d = this.rule) == null ? void 0 : _d.prelude.type) === "SelectorList") {
427
- const origColorsRule = csstree.clone(this.rule);
428
- origColorsRule.__SKIP_SVG_LOADER__ = true;
429
- const origColorsSelectors = new csstree.List();
430
- const selectors = this.rule.prelude.children;
431
- selectors.forEach((node2, listItem) => {
432
- if (node2.__ORIG_COLOR__) {
433
- selectors.remove(listItem);
434
- origColorsSelectors.push(node2);
435
- }
436
- });
437
- origColorsRule.prelude.children = origColorsSelectors;
438
- const parent = ((_f = (_e = this.atrule) == null ? void 0 : _e.block) == null ? void 0 : _f.children) || ((_g = this.stylesheet) == null ? void 0 : _g.children);
439
- let insertBefore;
440
- parent == null ? void 0 : parent.some((rule, listItem) => {
441
- if (rule === this.rule) {
442
- insertBefore = listItem;
443
- return true;
444
- }
445
- return false;
446
- });
447
- insertBefore ? parent == null ? void 0 : parent.insertData(origColorsRule, insertBefore) : parent == null ? void 0 : parent.push(origColorsRule);
448
- didSplitSelectors = true;
449
- }
450
- node.value = csstree.parse("currentColor", { context: "value" });
451
470
  });
452
471
  return csstree.generate(ast);
453
472
  }
package/index.mjs CHANGED
@@ -125,13 +125,19 @@ function viteAwesomeSvgLoader(options = {}) {
125
125
  const shouldSetCurrentColor = !shouldSkipTransforms && matchesQueryOrList(relPathWithSlash, query["set-current-color"], mergedOptions.setCurrentColorList) && !matchesQueryOrList(relPathWithSlash, void 0, mergedOptions.skipSetCurrentColorList);
126
126
  const skipPreserveLineWidthSelectors = selectorsToList(
127
127
  relPathWithSlash,
128
- mergedOptions.skipPreserveLineWidthSelectors
128
+ mergedOptions.skipPreserveLineWidthSelectors,
129
+ !shouldPreserveLineWidth
129
130
  );
130
131
  const skipSetCurrentColorSelectors = selectorsToList(
131
132
  relPathWithSlash,
132
- mergedOptions.skipSetCurrentColorSelectors
133
+ mergedOptions.skipSetCurrentColorSelectors,
134
+ !shouldSetCurrentColor
135
+ );
136
+ const skipTransformsSelectors = selectorsToList(
137
+ relPathWithSlash,
138
+ mergedOptions.skipTransformsSelectors,
139
+ shouldSkipTransforms
133
140
  );
134
- const skipTransformsSelectors = selectorsToList(relPathWithSlash, mergedOptions.skipTransformsSelectors);
135
141
  const nodesWithOrigColors = [];
136
142
  let joinedParamsStr = "";
137
143
  for (const param of [shouldSkipTransforms, shouldPreserveLineWidth, shouldSetCurrentColor]) {
@@ -144,6 +150,7 @@ function viteAwesomeSvgLoader(options = {}) {
144
150
  const fullPath = root + relPathWithSlash;
145
151
  let code = fs.readFileSync(fullPath).toString();
146
152
  let isFillSetOnRoot = false;
153
+ let didTransform = false;
147
154
  code = optimize(code, {
148
155
  multipass: true,
149
156
  plugins: [
@@ -158,6 +165,10 @@ function viteAwesomeSvgLoader(options = {}) {
158
165
  {
159
166
  name: "awesome-svg-loader",
160
167
  fn: () => {
168
+ if (didTransform) {
169
+ return null;
170
+ }
171
+ didTransform = true;
161
172
  return {
162
173
  root: {
163
174
  enter: (root2) => {
@@ -244,8 +255,11 @@ function matchesPath(relPathWithSlash, matchers) {
244
255
  }
245
256
  return false;
246
257
  }
247
- function selectorsToList(relPathWithSlash, selectors) {
258
+ function selectorsToList(relPathWithSlash, selectors, returnEmptyList) {
248
259
  const resolvedSelectors = [];
260
+ if (returnEmptyList) {
261
+ return resolvedSelectors;
262
+ }
249
263
  for (const selector of selectors) {
250
264
  if (typeof selector === "string") {
251
265
  resolvedSelectors.push(selector);
@@ -358,65 +372,70 @@ function setCurrentColorCss(css, nodesWithOrigColors, isInline = false) {
358
372
  let currentColorSelectors = [];
359
373
  let didSplitSelectors = false;
360
374
  const ast = csstree.parse(css, { context });
361
- csstree.walk(ast, function(node) {
362
- var _a, _b, _c, _d, _e, _f, _g;
363
- if (node.__SKIP_SVG_LOADER__ || ((_a = this.rule) == null ? void 0 : _a.__SKIP_SVG_LOADER__)) {
364
- return;
365
- }
366
- if (shouldPreserveColors) {
367
- if (node.type === "SelectorList") {
368
- origColorSelectors = [];
369
- currentColorSelectors = [];
370
- didSplitSelectors = false;
375
+ csstree.walk(ast, {
376
+ // Ignore because of broken types in csstree:
377
+ // @ts-ignore
378
+ visit: shouldPreserveColors ? void 0 : "Declaration",
379
+ enter: function(node) {
380
+ var _a, _b, _c, _d, _e, _f, _g;
381
+ if (node.__SKIP_SVG_LOADER__ || ((_a = this.rule) == null ? void 0 : _a.__SKIP_SVG_LOADER__)) {
371
382
  return;
372
383
  }
373
- if (node.type === "Selector") {
374
- const selector = csstree.generate(node);
375
- let isOrigColor = false;
376
- for (const svgNode of nodesWithOrigColors) {
377
- if (matchesSelector(svgNode, selector)) {
378
- isOrigColor = true;
379
- node.__ORIG_COLOR__ = true;
380
- break;
384
+ if (shouldPreserveColors) {
385
+ if (node.type === "SelectorList") {
386
+ origColorSelectors = [];
387
+ currentColorSelectors = [];
388
+ didSplitSelectors = false;
389
+ return;
390
+ }
391
+ if (node.type === "Selector") {
392
+ const selector = csstree.generate(node);
393
+ let isOrigColor = false;
394
+ for (const svgNode of nodesWithOrigColors) {
395
+ if (matchesSelector(svgNode, selector)) {
396
+ isOrigColor = true;
397
+ node.__ORIG_COLOR__ = true;
398
+ break;
399
+ }
381
400
  }
401
+ (isOrigColor ? origColorSelectors : currentColorSelectors).push(selector);
402
+ return;
382
403
  }
383
- (isOrigColor ? origColorSelectors : currentColorSelectors).push(selector);
404
+ }
405
+ if (node.type !== "Declaration" || !COLOR_ATTRS_TO_REPLACE[node.property]) {
384
406
  return;
385
407
  }
408
+ const identifier = (_c = (_b = node.value) == null ? void 0 : _b.children) == null ? void 0 : _c.first;
409
+ const color = (identifier == null ? void 0 : identifier.value) || (identifier == null ? void 0 : identifier.name);
410
+ if (!color || IGNORE_COLORS[color]) {
411
+ return;
412
+ }
413
+ if (shouldPreserveColors && !didSplitSelectors && ((_d = this.rule) == null ? void 0 : _d.prelude.type) === "SelectorList") {
414
+ const origColorsRule = csstree.clone(this.rule);
415
+ origColorsRule.__SKIP_SVG_LOADER__ = true;
416
+ const origColorsSelectors = new csstree.List();
417
+ const selectors = this.rule.prelude.children;
418
+ selectors.forEach((node2, listItem) => {
419
+ if (node2.__ORIG_COLOR__) {
420
+ selectors.remove(listItem);
421
+ origColorsSelectors.push(node2);
422
+ }
423
+ });
424
+ origColorsRule.prelude.children = origColorsSelectors;
425
+ const parent = ((_f = (_e = this.atrule) == null ? void 0 : _e.block) == null ? void 0 : _f.children) || ((_g = this.stylesheet) == null ? void 0 : _g.children);
426
+ let insertBefore;
427
+ parent == null ? void 0 : parent.some((rule, listItem) => {
428
+ if (rule === this.rule) {
429
+ insertBefore = listItem;
430
+ return true;
431
+ }
432
+ return false;
433
+ });
434
+ insertBefore ? parent == null ? void 0 : parent.insertData(origColorsRule, insertBefore) : parent == null ? void 0 : parent.push(origColorsRule);
435
+ didSplitSelectors = true;
436
+ }
437
+ node.value = csstree.parse("currentColor", { context: "value" });
386
438
  }
387
- if (node.type !== "Declaration" || !COLOR_ATTRS_TO_REPLACE[node.property]) {
388
- return;
389
- }
390
- const identifier = (_c = (_b = node.value) == null ? void 0 : _b.children) == null ? void 0 : _c.first;
391
- const color = (identifier == null ? void 0 : identifier.value) || (identifier == null ? void 0 : identifier.name);
392
- if (!color || IGNORE_COLORS[color]) {
393
- return;
394
- }
395
- if (shouldPreserveColors && !didSplitSelectors && ((_d = this.rule) == null ? void 0 : _d.prelude.type) === "SelectorList") {
396
- const origColorsRule = csstree.clone(this.rule);
397
- origColorsRule.__SKIP_SVG_LOADER__ = true;
398
- const origColorsSelectors = new csstree.List();
399
- const selectors = this.rule.prelude.children;
400
- selectors.forEach((node2, listItem) => {
401
- if (node2.__ORIG_COLOR__) {
402
- selectors.remove(listItem);
403
- origColorsSelectors.push(node2);
404
- }
405
- });
406
- origColorsRule.prelude.children = origColorsSelectors;
407
- const parent = ((_f = (_e = this.atrule) == null ? void 0 : _e.block) == null ? void 0 : _f.children) || ((_g = this.stylesheet) == null ? void 0 : _g.children);
408
- let insertBefore;
409
- parent == null ? void 0 : parent.some((rule, listItem) => {
410
- if (rule === this.rule) {
411
- insertBefore = listItem;
412
- return true;
413
- }
414
- return false;
415
- });
416
- insertBefore ? parent == null ? void 0 : parent.insertData(origColorsRule, insertBefore) : parent == null ? void 0 : parent.push(origColorsRule);
417
- didSplitSelectors = true;
418
- }
419
- node.value = csstree.parse("currentColor", { context: "value" });
420
439
  });
421
440
  return csstree.generate(ast);
422
441
  }
package/package.json CHANGED
@@ -27,7 +27,7 @@
27
27
  "vite-awesome-svg-loader",
28
28
  "svgo"
29
29
  ],
30
- "version": "1.4.0",
30
+ "version": "1.4.1",
31
31
  "homepage": "https://github.com/matafokka/vite-awesome-svg-loader",
32
32
  "repository": "https://github.com/matafokka/vite-awesome-svg-loader",
33
33
  "license": "LGPL-2.1-or-later",