optimize.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { globSync } from 'glob'
  2. import { readFileSync, writeFileSync } from 'fs'
  3. import { join, basename } from 'path'
  4. import { optimizePath, ICONS_SRC_DIR, iconTemplate, types } from './helpers.mjs'
  5. types.forEach(type => {
  6. const files = globSync(join(ICONS_SRC_DIR, type, '*.svg'))
  7. files.forEach(function (file, i) {
  8. console.log(`Optimize ${basename(file)}`);
  9. // Read files
  10. let svgFile = readFileSync(file),
  11. svgFileContent = svgFile.toString()
  12. // Optimize SVG
  13. svgFileContent = svgFileContent.replace(/><\/(polyline|line|rect|circle|path|ellipse)>/g, '/>')
  14. .replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"')
  15. .replace(/<path stroke="red" stroke-width="\.1"([^>]+)?\/>/g, '')
  16. .replace(/<path[^>]+d="M0 0h24v24h-24z"[^>]+\/>/g, '')
  17. .replace(/\s?\/>/g, ' />')
  18. .replace(/\n\s*<(line|circle|path|polyline|rect|ellipse)/g, '\n <$1')
  19. // .replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"')
  20. .replace(/<line x1="([^"]+)" y1="([^"]+)" x2="([^"]+)" y2="([^"]+)"\s*\/>/g, function (f, x1, y1, x2, y2) {
  21. return `<path d="M${x1} ${y1}L${x2} ${y2}" />`
  22. })
  23. .replace(/<circle cx="([^"]+)" cy="([^"]+)" r="([^"]+)"\s+\/>/g, function (f, cx, cy, r) {
  24. return `<path d="M ${cx} ${cy}m -${r} 0a ${r} ${r} 0 1 0 ${r * 2} 0a ${r} ${r} 0 1 0 ${r * -2} 0" />`
  25. })
  26. .replace(/<ellipse cx="([^"]+)" cy="([^"]+)" rx="([^"]+)"\s+\/>/g, function (f, cx, cy, rx) {
  27. return `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${rx}" />`
  28. })
  29. .replace(/<ellipse cx="([^"]+)" cy="([^"]+)" rx="([^"]+)" ry="([^"]+)"\s+\/>/g, function (f, cx, cy, rx, ry) {
  30. return `<path d="M${cx} ${cy}m -${rx} 0a${rx} ${ry} 0 1 0 ${rx * 2} 0a ${rx} ${ry} 0 1 0 -${rx * 2} 0" />`
  31. })
  32. .replace(/<rect width="([^"]+)" height="([^"]+)" x="([^"]+)" y="([^"]+)" rx="([^"]+)"\s+\/>/g, function (f, width, height, x, y, rx) {
  33. return `<rect x="${x}" y="${y}" width="${width}" height="${height}" rx="${rx}" />`
  34. })
  35. .replace(/<rect x="([^"]+)" y="([^"]+)" rx="([^"]+)" width="([^"]+)" height="([^"]+)"\s+\/>/g, function (f, x, y, rx, width, height) {
  36. return `<rect x="${x}" y="${y}" width="${height}" height="${height}" rx="${rx}" />`
  37. })
  38. .replace(/<rect x="([^"]+)" y="([^"]+)" width="([^"]+)" height="([^"]+)" rx="([^"]+)"\s+\/>/g, function (f, x, y, width, height, rx) {
  39. return `<path d="M ${x} ${y}m 0 ${rx}a${rx} ${rx} 0 0 1 ${rx} ${-rx}h${width - rx * 2}a${rx} ${rx} 0 0 1 ${rx} ${rx}v${height - rx * 2}a${rx} ${rx} 0 0 1 ${-rx} ${rx}h${-width + rx * 2}a${rx} ${rx} 0 0 1 ${-rx} ${-rx}Z" />`
  40. })
  41. .replace(/<rect x="([^"]+)" y="([^"]+)" width="([^"]+)" height="([^"]+)"\s+\/>/g, function (f, x, y, width, height) {
  42. return `<path d="M ${x} ${y}h${width}v${height}h${-width}Z" />`
  43. })
  44. .replace(/<polyline points="([^"]+)\s?"\s+\/>/g, function (f, points) {
  45. const path = points.split(' ').reduce(
  46. (accumulator, currentValue, currentIndex) => `${accumulator}${currentIndex % 2 === 0 ? (currentIndex === 0 ? 'M' : 'L') : ''}${currentValue} `,
  47. ''
  48. )
  49. return `<path d="${path}" />`
  50. })
  51. .replace(/<path d="([^"]+)"/g, function (f, r1) {
  52. r1 = optimizePath(r1)
  53. return `<path d="${r1}"`
  54. })
  55. .replace(/<path\s+d="([^"]+)"/g, function (f, d) {
  56. const d2 = d
  57. .replace(/m0 0/g, (f, m) => ``)
  58. .replace(/ 0\./g, ' .')
  59. .replace(/ -0\./g, ' -.')
  60. .replace(/([amcvhslAMCVHLS]) /g, '$1')
  61. return `<path d="${d2}"`
  62. })
  63. .replace(/d="m/g, 'd="M')
  64. .replace(/([Aa])\s?([0-9.]+)[\s,]([0-9.]+)[\s,]([0-9.]+)[\s,]?([0-1])[\s,]?([0-1])[\s,]?(-?[0-9.]+)[\s,]?(-?[0-9.]+)/gi, '$1$2 $3 $4 $5 $6 $7 $8')
  65. .replace(/\n\s+\n+/g, '\n')
  66. // Add icon template
  67. svgFileContent = svgFileContent.replace(/<svg[^>]+>/, iconTemplate(type))
  68. // Remove stroke and fill
  69. if (file.match(/\/filled\//)) {
  70. svgFileContent = svgFileContent
  71. .replace(/stroke-width="0" fill="currentColor"/gm, '')
  72. .replace('stroke-width="2"', '')
  73. .replace('stroke-linecap="round"', '')
  74. .replace('stroke-linejoin="round"', '')
  75. .replace('stroke="currentColor"', '')
  76. .replace('fill="none"', 'fill="currentColor"')
  77. .replace(/^\s*[\r\n]/gm, '')
  78. .replace(/\s{2,}\//g, ' /')
  79. }
  80. // Add comment if not exists
  81. if (!svgFileContent.includes('<!--')) {
  82. svgFileContent = '<!--\n-->\n' + svgFileContent
  83. }
  84. svgFileContent = svgFileContent.replace(/tags: \[([^]+)\]/, (m, tags) => {
  85. tags = [...new Set(tags.split(',').map(t => t.trim()))].filter(t => t).join(', ')
  86. return `tags: [${tags}]`
  87. })
  88. // Write files
  89. if (svgFile.toString() !== svgFileContent) {
  90. writeFileSync(file, svgFileContent)
  91. }
  92. })
  93. })