optimize.mjs 3.8 KB

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