@snack-uikit/icons 0.20.1 → 0.20.2-preview-3cdd8d31.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/README.md +0 -2
- package/dist/utils/createSprite.d.ts +6 -0
- package/dist/utils/createSprite.js +47 -0
- package/dist/utils/getAllSVGPaths.d.ts +1 -0
- package/dist/utils/getAllSVGPaths.js +21 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/mergeSprites.d.ts +5 -0
- package/dist/utils/mergeSprites.js +40 -0
- package/package.json +9 -5
- package/src/utils/createSprite.ts +53 -0
- package/src/utils/getAllSVGPaths.ts +32 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/mergeSprites.ts +44 -0
package/README.md
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
11
|
+
import { basename, join } from 'path';
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
import SVGSpriter from 'svg-sprite';
|
|
15
|
+
import { optimize } from 'svgo';
|
|
16
|
+
import { getAllSVGPaths } from './getAllSVGPaths';
|
|
17
|
+
const OUTPUT_DIRS = [join(...['src', 'sprite', 'svg']), join(...['dist', 'sprite', 'svg'])];
|
|
18
|
+
const PREFIX = 'snack-uikit-';
|
|
19
|
+
const FILENAME = 'sprite.symbol.svg';
|
|
20
|
+
export function createSprite({ source, destinations = OUTPUT_DIRS, fileName = FILENAME, prefix = PREFIX, }) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const spriter = new SVGSpriter({
|
|
23
|
+
dest: '',
|
|
24
|
+
mode: { symbol: true },
|
|
25
|
+
shape: {
|
|
26
|
+
transform: [
|
|
27
|
+
{
|
|
28
|
+
custom: (shape, sprite, callback) => {
|
|
29
|
+
const updatedSVG = optimize(shape.getSVG().replace(/fill="[A-Za-z0-9#]+"/g, 'fill="inherit"')).data;
|
|
30
|
+
shape.setSVG(updatedSVG);
|
|
31
|
+
callback(null);
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
const allPaths = getAllSVGPaths(source);
|
|
38
|
+
for (const path of allPaths) {
|
|
39
|
+
spriter.add(prefix + basename(path), null, readFileSync(path, 'utf-8'));
|
|
40
|
+
}
|
|
41
|
+
const { result } = yield spriter.compileAsync();
|
|
42
|
+
for (const dir of destinations) {
|
|
43
|
+
mkdirSync(dir, { recursive: true });
|
|
44
|
+
writeFileSync(join(dir, fileName), result.symbol.sprite.contents);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getAllSVGPaths(directory: string): string[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { readdirSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
export function getAllSVGPaths(directory) {
|
|
4
|
+
const readDirectory = (path) => readdirSync(path, {
|
|
5
|
+
withFileTypes: true,
|
|
6
|
+
encoding: 'utf-8',
|
|
7
|
+
}).map(item => ({ item, parent: path }));
|
|
8
|
+
let currentItem;
|
|
9
|
+
const paths = [];
|
|
10
|
+
const queue = readDirectory(directory);
|
|
11
|
+
while ((currentItem = queue.shift())) {
|
|
12
|
+
if (currentItem.item.isFile() && currentItem.item.name.endsWith('.svg')) {
|
|
13
|
+
paths.push(join(currentItem.parent, currentItem.item.name));
|
|
14
|
+
}
|
|
15
|
+
if (currentItem.item.isDirectory()) {
|
|
16
|
+
const parentDirectory = join(currentItem.parent, currentItem.item.name);
|
|
17
|
+
queue.push(...readDirectory(parentDirectory));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return paths;
|
|
21
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
11
|
+
import { JSDOM } from 'jsdom';
|
|
12
|
+
export function mergeSprites({ sprite1, sprite2, outputSprite, }) {
|
|
13
|
+
var _a;
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
// Read the SVG files
|
|
16
|
+
const svg1 = yield readFile(sprite1, 'utf-8');
|
|
17
|
+
const svg2 = yield readFile(sprite2, 'utf-8');
|
|
18
|
+
// Parse the SVG files
|
|
19
|
+
const dom1 = new JSDOM(svg1);
|
|
20
|
+
const dom2 = new JSDOM(svg2);
|
|
21
|
+
// Get the <symbol> elements
|
|
22
|
+
const symbols1 = dom1.window.document.querySelectorAll('symbol');
|
|
23
|
+
const symbols2 = dom2.window.document.querySelectorAll('symbol');
|
|
24
|
+
// Create a new SVG document
|
|
25
|
+
const outputDom = new JSDOM('<svg xmlns="http://www.w3.org/2000/svg"></svg>');
|
|
26
|
+
const outputSvg = outputDom.window.document.querySelector('svg');
|
|
27
|
+
// Append the <symbol> elements from the second file to the new SVG document
|
|
28
|
+
symbols2.forEach(symbol => outputSvg === null || outputSvg === void 0 ? void 0 : outputSvg.appendChild(outputDom.window.document.importNode(symbol, true)));
|
|
29
|
+
// Append the <symbol> elements from the first file to the new SVG document,
|
|
30
|
+
// only if not present in the output file
|
|
31
|
+
symbols1.forEach(symbol => {
|
|
32
|
+
const id = symbol.getAttribute('id');
|
|
33
|
+
if (!(outputSvg === null || outputSvg === void 0 ? void 0 : outputSvg.querySelector(`symbol[id="${id}"]`))) {
|
|
34
|
+
outputSvg === null || outputSvg === void 0 ? void 0 : outputSvg.appendChild(outputDom.window.document.importNode(symbol, true));
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
// Write the new SVG document to a file
|
|
38
|
+
yield writeFile(outputSprite, (_a = outputSvg === null || outputSvg === void 0 ? void 0 : outputSvg.outerHTML) !== null && _a !== void 0 ? _a : '', 'utf-8');
|
|
39
|
+
});
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"title": "Icons",
|
|
7
|
-
"version": "0.20.
|
|
7
|
+
"version": "0.20.2-preview-3cdd8d31.0",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.svg",
|
|
10
10
|
"*.css",
|
|
@@ -40,14 +40,18 @@
|
|
|
40
40
|
"build:interface-icons": "npm run fix:icons --directory=interface-icons && npm run build:icons --directory=interface-icons",
|
|
41
41
|
"compile": "npm run validate:icons && npm run build:interface-icons && npm run create-export-index-file && npm run create-sprite"
|
|
42
42
|
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"jsdom": "24.0.0",
|
|
45
|
+
"svg-sprite": "2.0.2",
|
|
46
|
+
"svgo": "3.1.0"
|
|
47
|
+
},
|
|
43
48
|
"devDependencies": {
|
|
44
49
|
"@svgr/cli": "8.1.0",
|
|
45
50
|
"@svgr/plugin-jsx": "8.1.0",
|
|
51
|
+
"@types/jsdom": "21.1.6",
|
|
46
52
|
"@types/svg-sprite": "0.0.38",
|
|
47
53
|
"fast-xml-parser": "4.2.5",
|
|
48
|
-
"oslllo-svg-fixer": "3.0.0"
|
|
49
|
-
"svg-sprite": "2.0.2",
|
|
50
|
-
"svgo": "3.1.0"
|
|
54
|
+
"oslllo-svg-fixer": "3.0.0"
|
|
51
55
|
},
|
|
52
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "5c53d9cb0f3fcf9430b33814daca0755e43cc103"
|
|
53
57
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
2
|
+
import { basename, join } from 'path';
|
|
3
|
+
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
import SVGSpriter from 'svg-sprite';
|
|
7
|
+
import { optimize } from 'svgo';
|
|
8
|
+
import { getAllSVGPaths } from './getAllSVGPaths';
|
|
9
|
+
|
|
10
|
+
const OUTPUT_DIRS = [join(...['src', 'sprite', 'svg']), join(...['dist', 'sprite', 'svg'])];
|
|
11
|
+
const PREFIX = 'snack-uikit-';
|
|
12
|
+
const FILENAME = 'sprite.symbol.svg';
|
|
13
|
+
|
|
14
|
+
export async function createSprite({
|
|
15
|
+
source,
|
|
16
|
+
destinations = OUTPUT_DIRS,
|
|
17
|
+
fileName = FILENAME,
|
|
18
|
+
prefix = PREFIX,
|
|
19
|
+
}: {
|
|
20
|
+
source: string;
|
|
21
|
+
destinations?: string[];
|
|
22
|
+
fileName?: string;
|
|
23
|
+
prefix?: string;
|
|
24
|
+
}) {
|
|
25
|
+
const spriter = new SVGSpriter({
|
|
26
|
+
dest: '',
|
|
27
|
+
mode: { symbol: true },
|
|
28
|
+
shape: {
|
|
29
|
+
transform: [
|
|
30
|
+
{
|
|
31
|
+
custom: (shape, sprite, callback) => {
|
|
32
|
+
const updatedSVG = optimize(shape.getSVG().replace(/fill="[A-Za-z0-9#]+"/g, 'fill="inherit"')).data;
|
|
33
|
+
shape.setSVG(updatedSVG);
|
|
34
|
+
callback(null);
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const allPaths = getAllSVGPaths(source);
|
|
42
|
+
|
|
43
|
+
for (const path of allPaths) {
|
|
44
|
+
spriter.add(prefix + basename(path), null, readFileSync(path, 'utf-8'));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { result } = await spriter.compileAsync();
|
|
48
|
+
|
|
49
|
+
for (const dir of destinations) {
|
|
50
|
+
mkdirSync(dir, { recursive: true });
|
|
51
|
+
writeFileSync(join(dir, fileName), result.symbol.sprite.contents);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Dirent, readdirSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
|
|
4
|
+
type CurrentItem = {
|
|
5
|
+
item: Dirent;
|
|
6
|
+
parent: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function getAllSVGPaths(directory: string): string[] {
|
|
10
|
+
const readDirectory = (path: string) =>
|
|
11
|
+
readdirSync(path, {
|
|
12
|
+
withFileTypes: true,
|
|
13
|
+
encoding: 'utf-8',
|
|
14
|
+
}).map(item => ({ item, parent: path }));
|
|
15
|
+
|
|
16
|
+
let currentItem: CurrentItem | undefined;
|
|
17
|
+
const paths: string[] = [];
|
|
18
|
+
const queue = readDirectory(directory);
|
|
19
|
+
|
|
20
|
+
while ((currentItem = queue.shift())) {
|
|
21
|
+
if (currentItem.item.isFile() && currentItem.item.name.endsWith('.svg')) {
|
|
22
|
+
paths.push(join(currentItem.parent, currentItem.item.name));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (currentItem.item.isDirectory()) {
|
|
26
|
+
const parentDirectory = join(currentItem.parent, currentItem.item.name);
|
|
27
|
+
queue.push(...readDirectory(parentDirectory));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return paths;
|
|
32
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
2
|
+
import { JSDOM } from 'jsdom';
|
|
3
|
+
|
|
4
|
+
export async function mergeSprites({
|
|
5
|
+
sprite1,
|
|
6
|
+
sprite2,
|
|
7
|
+
outputSprite,
|
|
8
|
+
}: {
|
|
9
|
+
sprite1: string;
|
|
10
|
+
sprite2: string;
|
|
11
|
+
outputSprite: string;
|
|
12
|
+
}) {
|
|
13
|
+
// Read the SVG files
|
|
14
|
+
const svg1 = await readFile(sprite1, 'utf-8');
|
|
15
|
+
const svg2 = await readFile(sprite2, 'utf-8');
|
|
16
|
+
|
|
17
|
+
// Parse the SVG files
|
|
18
|
+
const dom1 = new JSDOM(svg1);
|
|
19
|
+
const dom2 = new JSDOM(svg2);
|
|
20
|
+
|
|
21
|
+
// Get the <symbol> elements
|
|
22
|
+
const symbols1 = dom1.window.document.querySelectorAll('symbol');
|
|
23
|
+
const symbols2 = dom2.window.document.querySelectorAll('symbol');
|
|
24
|
+
|
|
25
|
+
// Create a new SVG document
|
|
26
|
+
const outputDom = new JSDOM('<svg xmlns="http://www.w3.org/2000/svg"></svg>');
|
|
27
|
+
const outputSvg = outputDom.window.document.querySelector('svg');
|
|
28
|
+
|
|
29
|
+
// Append the <symbol> elements from the second file to the new SVG document
|
|
30
|
+
symbols2.forEach(symbol => outputSvg?.appendChild(outputDom.window.document.importNode(symbol, true)));
|
|
31
|
+
|
|
32
|
+
// Append the <symbol> elements from the first file to the new SVG document,
|
|
33
|
+
// only if not present in the output file
|
|
34
|
+
symbols1.forEach(symbol => {
|
|
35
|
+
const id = symbol.getAttribute('id');
|
|
36
|
+
|
|
37
|
+
if (!outputSvg?.querySelector(`symbol[id="${id}"]`)) {
|
|
38
|
+
outputSvg?.appendChild(outputDom.window.document.importNode(symbol, true));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Write the new SVG document to a file
|
|
43
|
+
await writeFile(outputSprite, outputSvg?.outerHTML ?? '', 'utf-8');
|
|
44
|
+
}
|