-
-
Save ashimon83/095f50aa01e47e9a7b6e5efef0c16270 to your computer and use it in GitHub Desktop.
svgr custom index-template.tsx
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
const path = require('path') | |
function defaultIndexTemplate(filePaths) { | |
// named exportの定義 | |
const exportEntries = filePaths.map((filePath) => { | |
const basename = path.basename(filePath, path.extname(filePath)) | |
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename | |
return ` | |
import Icon${exportName} from './${basename}' | |
export { Icon${exportName} }` | |
}) | |
// StorybookでIconを一覧表示するため | |
const allIconsStory = filePaths.map((filePath) => { | |
const basename = path.basename(filePath, path.extname(filePath)) | |
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename | |
return ` <HStack><Icon${exportName} width={20} height={20} {...args} /><span>${exportName}</span></HStack>` | |
}) | |
// ここから左記が実際ファイルとして出力される部分。上記で処理した文字列を入れる | |
return ` | |
import { Story } from '@storybook/react/types-6-0' | |
import React from 'react' | |
import { colors, Colors } from '../../styles/colors' | |
import { VStack, HStack } from '../Stack' | |
${exportEntries.join('\n')} | |
// for storybook | |
export default { | |
title: 'Icons', | |
args: { | |
color: colors.blueBase, | |
}, | |
} | |
type AllIconsProps = { | |
color: Colors, | |
} | |
export const AllIcons: Story<AllIconsProps> = (args) => { | |
return ( | |
<VStack> | |
${allIconsStory.join('\n')} | |
</VStack> | |
) | |
} | |
` | |
} | |
module.exports = defaultIndexTemplate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment