Skip to content

Commit 003009c

Browse files
authored
feat: make index template more flexible (#861)
BREAKING CHANGE: index template now receives an array of objects containing both the created component path (`path`) and the original SVG path (`originalPath`)
1 parent fd27c1c commit 003009c

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

__fixtures__/custom-index-template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path')
22

33
function indexTemplate(files) {
4-
const exportEntries = files.map(file => {
4+
const exportEntries = files.map(({path: file}) => {
55
const basename = path.basename(file, path.extname(file))
66
return `export { ${basename} } from './${basename}';`
77
})

packages/cli/src/dirCommand.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ export const isCompilable = (filename: string): boolean => {
3535
}
3636

3737
export interface IndexTemplate {
38-
(paths: string[]): string
38+
(paths: FileInfo[]): string
3939
}
4040

41-
const defaultIndexTemplate: IndexTemplate = (paths) => {
42-
const exportEntries = paths.map((filePath) => {
41+
interface FileInfo {
42+
path: string
43+
originalPath: string
44+
}
45+
46+
const defaultIndexTemplate: IndexTemplate = (paths: FileInfo[]) => {
47+
const exportEntries = paths.map(({ path: filePath }) => {
4348
const basename = path.basename(filePath, path.extname(filePath))
4449
const exportName = formatExportName(basename)
4550
return `export { default as ${exportName} } from './${basename}'`
@@ -92,7 +97,7 @@ export const dirCommand: SvgrCommand = async (
9297

9398
const generateIndex = async (
9499
dest: string,
95-
files: string[],
100+
files: FileInfo[],
96101
opts: Options,
97102
) => {
98103
const ext = resolveExtension(opts, extOpt, false)
@@ -119,17 +124,27 @@ export const dirCommand: SvgrCommand = async (
119124
if (stats.isDirectory()) {
120125
const dirname = filename
121126
const files = await fs.readdir(dirname)
122-
const results = await Promise.all(
127+
const results = (await Promise.all(
123128
files.map(async (relativeFile) => {
124129
const absFile = path.join(dirname, relativeFile)
125-
return handle(absFile, root)
130+
return [absFile, await handle(absFile, root)]
126131
}),
127-
)
128-
const transformed = results.filter((result) => result.transformed)
132+
)) as [
133+
string,
134+
{
135+
dest: string | null
136+
transformed: boolean
137+
},
138+
][]
139+
const transformed = results.filter(([, result]) => result.transformed)
129140
if (transformed.length) {
130141
const destFiles = results
131-
.map((result) => result.dest)
132-
.filter(Boolean) as string[]
142+
.filter(([, result]) => result.dest)
143+
.map(([originalPath, result]) => ({
144+
path: result.dest,
145+
originalPath,
146+
}))
147+
.filter(({ path }) => path) as FileInfo[]
133148
const dest = path.resolve(
134149
outDir as string,
135150
path.relative(root, dirname),

website/pages/docs/cli.mdx

+1-1
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((filePath) => {
102+
const exportEntries = filePaths.map(({ path: filePath }) => {
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ${variables.interfaces};
4040
const ${variables.componentName} = (${variables.props}) => (
4141
${variables.jsx}
4242
);
43-
43+
4444
${variables.exports};
4545
`
4646
}
@@ -105,7 +105,7 @@ The customization is the same, a file that exports a function:
105105
const path = require('path')
106106

107107
function defaultIndexTemplate(filePaths) {
108-
const exportEntries = filePaths.map((filePath) => {
108+
const exportEntries = filePaths.map(({ path: filePath }) => {
109109
const basename = path.basename(filePath, path.extname(filePath))
110110
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename
111111
return `export { default as ${exportName} } from './${basename}'`

0 commit comments

Comments
 (0)