Skip to content

Commit

Permalink
fix: use .ts extension for generated index (#670)
Browse files Browse the repository at this point in the history
Fix #462
  • Loading branch information
gregberge authored Jan 29, 2022
1 parent 318eeaa commit d19abe2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/cli/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,8 @@ export default SvgComponent
"
`;
exports[`cli using typescript option, it creates index with \`.ts\` extension 1`] = `
"export { default as File } from './File'
"
`;
10 changes: 7 additions & 3 deletions packages/cli/src/dirCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ const defaultIndexTemplate: IndexTemplate = (paths) => {
return exportEntries.join('\n')
}

const resolveExtension = (config: Config, ext?: string) =>
ext || (config.typescript ? 'tsx' : 'js')
const resolveExtension = (
config: Config,
ext: string | null | undefined,
jsx: boolean,
) => ext || (config.typescript ? (jsx ? 'tsx' : 'ts') : 'js')

export const dirCommand: SvgrCommand = async (
opts,
Expand All @@ -64,7 +67,7 @@ export const dirCommand: SvgrCommand = async (
outDir,
} = opts

const ext = resolveExtension(opts, extOpt)
const ext = resolveExtension(opts, extOpt, true)

const write = async (src: string, dest: string) => {
if (!isCompilable(src)) {
Expand Down Expand Up @@ -92,6 +95,7 @@ export const dirCommand: SvgrCommand = async (
files: string[],
opts: Options,
) => {
const ext = resolveExtension(opts, extOpt, false)
const filepath = path.join(dest, `index.${ext}`)
const indexTemplate = opts.indexTemplate || defaultIndexTemplate
const fileContent = indexTemplate(files)
Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ describe('cli', () => {
expect(content).toMatchSnapshot()
})

it('using typescript option, it creates index with `.ts` extension', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/ts-index`
await del(outDir)
await cli(`${inDir} --out-dir=${outDir} --typescript`)
const content = await fs.readFile(path.join(outDir, 'index.ts'), 'utf-8')
expect(content).toMatchSnapshot()
})

it('should support --index-template in cli', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/custom-index-arg`
Expand Down

1 comment on commit d19abe2

@vercel
Copy link

@vercel vercel bot commented on d19abe2 Jan 29, 2022

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.