Skip to content

Instantly share code, notes, and snippets.

@ashimon83
Last active November 30, 2021 14:56
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 ashimon83/095f50aa01e47e9a7b6e5efef0c16270 to your computer and use it in GitHub Desktop.
Save ashimon83/095f50aa01e47e9a7b6e5efef0c16270 to your computer and use it in GitHub Desktop.
svgr custom index-template.tsx
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