-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NAS-130954 / 25.04 / Icon sprite (#10788)
- Loading branch information
Showing
298 changed files
with
1,543 additions
and
2,428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import fs from 'fs'; | ||
import path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { resolve } from 'path'; | ||
|
||
export function addCustomIcons(usedIcons: Set<string>): Set<string> { | ||
// TODO: Can be simplified in node 20.11+ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const customIconsPath = resolve(__dirname, '../../../src/assets/icons/custom'); | ||
|
||
const customIcons = new Set<string>(); | ||
|
||
fs.readdirSync(customIconsPath).forEach((filename) => { | ||
const icon = `ix-${filename.replace('.svg', '')}`; | ||
if (!usedIcons.has(icon)) { | ||
console.warn(`Custom icon "${icon}" does not appear to be used in the application.`); | ||
} | ||
|
||
customIcons.add(icon); | ||
}); | ||
|
||
return new Set([...customIcons, ...usedIcons]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import fs from 'fs'; | ||
import Spriter from 'svg-sprite'; | ||
import File from 'vinyl'; | ||
|
||
export type SpriteResult = Record<string, { sprite: File }>; | ||
|
||
export async function buildSprite(icons: Map<string, string>): Promise<SpriteResult> { | ||
const spriter = new Spriter({ | ||
mode: { | ||
stack: true, | ||
}, | ||
}); | ||
|
||
icons.forEach((path, name) => { | ||
try { | ||
spriter.add(name, null, fs.readFileSync(path, 'utf-8')); | ||
} catch (error) { | ||
console.error(`Failed to add icon "${name}": `); | ||
throw error; | ||
} | ||
}); | ||
|
||
const { result } = await spriter.compileAsync() as { result: SpriteResult }; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import fs from 'fs'; | ||
import * as cheerio from 'cheerio'; | ||
import glob from 'glob'; | ||
|
||
export function findIconsInTemplates(path: string): Set<string> { | ||
const iconNames = new Set<string>(); | ||
|
||
const templates = glob.sync(`${path}/**/*.html`); | ||
|
||
templates.forEach((template) => { | ||
const content = fs.readFileSync(template, 'utf-8'); | ||
const parsedTemplate = cheerio.load(content); | ||
|
||
parsedTemplate('ix-icon').each((_, iconTag) => { | ||
const name = parsedTemplate(iconTag).attr('name'); | ||
if (name) { | ||
iconNames.add(name); | ||
} | ||
}); | ||
}); | ||
|
||
return iconNames; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { execSync } from 'node:child_process'; | ||
|
||
export function findIconsWithMarker(path: string): Set<string> { | ||
const command = `grep -rEo "iconMarker\\\\('[^']+'" --include="*.ts" --include="*.html" ${path}`; | ||
|
||
const icons = new Set<string>(); | ||
const output = execSync(command, { encoding: 'utf-8' }); | ||
output | ||
.split('\n') | ||
.filter(Boolean) | ||
.forEach((line) => { | ||
const [, match] = line.split(':'); | ||
const value = match.match(/'([^']+)'/)[1]; | ||
icons.add(value); | ||
}); | ||
|
||
return icons; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function getIconPaths(names: Set<string>): Map<string, string> { | ||
const iconPaths = new Map<string, string>(); | ||
|
||
names.forEach((name) => { | ||
if (name.startsWith('ix-')) { | ||
iconPaths.set(name, `src/assets/icons/custom/${name.slice(3)}.svg`); | ||
return; | ||
} | ||
|
||
if (name.startsWith('mdi-')) { | ||
iconPaths.set(name, `node_modules/@mdi/svg/svg/${name.slice(4)}.svg`); | ||
return; | ||
} | ||
|
||
iconPaths.set(name, `node_modules/@material-design-icons/svg/filled/${name}.svg`); | ||
}); | ||
|
||
return iconPaths; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function warnAboutDuplicates(icons: Set<string>): void { | ||
icons.forEach((icon) => { | ||
if (icon.startsWith('mdi-')) { | ||
return; | ||
} | ||
|
||
if (!icons.has(`mdi-${icon}`)) { | ||
return; | ||
} | ||
|
||
console.warn(`Both "${icon}" and "mdi-${icon}" are used in the application. Consider only using the 'mdi' version.`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import fs from 'fs'; | ||
import * as path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { resolve } from 'path'; | ||
import { addCustomIcons } from './lib/add-custom-icons'; | ||
import { buildSprite } from './lib/build-sprite'; | ||
import { findIconsInTemplates } from './lib/find-icons-in-templates'; | ||
import { findIconsWithMarker } from './lib/find-icons-with-marker'; | ||
import { getIconPaths } from './lib/get-icon-paths'; | ||
import { warnAboutDuplicates } from './lib/warn-about-duplicates'; | ||
|
||
async function makeSprite(): Promise<void> { | ||
try { | ||
// TODO: Can be simplified in node 20.11+ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
const srcDir = resolve(__dirname, '../../src'); | ||
const targetPath = resolve(__dirname, '../../src/assets/icons/sprite.svg'); | ||
|
||
const templateIcons = findIconsInTemplates(srcDir); | ||
const markerIcons = findIconsWithMarker(srcDir); | ||
const usedIcons = new Set([...templateIcons, ...markerIcons]); | ||
|
||
const allIcons = addCustomIcons(usedIcons); | ||
|
||
warnAboutDuplicates(allIcons); | ||
|
||
if (!allIcons.size) { | ||
throw new Error('No icons found in the project.'); | ||
} | ||
|
||
const icons = getIconPaths(allIcons); | ||
|
||
const result = await buildSprite(icons); | ||
const file = Object.values(result)[0].sprite; | ||
|
||
const buffer = file.contents as Buffer; | ||
const size = buffer.length / 1024; | ||
|
||
fs.writeFileSync(targetPath, buffer); | ||
|
||
console.info(`Generated icon sprite with ${allIcons.size} icons (${size.toFixed(2)} KiB).`); | ||
} catch (error: unknown) { | ||
console.error('Error when building the icon sprite:'); | ||
throw error; | ||
} | ||
} | ||
|
||
makeSprite(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.