Skip to content

Commit

Permalink
perf: speed up build-fonts script (#11196)
Browse files Browse the repository at this point in the history
**Related Issue:** N/A

## Summary

This significantly improves performance by switching the script to a
Node-based implementation.

#10996
unintentionally introduced significant slowdown on macOS when running
builds at the monorepo level or within the `calcite-ui-icons` package.
  • Loading branch information
jcfranco authored Jan 3, 2025
1 parent d2ced69 commit c7e3071
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 36 deletions.
78 changes: 78 additions & 0 deletions packages/calcite-ui-icons/bin/build-fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env node
const { execSync } = require('child_process');
const {
readdirSync,
mkdirSync,
rmSync,
existsSync,
lstatSync,
symlinkSync,
unlinkSync,
readFileSync,
writeFileSync,
} = require('fs');
const { join, basename } = require('path');
const prettier = require('@prettier/sync');

const SCRIPT_DIR = __dirname;
const PACKAGE_ROOT = join(SCRIPT_DIR, '..');
const fontsDir = join(PACKAGE_ROOT, 'fonts/icons');
const iconsDir = join(PACKAGE_ROOT, 'icons');

const sizes = [16, 24, 32];
const ensureDir = (dir) => mkdirSync(dir, { recursive: true });
const clearAndPrepareDirs = () => {
if (existsSync(fontsDir)) {
rmSync(fontsDir, { recursive: true, force: true });
}
sizes.forEach((size) => ensureDir(join(fontsDir, size.toString())));
};

const filterIcons = () =>
readdirSync(iconsDir).filter((file) => {
const filePath = join(iconsDir, file);
return file.endsWith('.svg') && !readFileSync(filePath, 'utf8').includes('opacity');
});

const createLinks = (icons) => {
icons.forEach((icon) => {
const src = join(iconsDir, icon);
const dest = join(fontsDir, basename(icon));
symlinkSync(src, dest);
});
};

const organizeIconsBySize = (size) => {
const sizeDir = join(fontsDir, size.toString());
readdirSync(fontsDir).forEach((file) => {
if (file.includes(`-${size}`)) {
const src = join(fontsDir, file);
const dest = join(sizeDir, file.replace(`-${size}`, ''));
if (lstatSync(src).isSymbolicLink()) {
if (existsSync(dest)) unlinkSync(dest);
symlinkSync(src, dest);
}
}
});
};

const generateFonts = (size) =>
execSync(`fantasticon fonts/icons/${size}/ -n calcite-ui-icons-${size} --normalize true -t ttf -g json -o fonts/`, {
stdio: 'inherit',
});

const updateConfig = () => {
const codepoints = JSON.parse(readFileSync(join(PACKAGE_ROOT, 'fonts/calcite-ui-icons-16.json'), 'utf8'));
const config = prettier.format(JSON.stringify({ codepoints }), { parser: 'json' });
writeFileSync(join(PACKAGE_ROOT, 'fantasticonrc.json'), config);
};

const main = () => {
clearAndPrepareDirs();
createLinks(filterIcons());
sizes.forEach(organizeIconsBySize);
sizes.forEach(generateFonts);
updateConfig();
};

main();
35 changes: 0 additions & 35 deletions packages/calcite-ui-icons/bin/build-fonts.sh

This file was deleted.

2 changes: 1 addition & 1 deletion packages/calcite-ui-icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"scripts": {
"build": "node bin/cli.js",
"postbuild": "npm run build:sprite && npm run build:fonts",
"build:fonts": "bin/build-fonts.sh || true",
"build:fonts": "bin/build-fonts.js || true",
"build:sprite": "concurrently npm:build:sprite-*",
"build:sprite-16": "svgstore -o sprite-16.svg icons/*-16*.svg",
"build:sprite-24": "svgstore -o sprite-24.svg icons/*-24*.svg",
Expand Down

0 comments on commit c7e3071

Please sign in to comment.