vite-awesome-svg-loader 2.0.0 → 2.0.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 (3) hide show
  1. package/index.js +18 -10
  2. package/index.mjs +18 -10
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -84,14 +84,17 @@ function matchesPath(relPathWithSlash, matchers) {
84
84
  for (const matcher of matchers) {
85
85
  const isRegex = matcher instanceof RegExp;
86
86
  for (const entry of toMatch) {
87
- if (isRegex) {
88
- return !!matcher.exec(entry);
87
+ const matches = isRegex ? matcher.test(entry) : entry === matcher;
88
+ if (matches) {
89
+ return true;
89
90
  }
90
- return entry === matcher;
91
91
  }
92
92
  }
93
93
  return false;
94
94
  }
95
+ function normalizeSelector(selector) {
96
+ return selector.replaceAll(/\s+/g, " ").trim();
97
+ }
95
98
  function selectorsToList(relPathWithSlash, selectors, returnEmptyList) {
96
99
  const resolvedSelectors = [];
97
100
  if (returnEmptyList) {
@@ -99,11 +102,13 @@ function selectorsToList(relPathWithSlash, selectors, returnEmptyList) {
99
102
  }
100
103
  for (const selector of selectors) {
101
104
  if (typeof selector === "string") {
102
- resolvedSelectors.push(selector);
105
+ resolvedSelectors.push(normalizeSelector(selector));
103
106
  continue;
104
107
  }
105
108
  if (matchesPath(relPathWithSlash, selector.files)) {
106
- resolvedSelectors.push(...selector.selectors);
109
+ for (const selectorStr of selector.selectors) {
110
+ resolvedSelectors.push(normalizeSelector(selectorStr));
111
+ }
107
112
  }
108
113
  }
109
114
  return resolvedSelectors;
@@ -119,7 +124,7 @@ function matchesSelectors(node, selectors) {
119
124
  }
120
125
  function replaceColor(color, replacements) {
121
126
  if (!color) {
122
- return "";
127
+ return replacements.default || "";
123
128
  }
124
129
  return replacements.replacements[color.toLowerCase()] || replacements.default || color;
125
130
  }
@@ -471,14 +476,17 @@ function viteAwesomeSvgLoader(options = {}) {
471
476
  mergedOptions.skipTransformsSelectors,
472
477
  shouldSkipTransforms
473
478
  );
474
- let hash = relPathWithSlash + "-";
479
+ const hashParts = [relPathWithSlash];
480
+ for (const arr of [skipPreserveLineWidthSelectors, skipReplaceColorsSelectors, skipTransformsSelectors]) {
481
+ hashParts.push(arr.join(","));
482
+ }
475
483
  for (const param of [shouldSkipTransforms, shouldPreserveLineWidth, shouldReplaceColors]) {
476
- hash += param ? "1" : "0";
484
+ hashParts.push(param ? "1" : "0");
477
485
  }
478
486
  if (shouldReplaceColors) {
479
- hash += JSON.stringify(colorReplacements);
487
+ hashParts.push(JSON.stringify(colorReplacements));
480
488
  }
481
- hash = new import_imurmurhash.default(hash).result();
489
+ const hash = new import_imurmurhash.default(hashParts.join("__")).result();
482
490
  const fileNameNoExt = import_path2.default.basename(relPathWithSlash).split(".")[0];
483
491
  const assetFileNameNoExt = `${fileNameNoExt}-${hash}`;
484
492
  const assetFileName = assetFileNameNoExt + ".svg";
package/index.mjs CHANGED
@@ -51,14 +51,17 @@ function matchesPath(relPathWithSlash, matchers) {
51
51
  for (const matcher of matchers) {
52
52
  const isRegex = matcher instanceof RegExp;
53
53
  for (const entry of toMatch) {
54
- if (isRegex) {
55
- return !!matcher.exec(entry);
54
+ const matches = isRegex ? matcher.test(entry) : entry === matcher;
55
+ if (matches) {
56
+ return true;
56
57
  }
57
- return entry === matcher;
58
58
  }
59
59
  }
60
60
  return false;
61
61
  }
62
+ function normalizeSelector(selector) {
63
+ return selector.replaceAll(/\s+/g, " ").trim();
64
+ }
62
65
  function selectorsToList(relPathWithSlash, selectors, returnEmptyList) {
63
66
  const resolvedSelectors = [];
64
67
  if (returnEmptyList) {
@@ -66,11 +69,13 @@ function selectorsToList(relPathWithSlash, selectors, returnEmptyList) {
66
69
  }
67
70
  for (const selector of selectors) {
68
71
  if (typeof selector === "string") {
69
- resolvedSelectors.push(selector);
72
+ resolvedSelectors.push(normalizeSelector(selector));
70
73
  continue;
71
74
  }
72
75
  if (matchesPath(relPathWithSlash, selector.files)) {
73
- resolvedSelectors.push(...selector.selectors);
76
+ for (const selectorStr of selector.selectors) {
77
+ resolvedSelectors.push(normalizeSelector(selectorStr));
78
+ }
74
79
  }
75
80
  }
76
81
  return resolvedSelectors;
@@ -86,7 +91,7 @@ function matchesSelectors(node, selectors) {
86
91
  }
87
92
  function replaceColor(color, replacements) {
88
93
  if (!color) {
89
- return "";
94
+ return replacements.default || "";
90
95
  }
91
96
  return replacements.replacements[color.toLowerCase()] || replacements.default || color;
92
97
  }
@@ -438,14 +443,17 @@ function viteAwesomeSvgLoader(options = {}) {
438
443
  mergedOptions.skipTransformsSelectors,
439
444
  shouldSkipTransforms
440
445
  );
441
- let hash = relPathWithSlash + "-";
446
+ const hashParts = [relPathWithSlash];
447
+ for (const arr of [skipPreserveLineWidthSelectors, skipReplaceColorsSelectors, skipTransformsSelectors]) {
448
+ hashParts.push(arr.join(","));
449
+ }
442
450
  for (const param of [shouldSkipTransforms, shouldPreserveLineWidth, shouldReplaceColors]) {
443
- hash += param ? "1" : "0";
451
+ hashParts.push(param ? "1" : "0");
444
452
  }
445
453
  if (shouldReplaceColors) {
446
- hash += JSON.stringify(colorReplacements);
454
+ hashParts.push(JSON.stringify(colorReplacements));
447
455
  }
448
- hash = new MurmurHash3(hash).result();
456
+ const hash = new MurmurHash3(hashParts.join("__")).result();
449
457
  const fileNameNoExt = path2.basename(relPathWithSlash).split(".")[0];
450
458
  const assetFileNameNoExt = `${fileNameNoExt}-${hash}`;
451
459
  const assetFileName = assetFileNameNoExt + ".svg";
package/package.json CHANGED
@@ -27,7 +27,7 @@
27
27
  "vite-awesome-svg-loader",
28
28
  "svgo"
29
29
  ],
30
- "version": "2.0.0",
30
+ "version": "2.0.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",