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

fix: use .ts extension for generated index #670

Merged
merged 1 commit into from
Jan 29, 2022
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
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