build.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { fileURLToPath } from 'url'
  4. import { parseSync } from 'svgson'
  5. const svgFiles = fs.readdirSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), './svg'))
  6. .filter((file) => path.extname(file) === '.svg')
  7. .map(svgFile => {
  8. const name = path.basename(svgFile, '.svg'),
  9. contents = fs.readFileSync(path.join(path.resolve(path.dirname(fileURLToPath(import.meta.url)), './svg'), svgFile), 'utf-8').trim().replace('<path stroke="none" d="M0 0h24v24H0z" fill="none"/>', ''),
  10. obj = parseSync(contents.replace('<path stroke="none" d="M0 0h24v24H0z" fill="none"/>', ''));
  11. return {
  12. name,
  13. contents,
  14. obj
  15. };
  16. });
  17. // Build sprites
  18. (() => {
  19. let svgContent = ''
  20. svgFiles.forEach(function(file, i) {
  21. const svgFileContent = file.contents.replace(/<svg[^>]+>/g, '').replace(/<\/svg>/g, '').replace(/\n+/g, '').replace(/>\s+</g, '><').trim()
  22. svgContent += `<symbol id="tabler-${file.name}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${svgFileContent}</symbol>`
  23. })
  24. let svg = `<svg xmlns="http://www.w3.org/2000/svg"><defs>${svgContent}</defs></svg>`
  25. fs.writeFileSync('./src/tabler-sprite.svg', svg)
  26. fs.writeFileSync('./src/tabler-sprite-nostroke.svg', svg.replace(/stroke-width="2"\s/g, ''))
  27. })();
  28. // Build nodes
  29. (() => {
  30. const iconNodes = svgFiles.reduce((acc, { name, obj }) => {
  31. acc[name] = obj.children.map(({ name, attributes }) => [name, attributes]);
  32. return acc;
  33. }, {});
  34. const iconNodesStringified = JSON.stringify(iconNodes, null, 2);
  35. fs.writeFileSync(`./src/tabler-nodes.json`, iconNodesStringified);
  36. })();
  37. // Build contents
  38. (() => {
  39. const iconContents = svgFiles.reduce((acc, { name, contents }) => {
  40. var lines = contents.split('\n');
  41. lines.splice(0,1);
  42. var trimmedContent = lines.join('\n').replace('</svg>', '').replace(/(\r\n|\n|\r)/gm, '').replace(/(\s){2,}/g, '');
  43. acc[name] = trimmedContent;
  44. return acc;
  45. }, {});
  46. const iconContentsStringified = JSON.stringify(iconContents, null, 2);
  47. fs.writeFileSync(`./src/tabler-contents.json`, iconContentsStringified);
  48. })();