|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import mkdirp from 'mkdirp' |
| 4 | +import rimraf from 'rimraf' |
| 5 | +import svgson, { stringify } from 'svgson' |
| 6 | +import Svgo from 'svgo' |
| 7 | +const svgo = new Svgo({ |
| 8 | + multipass: true, |
| 9 | + removeViewBox: false, |
| 10 | + floatPrecision: 2 |
| 11 | +}) |
| 12 | + |
| 13 | +let icons = 0 |
| 14 | +const RAW_DIR = path.resolve(__dirname, '../raw/') |
| 15 | +const SVG_DIR = path.resolve(__dirname, '../svg/') |
| 16 | +const ICON_PATTERN = /^(.+)\.svg$/ |
| 17 | + |
| 18 | +rimraf.sync(SVG_DIR) |
| 19 | +mkdirp.sync(SVG_DIR) |
| 20 | + |
| 21 | +Promise.all( |
| 22 | + fs.readdirSync(RAW_DIR).map(async file => { |
| 23 | + if (!ICON_PATTERN.test(file)) { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + let fileData = fs.readFileSync(path.resolve(RAW_DIR, file), 'utf8') |
| 28 | + let { error, data } = await svgo.optimize(fileData) |
| 29 | + if (error) { |
| 30 | + console.error(file, error) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + let el = await svgson(data) |
| 35 | + console.log(`Normalizing ${file}...`) |
| 36 | + let { attributes } = el |
| 37 | + let { width, height, viewBox } = attributes |
| 38 | + if (!(width && height)) { |
| 39 | + if (!viewBox) { |
| 40 | + console.error(file, `doesn't contain a valid size declaration.`) |
| 41 | + console.error(width, height, viewBox) |
| 42 | + } |
| 43 | + |
| 44 | + ;[, width, height] = (viewBox.match(/0 0 (\d+) (\d+)/) || []).map(size => |
| 45 | + parseInt(size, 10) |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + if (!(width && height)) { |
| 50 | + console.error(file, `doesn't contain a valid size declaration.`) |
| 51 | + console.error(width, height, viewBox) |
| 52 | + } |
| 53 | + |
| 54 | + if (!viewBox) { |
| 55 | + attributes.viewBox = `0 0 ${width} ${height}` |
| 56 | + } |
| 57 | + |
| 58 | + walkElement(el, { |
| 59 | + enter (node) { |
| 60 | + let { attributes } = node |
| 61 | + |
| 62 | + delete attributes.class |
| 63 | + |
| 64 | + let ctxFill = (getContextAttr(node, 'fill') || '').toLowerCase() |
| 65 | + let ctxStroke = (getContextAttr(node, 'stroke') || '').toLowerCase() |
| 66 | + let attrFill = (attributes.fill || '').toLowerCase() |
| 67 | + let attrStroke = (attributes.stroke || '').toLowerCase() |
| 68 | + |
| 69 | + if (attrFill) { |
| 70 | + if (!ctxFill) { |
| 71 | + if (attrFill !== 'none') { |
| 72 | + attributes.fill = 'currentColor' |
| 73 | + console.log(` fill: ${attrFill} -> currentColor`) |
| 74 | + } |
| 75 | + } else { |
| 76 | + if (attrFill === ctxFill) { |
| 77 | + delete attributes.fill |
| 78 | + console.log(` fill: ${attrFill} -> / (same as context)`) |
| 79 | + } else if (attrFill !== 'none') { |
| 80 | + attributes.fill = 'currentColor' |
| 81 | + console.log( |
| 82 | + ` fill: ${attrFill} -> currentColor (different from context)` |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (attrStroke) { |
| 89 | + if (!ctxStroke) { |
| 90 | + if (attrStroke !== 'none') { |
| 91 | + attributes.stroke = 'currentColor' |
| 92 | + console.log(` stroke: ${attrStroke} -> currentColor`) |
| 93 | + } else { |
| 94 | + delete attributes.stroke |
| 95 | + console.log(` stroke: ${attrStroke} -> / (same as default)`) |
| 96 | + } |
| 97 | + } else { |
| 98 | + if (attrStroke && attrStroke === ctxStroke) { |
| 99 | + delete attributes.stroke |
| 100 | + console.log(` stroke: ${attrStroke} -> / (same as context)`) |
| 101 | + } else if (attrStroke !== 'none') { |
| 102 | + attributes.stroke = 'currentColor' |
| 103 | + console.log( |
| 104 | + ` stroke: ${attrStroke} -> currentColor (different from context)` |
| 105 | + ) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + fs.writeFileSync(path.join(SVG_DIR, file), stringify(el), 'utf8') |
| 113 | + icons++ |
| 114 | + }) |
| 115 | +).then(() => { |
| 116 | + console.log(`Normalized ${icons} icons.`) |
| 117 | +}) |
| 118 | + |
| 119 | +function walkElement (el, { enter, leave }) { |
| 120 | + if (typeof enter === 'function') { |
| 121 | + enter(el) |
| 122 | + } |
| 123 | + if (el.children && el.children.length) { |
| 124 | + el.children.forEach(child => { |
| 125 | + child.parentNode = el |
| 126 | + walkElement(child, { enter, leave }) |
| 127 | + delete child.parentNode |
| 128 | + }) |
| 129 | + } |
| 130 | + if (typeof leave === 'function') { |
| 131 | + leave(el) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +function getContextAttr (el, attr) { |
| 136 | + let node = el.parentNode |
| 137 | + while (node) { |
| 138 | + if (node.attributes && node.attributes[attr]) { |
| 139 | + return node.attributes[attr] |
| 140 | + } |
| 141 | + |
| 142 | + node = node.parentNode |
| 143 | + } |
| 144 | + return null |
| 145 | +} |
0 commit comments