Skip to content

Commit

Permalink
refactor(icons): port to ES module, formating.
Browse files Browse the repository at this point in the history
Add changeset to trigger release
  • Loading branch information
PaulHax committed Dec 1, 2023
1 parent 7fe28eb commit 7475d70
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-terms-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itk-viewer/icons': patch
---

Add @itk-viewer/icons package.
1 change: 0 additions & 1 deletion packages/icons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ revision `f6b4fa13c8`, before they were extracted.

## Icon Renderings


<table>
<tr>
<th>annotationsIconDataUri</th>
Expand Down
1 change: 0 additions & 1 deletion packages/icons/README.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ revision `f6b4fa13c8`, before they were extracted.

## Icon Renderings


56 changes: 0 additions & 56 deletions packages/icons/build.js

This file was deleted.

61 changes: 61 additions & 0 deletions packages/icons/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import glob from 'glob';
import svgToMiniDataURI from 'mini-svg-data-uri';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

try {
fs.mkdirSync('dist');
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}

const svgFiles = glob.sync(path.join('src', '*.svg'));
const icons = new Map();
svgFiles.forEach((svgFile) => {
const contents = fs.readFileSync(svgFile, { encoding: 'utf8', flag: 'r' });
const optimizedSVGDataURI = svgToMiniDataURI(contents);
const moduleContents = `const optimizedSVGDataUri = "${optimizedSVGDataURI}"
export default optimizedSVGDataUri
`;
const basename = path.basename(svgFile, '.svg');
icons.set(`${basename}IconDataUri`, {
modulePath: `./${basename}.svg.uri.js`,
svgFile,
});
fs.writeFileSync(path.join('dist', `${basename}.svg.uri.js`), moduleContents);
});

const indexFile = path.join('dist', 'index.js');
const indexFD = fs.openSync(indexFile, 'w');
for (const [varName, { modulePath }] of icons) {
fs.writeSync(
indexFD,
`export { default as ${varName} } from "${modulePath}"\n`,
);
}
fs.closeSync(indexFD);

const readmeContents = fs.readFileSync(path.join(__dirname, 'README.md.in'), {
encoding: 'utf8',
flag: 'r',
});
const readmeFD = fs.openSync(path.join(__dirname, 'README.md'), 'w');
fs.writeSync(readmeFD, readmeContents);

fs.writeSync(readmeFD, '<table>\n');
for (const [varName, { svgFile }] of icons) {
fs.writeSync(readmeFD, ' <tr>\n');
fs.writeSync(readmeFD, ` <th>${varName}</th>\n`);
fs.writeSync(
readmeFD,
` <th><img src="${svgFile}" width="40" alt="${varName}"/></th>\n`,
);
fs.writeSync(readmeFD, ' </tr>\n');
}
fs.writeSync(readmeFD, '</table>\n');
2 changes: 1 addition & 1 deletion packages/icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "UI icons for itk-viewer",
"module": "dist/index.js",
"scripts": {
"build": "node build.js",
"build": "node build.mjs",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7475d70

Please sign in to comment.