This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
Add script to automate and standardise SVG component generation #15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ts
run throughts-node
, rather thanjs
We established that handling SVGs is one area in which we could improve developer experience and efficiency by creating a standardised, streamlined, but non-project-specific solution for a common pain point.
This PR focuses on static SVG images, like icons. These must:
<svg>
elements) and native (using components fromreact-native-svg
to use native OS elements)index.d.ts
file.Previous approach
Currently, any time an SVG icon or image is added or updated, the workflow appears to be the following (this is undocumented, I'm inferring this from the state of the files):
npx @svgr/cli <options>
with typescript options chosen to generate theindex.web.ts
file. At least some of the icon components were clearly generated by SVGR, but others look like they might have been manually created, copying the SVGR approach.index.native.ts
file.src/components/atoms/icons/common.ts
src/components/atoms/icons/
index.d.ts
from another iconsrc/components/atoms/icons/index.ts
(SVGR can generate indexes, but only injs
, not ints
)src/assets/icons
, and try to avoid these files getting out of sync with the components.It works, but it's cumbersome and allows inconsistency to creep in: even here there are already inconsistencies in naming (e.g. some match the original SVG names while some have different folder names) and in how different icons have been converted (e.g. which attributes are stripped and which elements modified). There are also some leftover SVG assets that appear to be unused and some SVG components without source files.
It would also be a pain to make general changes to how icons are used (for example applying
currentColor
orem
sizing). This would probably need to be done by hand for every asset.New approach
This PR adds a script that handles all of this.
To add one or more new SVG icons or images:
.svg
file(s) tosrc/assets/icons
orsrc/assets/images
npm run convert-svg
. This generates all the needed files, with typing, and updates the appropriateindex.ts
To update existing SVG icons or images:
.svg
file(s) insrc/assets/icons
orsrc/assets/images
scripts/convert-svg.js
(e.g. to add a prop or attribute conversion)npm run convert-svg:update
. This generates files and updates indexes as above, overwriting rather than ignoring those that were already generatedThe script also adds a comment to each generated component, saying that it was generated by this script and pointing to the asset file it was generated from, so it's easy for anyone picking up a project and looking at a particular icon or image to see what is going on. For example:
...and the console output gives a summary of what has been done:
I think this is a good example of how an "accelerator" project can streamline a common, normally-troublesome workflow.