Skip to content

Commit 56a111f

Browse files
jstckigregberge
authored andcommitted
feat: support custom file extension (#47)
Closes #31
1 parent ca40d16 commit 56a111f

5 files changed

Lines changed: 21 additions & 4 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Options:
9090
--no-svgo disable SVGO (default: true)
9191
--no-prettier disable Prettier (default: true)
9292
--template <file> specify a custom template to use
93+
--ext <ext> specify a custom file extension to use (default: "js")
9394
--no-expand-props disable props expanding (default: true)
9495
--ref add svgRef prop to svg
9596
--icon use "1em" as width and height
@@ -320,6 +321,14 @@ example of template, see [the default one](src/transforms/wrapIntoComponent.js).
320321
| ---------------------------------------------------------- | ------------ | ------------------ |
321322
| [`wrapIntoComponent`](src/transforms/wrapIntoComponent.js) | `--template` | `template: <func>` |
322323

324+
### File extension
325+
326+
Specify a custom extension for generated files.
327+
328+
| Default | CLI Override | API Override |
329+
| ------- | ------------ | --------------- |
330+
| `"js"` | `--ext` | `ext: <string>` |
331+
323332
### Expand props
324333

325334
All properties given to component will be forwarded on SVG tag.

src/cli/dirCommand.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import outputFileSync from 'output-file-sync'
55
import { convertFile, isCompilableExtension, readdir } from './util'
66
import { pascalCase } from '../transforms/rename'
77

8-
export const rename = relative => {
8+
export const rename = (relative, { ext = 'js' } = {}) => {
99
const relativePath = path.parse(relative)
10-
relativePath.ext = '.js'
10+
relativePath.ext = `.${ext}`
1111
relativePath.name = pascalCase(relativePath.name)
1212
relativePath.base = null
1313

@@ -18,7 +18,7 @@ async function dirCommand(program, filenames, opts) {
1818
async function write(src, relative) {
1919
if (!isCompilableExtension(relative)) return false
2020

21-
relative = rename(relative)
21+
relative = rename(relative, opts)
2222

2323
const dest = path.join(program.outDir, relative)
2424

src/cli/dirCommand.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ describe('rename', () => {
88
expect(rename('camelcase.js')).toBe('Camelcase.js')
99
})
1010

11-
it('should change the extension to js', () => {
11+
it('should change the extension to js by default', () => {
1212
const result = rename('camel-case.svg')
1313
expect(result).toBe('CamelCase.js')
1414
})
15+
16+
it('should change the extension to whatever is configured', () => {
17+
const result = rename('camel-case.svg', { ext: 'tsx' })
18+
expect(result).toBe('CamelCase.tsx')
19+
})
1520
})

src/cli/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ program
2222
.option('--no-svgo', 'disable SVGO')
2323
.option('--no-prettier', 'disable Prettier')
2424
.option('--template <file>', 'specify a custom template to use')
25+
.option('--ext <ext>', 'specify a custom file extension (default: "js")')
2526
.option('--no-expand-props', 'disable props expanding')
2627
.option('--ref', 'add svgRef prop to svg')
2728
.option('--icon', 'use "1em" as width and height')

src/configToOptions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const defaultConfig = {
3131
bracketSpacing: undefined, // default to prettier
3232
jsxBracketSameLine: undefined, // default to prettier
3333
template: wrapIntoComponent,
34+
ext: 'js',
3435
}
3536

3637
function configToOptions(config = {}) {
@@ -83,6 +84,7 @@ function configToOptions(config = {}) {
8384
},
8485
prettier: config.prettier ? getPrettierConfig() : null,
8586
template: config.template(config),
87+
ext: config.ext,
8688
}
8789
}
8890

0 commit comments

Comments
 (0)