Skip to content

Commit

Permalink
feat: add "index" option (#607)
Browse files Browse the repository at this point in the history
Thanks!
  • Loading branch information
Filipe Marins committed Oct 12, 2021
1 parent 1e36ead commit 4978610
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/cli/README.md
Expand Up @@ -33,6 +33,7 @@ Options:
--replace-attr-values <old=new> replace an attribute value
--template <file> specify a custom template to use
--index-template <file> specify a custom index.js template to use
--no-index disable index file generation
--title-prop create a title element linked with props
--prettier-config <fileOrJson> Prettier config
--no-prettier disable Prettier
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/__snapshots__/index.test.js.snap
Expand Up @@ -18,6 +18,12 @@ export default SvgFile;

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

exports[`cli should support --no-index 1`] = `
Array [
"File.js",
]
`;

exports[`cli should support --prettier-config as file 1`] = `
"import * as React from 'react'
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/dirCommand.js
Expand Up @@ -75,9 +75,8 @@ export default async function dirCommand(
return { transformed: true, dest }
}

async function generateIndex(dest, files) {
async function generateIndex(dest, files, config) {
const indexFile = path.join(dest, `index.${ext}`)
const config = loadConfig.sync(options, { filePath: indexFile })
const indexTemplate = config.indexTemplate || defaultIndexTemplate
await fs.writeFile(indexFile, indexTemplate(files))
}
Expand All @@ -98,7 +97,10 @@ export default async function dirCommand(
if (transformed.length) {
const destFiles = results.map((result) => result.dest).filter(Boolean)
const dest = path.resolve(opts.outDir, path.relative(root, dirname))
await generateIndex(dest, destFiles)
const config = loadConfig.sync(options, { filePath: dest })
if (config.index) {
await generateIndex(dest, destFiles, config)
}
}
return { transformed: false, dest: null }
}
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/index.js
Expand Up @@ -90,6 +90,7 @@ program
'--index-template <file>',
'specify a custom index.js template to use',
)
.option('--no-index', 'disable index file generation')
.option('--title-prop', 'create a title element linked with props')
.option(
'--prettier-config <fileOrJson>',
Expand Down Expand Up @@ -200,6 +201,10 @@ async function run() {
}
}

if (opts.index === false) {
delete config.index
}

const command = opts.outDir ? dirCommand : fileCommand

await command(opts, program, filenames, config)
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/index.test.js
Expand Up @@ -209,4 +209,12 @@ describe('cli', () => {
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
expect(content).toMatchSnapshot()
}, 10000)

it('should support --no-index', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/no-index-case`
await del(outDir)
await cli(`--no-index ${inDir} --out-dir=${outDir}`)
expect(await fs.readdir(outDir)).toMatchSnapshot()
}, 10000)
})
1 change: 1 addition & 0 deletions packages/core/src/config.js
Expand Up @@ -15,6 +15,7 @@ export const DEFAULT_CONFIG = {
svgo: true,
svgoConfig: null,
template: null,
index: false,
titleProp: false,
runtimeConfig: true,
plugins: null,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.d.ts
Expand Up @@ -68,6 +68,8 @@ export interface SvgrOpts {
* https://github.com/gregberge/svgr/blob/main/packages/babel-plugin-transform-svg-component/src/index.js
*/
template?: TemplateFunc
/** Disable index file generation. */
index?: boolean
/** Output files into a directory. */
outDir?: string
/**
Expand Down
8 changes: 8 additions & 0 deletions website/pages/docs/options.mdx
Expand Up @@ -188,6 +188,14 @@ Specify a template function (API) to change default index.js output (when --out-
| ------------------------------------------------------------------------------------------------ | ------------------ | -------------------------- |
| [`basic template`](https://github.com/gregberge/svgr/blob/master/packages/cli/src/dirCommand.js) | `--index-template` | indexTemplate: files => '' |

## index.js file

Disable index.js file generation

| Default | CLI Override | API Override |
| ------- | ------------ | --------------- |
| `false` | `--no-index` | `index: <bool>` |

## Ignore existing

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

1 comment on commit 4978610

@vercel
Copy link

@vercel vercel bot commented on 4978610 Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.