optimize.mjs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { glob } from 'glob'
  2. import { readFileSync, writeFileSync } from 'fs'
  3. import { join, basename } from 'path'
  4. import { optimizePath, ICONS_SRC_DIR } from './helpers.mjs'
  5. glob(join(ICONS_SRC_DIR, '*.svg'), {}, function(er, files) {
  6. files.forEach(function(file, i) {
  7. console.log(`Optimize ${basename(file)}`);
  8. let svgFile = readFileSync(file),
  9. svgFileContent = svgFile.toString()
  10. svgFileContent = svgFileContent.replace(/><\/(polyline|line|rect|circle|path|ellipse)>/g, '/>')
  11. .replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"')
  12. .replace(/<path stroke="red" stroke-width="\.1"([^>]+)?\/>/g, '')
  13. .replace(/<path[^>]+d="M0 0h24v24h-24z"[^>]+\/>/g, '')
  14. .replace(/\s?\/>/g, ' />')
  15. .replace(/\n\s*<(line|circle|path|polyline|rect|ellipse)/g, '\n <$1')
  16. // .replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"')
  17. .replace(/<line x1="([^"]+)" y1="([^"]+)" x2="([^"]+)" y2="([^"]+)"\s*\/>/g, function(f, x1, y1, x2, y2) {
  18. return `<path d="M${x1} ${y1}L${x2} ${y2}" />`
  19. })
  20. .replace(/<circle cx="([^"]+)" cy="([^"]+)" r="([^"]+)"\s+\/>/g, function(f, cx, cy, r) {
  21. 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" />`
  22. })
  23. .replace(/<ellipse cx="([^"]+)" cy="([^"]+)" rx="([^"]+)"\s+\/>/g, function(f, cx, cy, rx) {
  24. return `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${rx}" />`
  25. })
  26. .replace(/<ellipse cx="([^"]+)" cy="([^"]+)" rx="([^"]+)" ry="([^"]+)"\s+\/>/g, function(f, cx, cy, rx, ry) {
  27. 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" />`
  28. })
  29. .replace(/<rect width="([^"]+)" height="([^"]+)" x="([^"]+)" y="([^"]+)" rx="([^"]+)"\s+\/>/g, function(f, width, height, x, y, rx) {
  30. return `<rect x="${x}" y="${y}" width="${height}" height="${height}" rx="${rx}" />`
  31. })
  32. .replace(/<rect x="([^"]+)" y="([^"]+)" rx="([^"]+)" width="([^"]+)" height="([^"]+)"\s+\/>/g, function(f, x, y, rx, width, height) {
  33. return `<rect x="${x}" y="${y}" width="${height}" height="${height}" rx="${rx}" />`
  34. })
  35. .replace(/<rect x="([^"]+)" y="([^"]+)" width="([^"]+)" height="([^"]+)" rx="([^"]+)"\s+\/>/g, function(f, x, y, width, height, rx) {
  36. 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" />`
  37. })
  38. .replace(/<rect x="([^"]+)" y="([^"]+)" width="([^"]+)" height="([^"]+)"\s+\/>/g, function(f, x, y, width, height) {
  39. return `<path d="M ${x} ${y}h${width}v${height}h${-width}Z" />`
  40. })
  41. .replace(/<polyline points="([^"]+)\s?"\s+\/>/g, function(f, points) {
  42. const path = points.split(' ').reduce(
  43. (accumulator, currentValue, currentIndex) => `${accumulator}${currentIndex % 2 === 0 ? (currentIndex === 0 ? 'M' : 'L') : ''}${currentValue} `,
  44. ''
  45. )
  46. return `<path d="${path}" />`
  47. })
  48. .replace(/<path d="([^"]+)"/g, function(f, r1) {
  49. r1 = optimizePath(r1)
  50. return `<path d="${r1}"`
  51. })
  52. .replace(/<path\s+d="([^"]+)"/g, function(f, d) {
  53. const d2 = d
  54. .replace(/m0 0/g, (f, m) => ``)
  55. .replace(/ 0\./g, ' .')
  56. .replace(/ -0\./g, ' -.')
  57. .replace(/([amcvhslAMCVHLS]) /g, '$1')
  58. return `<path d="${d2}"`
  59. })
  60. .replace(/d="m/g, 'd="M')
  61. .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')
  62. .replace(/\n\s+\n+/g, '\n')
  63. if (svgFile.toString() !== svgFileContent) {
  64. writeFileSync(file, svgFileContent)
  65. }
  66. })
  67. })