Skip to content

Commit d19abe2

Browse files
authoredJan 29, 2022
fix: use .ts extension for generated index (#670)
Fix #462
1 parent 318eeaa commit d19abe2

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed
 

‎packages/cli/src/__snapshots__/index.test.ts.snap

+5
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,8 @@ export default SvgComponent
619619
620620
"
621621
`;
622+
623+
exports[`cli using typescript option, it creates index with \`.ts\` extension 1`] = `
624+
"export { default as File } from './File'
625+
"
626+
`;

‎packages/cli/src/dirCommand.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ const defaultIndexTemplate: IndexTemplate = (paths) => {
4747
return exportEntries.join('\n')
4848
}
4949

50-
const resolveExtension = (config: Config, ext?: string) =>
51-
ext || (config.typescript ? 'tsx' : 'js')
50+
const resolveExtension = (
51+
config: Config,
52+
ext: string | null | undefined,
53+
jsx: boolean,
54+
) => ext || (config.typescript ? (jsx ? 'tsx' : 'ts') : 'js')
5255

5356
export const dirCommand: SvgrCommand = async (
5457
opts,
@@ -64,7 +67,7 @@ export const dirCommand: SvgrCommand = async (
6467
outDir,
6568
} = opts
6669

67-
const ext = resolveExtension(opts, extOpt)
70+
const ext = resolveExtension(opts, extOpt, true)
6871

6972
const write = async (src: string, dest: string) => {
7073
if (!isCompilable(src)) {
@@ -92,6 +95,7 @@ export const dirCommand: SvgrCommand = async (
9295
files: string[],
9396
opts: Options,
9497
) => {
98+
const ext = resolveExtension(opts, extOpt, false)
9599
const filepath = path.join(dest, `index.${ext}`)
96100
const indexTemplate = opts.indexTemplate || defaultIndexTemplate
97101
const fileContent = indexTemplate(files)

‎packages/cli/src/index.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ describe('cli', () => {
203203
expect(content).toMatchSnapshot()
204204
})
205205

206+
it('using typescript option, it creates index with `.ts` extension', async () => {
207+
const inDir = '__fixtures__/simple'
208+
const outDir = `__fixtures_build__/ts-index`
209+
await del(outDir)
210+
await cli(`${inDir} --out-dir=${outDir} --typescript`)
211+
const content = await fs.readFile(path.join(outDir, 'index.ts'), 'utf-8')
212+
expect(content).toMatchSnapshot()
213+
})
214+
206215
it('should support --index-template in cli', async () => {
207216
const inDir = '__fixtures__/simple'
208217
const outDir = `__fixtures_build__/custom-index-arg`

1 commit comments

Comments
 (1)

vercel[bot] commented on Jan 29, 2022

@vercel[bot]
Please sign in to comment.