Skip to content

Commit 82928f0

Browse files
authored
docs: 858 Index template docs improvements (#872)
1 parent a6699e7 commit 82928f0

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

website/pages/docs/cli.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Advanced use cases could lead you to customize the index template. The `--index-
9999
const path = require('path')
100100

101101
function defaultIndexTemplate(filePaths) {
102-
const exportEntries = filePaths.map(({ path: filePath }) => {
102+
const exportEntries = filePaths.map(({ path: filePath, originalPath }) => {
103103
const basename = path.basename(filePath, path.extname(filePath))
104104
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename
105105
return `export { default as ${exportName} } from './${basename}'`

website/pages/docs/custom-templates.mdx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ module.exports = {
9999

100100
When you use the CLI with `--out-dir` option, an index file is automatically generated.
101101

102-
The customization is the same, a file that exports a function:
102+
The customization is the same, a file that exports a function. The function receives an argument that is an array containing objects made of the original file path and the path of the file containing the generated React component:
103+
104+
- `originalPath`: the original file's path
105+
- `path`: the React component's file's path
106+
107+
Default template only uses `path` and it is similar to this:
103108

104109
```js
105110
const path = require('path')
@@ -116,6 +121,37 @@ function defaultIndexTemplate(filePaths) {
116121
module.exports = defaultIndexTemplate
117122
```
118123

124+
but you could implement a more advanced template exploiting the original file name too:
125+
126+
```js
127+
const path = require('path')
128+
129+
function defaultIndexTemplate(filePaths) {
130+
const entries = filePaths.map(({ path: filePath, originalPath }) => {
131+
const originalFileName = path.basename(
132+
originalPath,
133+
path.extname(originalPath),
134+
)
135+
const basename = path.basename(filePath, path.extname(filePath))
136+
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename
137+
const importLine = `import ${exportName} from './${basename}';`
138+
const mapLine = `${
139+
/.*[.-].*/.test(originalFileName)
140+
? `'${originalFileName}'`
141+
: originalFileName
142+
}: ${exportName}`
143+
return { importLine, mapLine }
144+
})
145+
return `${entries.map(({ importLine }) => importLine).join('\n')}
146+
export const map = {
147+
${entries.map(({ mapLine }) => mapLine).join(',\n')}
148+
}
149+
`
150+
}
151+
152+
module.exports = defaultIndexTemplate
153+
```
154+
119155
### Use with CLI
120156

121157
You can use component template in the CLI:

0 commit comments

Comments
 (0)