Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "--no-index" option #607

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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