-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from nota/react-component
SVGからReact Componentを生成してパッケージに含める
- Loading branch information
Showing
8 changed files
with
3,996 additions
and
1,620 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,6 @@ typings/ | |
.dynamodb/ | ||
|
||
.DS_Store | ||
|
||
dist/ | ||
src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
example/ | ||
src/ | ||
generate-react-component.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import path from 'path'; | ||
import {readdir, readFile, writeFile} from 'fs/promises'; | ||
import pascalcase from 'pascalcase'; | ||
import { optimize } from 'svgo' | ||
|
||
const __dirname = import.meta.dirname | ||
|
||
const svgs = (await readdir(path.join(__dirname, './svg'))).filter(file => file.endsWith('.svg')); | ||
|
||
await Promise.all((svgs).map(async (file) => { | ||
const svg = await readFile(path.join(__dirname, './svg', file), 'utf8'); | ||
const optimizedSvg = optimize(svg, { | ||
plugins: [ | ||
'removeTitle', | ||
{name: 'removeAttrs', params: {attrs: 'fill'}},] | ||
}).data; | ||
const name = path.basename(file, '.svg'); | ||
await writeFile( | ||
path.join(__dirname, './src/react', `${pascalcase(name)}.tsx`), | ||
`import React from 'react'; | ||
function ${pascalcase(name)} (props: React.HTMLProps<SVGSVGElement>): React.ReactElement { | ||
return ( | ||
${optimizedSvg.replace(/<svg(.*?)>/, `<svg $1 {...props}>`)} | ||
); | ||
} | ||
export {${pascalcase(name)}}; | ||
` | ||
); | ||
})); | ||
|
||
await writeFile(path.join(__dirname, './src/react/index.ts'), svgs.map(file => { | ||
const name = path.basename(file, '.svg'); | ||
return `export {${pascalcase(name)}} from './${pascalcase(name)}';` | ||
}).join('\n')); |
Oops, something went wrong.