From fdb2d6465dae32bc777629841ec8bb4745d5881c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Sat, 29 Jan 2022 09:27:21 +0100 Subject: [PATCH] fix: use .ts extension for generated index Fix #462 --- packages/cli/src/__snapshots__/index.test.ts.snap | 5 +++++ packages/cli/src/dirCommand.ts | 10 +++++++--- packages/cli/src/index.test.ts | 9 +++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/__snapshots__/index.test.ts.snap b/packages/cli/src/__snapshots__/index.test.ts.snap index dc21e590..a2c6a585 100644 --- a/packages/cli/src/__snapshots__/index.test.ts.snap +++ b/packages/cli/src/__snapshots__/index.test.ts.snap @@ -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' +" +`; diff --git a/packages/cli/src/dirCommand.ts b/packages/cli/src/dirCommand.ts index 44098791..907220ea 100644 --- a/packages/cli/src/dirCommand.ts +++ b/packages/cli/src/dirCommand.ts @@ -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, @@ -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)) { @@ -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) diff --git a/packages/cli/src/index.test.ts b/packages/cli/src/index.test.ts index 78fb2da7..95750846 100644 --- a/packages/cli/src/index.test.ts +++ b/packages/cli/src/index.test.ts @@ -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`