Skip to content

Instantly share code, notes, and snippets.

@YousraBD
Last active July 2, 2021 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YousraBD/d024edb307b02ecbe042381d2024fd19 to your computer and use it in GitHub Desktop.
Save YousraBD/d024edb307b02ecbe042381d2024fd19 to your computer and use it in GitHub Desktop.
{
"scripts": {
"clean": "rimraf outline solid package *.tgz",
"export": "figma-export use-config figma.config.js",
"test": "node test.js",
"build": "run-s clean export",
"np": "np",
"release": "run-s build np"
}
}
const outputters = [
// ...
require("@figma-export/output-components-as-svgstore")({
getIconId: ({ componentName }) => componentName.toLowerCase(),
output: "./sprite",
}),
];
<svg>
<use xlink:href="path/to/sprite.svg#our-icon"/>
</svg>
{
"scripts": {
"export": "figma-export use-config figma.config.js"
}
}
// figma.config.js
require("dotenv").config();const fileId = process.env.FILE_ID;
const outputters = [
require("@figma-export/output-components-as-svg")({ output: "./" })
];/** @type {import('@figma-export/types').FigmaExportRC} */
module.exports = {
commands: [
[ "components", {
fileId,
onlyFromPages: ["solid"],
outputters,
},
],
[ "components", {
fileId,
onlyFromPages: ["outline"],
outputters,
},
],
],
};
$ npm install --save-dev dotenv @figma-export/cli @figma-export/output-components-as-svg
$ npm i --save-dev @figma-export/output-components-as-svgr
# .env
FIGMA_TOKEN=180901-363fe5d2-f0c2-45a9-b564-d49c708281ea # use your own
FILE_ID=V7WbFH5FKFahwtiqWdxzoO
{
"files": [
"outline/",
"solid/"
]
}
# build output
# dependencies
/node_modules
# build
/outline
/solid
# misc
.DS_Store
.npm
.eslintcache
.yarn-integrity
# env files
.env
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Output of 'npm pack'
*.tgz
/package
$ git add . && git commit -m 'initial commit'
$ git remote add origin git@github.com:$your_username/$github_repo.git
$ git branch -M main
$ git push -u origin main
import { HouseSolid, GlobeOutline } from 'iconlib'
// ./index.js
export * from "./src/solid";
export * from "./src/outline";
$ npm i --save-dev @figma-export/output-components-as-svgstore
{
"jsdelivr": "sprite/outline.svg",
"cdn": "sprite/outline.svg",
"unpkg": "sprite/outline.svg"
}
{
"main": "react/index.js"
}
$ npm publish --access public
$ npm i --save-dev np
$ FIGMA_TOKEN=180901-363fe5d2-f0c2-45a9-b564-d49c708281ea npx -p @figma-export/cli -p @figma-export/output-components-as-svg figma-export components V7WbFH5FKFahwtiqWdxzoO -O @figma-export/output-components-as-svg
$ npm i --save-dev @figma-export/transform-svg-with-svgo
output
├── outline
│ ├── Circle.svg
│ ├── Square.svg
│ └── S.svg
└── solid
├── Circle.svg
├── Square.svg
└── S.svg
const outputters = [
require("@figma-export/output-components-as-svg")({ output: "./" }),
require("@figma-export/output-components-as-svgr")({
output: "./src",
}),
];
{
"name": "$package_name",
"version": "0.0.0",
"description": "$package_description",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "$your_username <$your_email> ($your_website)",
"license": "$license"
}
$ mkdir $package_name && cd $package_name
{
"homepage": "https://github.com/$your_username/$github_repo#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/$your_username/$github_repo.git"
},
"bugs": {
"url": "https://github.com/$your_username/$github_repo/issues"
},
"keywords": [
"icons",
"svg",
"react"
]
}
// package.json
"files": [
"outline/",
"solid/",
"react/"
],
"peerDependencies": {
"react": ">= 16"
}
// package.json
"scripts": {
"clean": "rimraf outline solid react src package *.tgz"
}
# .gitignore
# build
/outline
/solid
/react
/src
# .gitignore
# build
/outline
/solid
/react
/src
/sprite
// package.json
"files": [
"outline/",
"solid/",
"react/",
"sprite/"
]
// package.json
"scripts": {
"clean": "rimraf outline solid react src sprite package *.tgz"
}
$ tar zxvf $package_name-0.0.0.tgz
// tsconfig.json
{
"compilerOptions": {
"jsx": "react",
"strict": true,
"noImplicitAny": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"outDir": "react"
}
}
// rollup.config.js
import typescript from "@rollup/plugin-typescript";
const config = [
{
input: "index.js",
output: {
dir: "react",
format: "module",
},
plugins: [typescript()],
},
];
export default config;
{
"scripts": {
"bundle": "rollup --config",
"build": "run-s clean export bundle",
}
}
<!-- before -->
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<path stroke="#000" d="M.5.5h15v15H.5V.5z"/>
<path stroke="#000" d="M6 10V6h4v4H6z"/>
</svg>
<!-- after -->
<svg stroke="#000" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">
<path d="M.5.5h15v15H.5V.5z"/>
<path d="M6 10V6h4v4H6z"/>
</svg>
$ npm i --save-dev rimraf
{
"scripts": {
"clean": "rimraf outline solid package *.tgz",
"export": "figma-export use-config figma.config.js"
}
}
$ npm run release
$ npm i --save-dev npm-run-all
{
"scripts": {
"clean": "rimraf outline solid package *.tgz",
"export": "figma-export use-config figma.config.js",
"build": "run-s clean export"
}
}
// figma.config.js
require("dotenv").config();
const svgo = require('@figma-export/transform-svg-with-svgo')
const fileId = process.env.FILE_ID;
const outputters = [
require("@figma-export/output-components-as-svg")({ output: "./" })
];
/** @type {import('svgo').PluginConfig[]} */
const solidSVGOConfig = [
{ removeDimensions: true },
{ sortAttrs: true },
{ removeAttrs: { attrs: "fill" } },
{ addAttributesToSVGElement: { attribute: { fill: "currentColor" } } },
];
/** @type {import('svgo').PluginConfig[]} */
const outlineSVGOConfig = [
{ removeDimensions: true },
{ sortAttrs: true },
{ removeAttrs: { attrs: "stroke" } },
{ addAttributesToSVGElement: { attribute: { stroke: "currentColor" } } },
];
/** @type {import('@figma-export/types').FigmaExportRC} */
module.exports = {
commands: [
["components", {
fileId,
onlyFromPages: ["solid"],
transformers: [svgo({ multipass: true, plugins: solidSVGOConfig })],
outputters,
},
],
["components", {
fileId,
onlyFromPages: ["outline"],
transformers: [svgo({ multipass: true, plugins: outlineSVGOConfig })],
outputters,
},
],
],
};
{
"scripts": {
"clean": "rimraf outline solid package *.tgz",
"export": "figma-export use-config figma.config.js",
"test": "node test.js",
"build": "run-s clean export"
}
}
const fs = require("fs");
const $ = require("cheerio");
const directories = ["outline", "solid"];
let errors = 0;
directories.forEach((dir) =>
fs.readdirSync(dir).forEach((file) => {
const viewBox = $.load(fs.readFileSync(`${dir}/${file}`))("svg").attr(
"viewBox"
);
if (viewBox !== "0 0 16 16") {
console.error(
`Error: \`${dir}/${file}\` has a viewBox of \x1b[31m\`${viewBox}\`\x1b[0m`
);
errors++;
}
})
);
if (errors > 0) {
process.exit(1);
} else {
console.log("Tests passed!");
}
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
const fileId = process.env.FILE_ID;
const outputters = [
require("@figma-export/output-components-as-svg")({ output: "./" }),
require("@figma-export/output-components-as-svgr")({
getFileExtension: () => ".tsx",
getComponentName: ({ componentName, pageName }) =>
componentName + capitalize(pageName),
getSvgrConfig: () => ({ typescript: true }),
output: "./src",
}),
];
$ npm i --save-dev react rollup typescript @rollup/plugin-typescript @types/react
$ npm i --save-dev cheerio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment