build.mjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import fs from 'fs'
  2. import { createDirectory, readSvgs } from '../../.build/helpers.mjs'
  3. import { buildIcons } from '../../.build/build-icons.mjs'
  4. import { stringify } from 'svgson'
  5. const svgFiles = readSvgs()
  6. const buildSprite = () => {
  7. let svgContent = ''
  8. svgFiles.forEach(function(file, i) {
  9. const svgFileContent = file.contents.replace(/<svg[^>]+>/g, '').replace(/<\/svg>/g, '').replace(/\n+/g, '').replace(/>\s+</g, '><').trim()
  10. 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>`
  11. })
  12. let svg = `<svg xmlns="http://www.w3.org/2000/svg"><defs>${svgContent}</defs></svg>`
  13. fs.writeFileSync('tabler-sprite.svg', svg)
  14. fs.writeFileSync('tabler-sprite-nostroke.svg', svg.replace(/stroke-width="2"\s/g, ''))
  15. }
  16. const buildNodes = () => {
  17. const iconNodes = svgFiles.reduce((acc, { name, obj }) => {
  18. acc[name] = obj.children.map(({ name, attributes }) => [name, attributes]);
  19. return acc;
  20. }, {});
  21. const iconNodesStringified = JSON.stringify(iconNodes, null, 2);
  22. fs.writeFileSync(`./tabler-nodes.json`, iconNodesStringified);
  23. }
  24. const componentTemplate = ({
  25. namePascal,
  26. svg
  27. }) => `\
  28. export default ${namePascal} => \`${svg.contents}\`;`;
  29. const indexItemTemplate = ({
  30. name,
  31. namePascal
  32. }) => `export { default as ${namePascal} } from './icons/${name}';`
  33. const typeDefinitionsTemplate = () => `// Generated icons`
  34. const indexTypeTemplate = ({
  35. namePascal
  36. }) => `export declare const ${namePascal}: string;`
  37. buildSprite()
  38. buildNodes()
  39. buildIcons({
  40. name: 'icons',
  41. componentTemplate,
  42. indexItemTemplate,
  43. typeDefinitionsTemplate,
  44. indexTypeTemplate,
  45. pretty: false
  46. })