Skip to content

Commit 4978610

Browse files
author
Filipe Marins
authored
feat: add "index" option (#607)
Thanks!
1 parent 1e36ead commit 4978610

8 files changed

Lines changed: 36 additions & 3 deletions

File tree

packages/cli/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Options:
3333
--replace-attr-values <old=new> replace an attribute value
3434
--template <file> specify a custom template to use
3535
--index-template <file> specify a custom index.js template to use
36+
--no-index disable index file generation
3637
--title-prop create a title element linked with props
3738
--prettier-config <fileOrJson> Prettier config
3839
--no-prettier disable Prettier

packages/cli/src/__snapshots__/index.test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export default SvgFile;
1818

1919
exports[`cli should support --index-template in cli 1`] = `"export { File } from './File'"`;
2020

21+
exports[`cli should support --no-index 1`] = `
22+
Array [
23+
"File.js",
24+
]
25+
`;
26+
2127
exports[`cli should support --prettier-config as file 1`] = `
2228
"import * as React from 'react'
2329

packages/cli/src/dirCommand.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ export default async function dirCommand(
7575
return { transformed: true, dest }
7676
}
7777

78-
async function generateIndex(dest, files) {
78+
async function generateIndex(dest, files, config) {
7979
const indexFile = path.join(dest, `index.${ext}`)
80-
const config = loadConfig.sync(options, { filePath: indexFile })
8180
const indexTemplate = config.indexTemplate || defaultIndexTemplate
8281
await fs.writeFile(indexFile, indexTemplate(files))
8382
}
@@ -98,7 +97,10 @@ export default async function dirCommand(
9897
if (transformed.length) {
9998
const destFiles = results.map((result) => result.dest).filter(Boolean)
10099
const dest = path.resolve(opts.outDir, path.relative(root, dirname))
101-
await generateIndex(dest, destFiles)
100+
const config = loadConfig.sync(options, { filePath: dest })
101+
if (config.index) {
102+
await generateIndex(dest, destFiles, config)
103+
}
102104
}
103105
return { transformed: false, dest: null }
104106
}

packages/cli/src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ program
9090
'--index-template <file>',
9191
'specify a custom index.js template to use',
9292
)
93+
.option('--no-index', 'disable index file generation')
9394
.option('--title-prop', 'create a title element linked with props')
9495
.option(
9596
'--prettier-config <fileOrJson>',
@@ -200,6 +201,10 @@ async function run() {
200201
}
201202
}
202203

204+
if (opts.index === false) {
205+
delete config.index
206+
}
207+
203208
const command = opts.outDir ? dirCommand : fileCommand
204209

205210
await command(opts, program, filenames, config)

packages/cli/src/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,12 @@ describe('cli', () => {
209209
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
210210
expect(content).toMatchSnapshot()
211211
}, 10000)
212+
213+
it('should support --no-index', async () => {
214+
const inDir = '__fixtures__/simple'
215+
const outDir = `__fixtures_build__/no-index-case`
216+
await del(outDir)
217+
await cli(`--no-index ${inDir} --out-dir=${outDir}`)
218+
expect(await fs.readdir(outDir)).toMatchSnapshot()
219+
}, 10000)
212220
})

packages/core/src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const DEFAULT_CONFIG = {
1515
svgo: true,
1616
svgoConfig: null,
1717
template: null,
18+
index: false,
1819
titleProp: false,
1920
runtimeConfig: true,
2021
plugins: null,

packages/core/src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export interface SvgrOpts {
6868
* https://github.com/gregberge/svgr/blob/main/packages/babel-plugin-transform-svg-component/src/index.js
6969
*/
7070
template?: TemplateFunc
71+
/** Disable index file generation. */
72+
index?: boolean
7173
/** Output files into a directory. */
7274
outDir?: string
7375
/**

website/pages/docs/options.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ Specify a template function (API) to change default index.js output (when --out-
188188
| ------------------------------------------------------------------------------------------------ | ------------------ | -------------------------- |
189189
| [`basic template`](https://github.com/gregberge/svgr/blob/master/packages/cli/src/dirCommand.js) | `--index-template` | indexTemplate: files => '' |
190190

191+
## index.js file
192+
193+
Disable index.js file generation
194+
195+
| Default | CLI Override | API Override |
196+
| ------- | ------------ | --------------- |
197+
| `false` | `--no-index` | `index: <bool>` |
198+
191199
## Ignore existing
192200

193201
When used with `--out-dir`, it ignores already existing files.

0 commit comments

Comments
 (0)