Skip to content

Commit fd120d1

Browse files
TrySoundgregberge
authored andcommitted
feat: add Svg prefix to exports that starts with a number (#383)
Closes #379
1 parent f734dda commit fd120d1

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

__fixtures__/numeric/2.file.svg

Lines changed: 20 additions & 0 deletions
Loading

__fixtures__/numeric/file.svg

Lines changed: 20 additions & 0 deletions
Loading

packages/cli/src/__snapshots__/index.test.js.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`cli should add Svg prefix to index.js exports staring with number 1`] = `
4+
"export { default as Svg2File } from './2File'
5+
export { default as File } from './File'"
6+
`;
7+
38
exports[`cli should not override config with cli defaults 1`] = `
49
"import React from \\"react\\";
510
@@ -412,6 +417,8 @@ __fixtures__/complex/telegram.svg -> __fixtures_build__/whole/complex/Telegram.j
412417
__fixtures__/nesting/a/c/three.svg -> __fixtures_build__/whole/nesting/a/c/Three.js
413418
__fixtures__/nesting/a/two.svg -> __fixtures_build__/whole/nesting/a/Two.js
414419
__fixtures__/nesting/one.svg -> __fixtures_build__/whole/nesting/One.js
420+
__fixtures__/numeric/2.file.svg -> __fixtures_build__/whole/numeric/2File.js
421+
__fixtures__/numeric/file.svg -> __fixtures_build__/whole/numeric/File.js
415422
__fixtures__/simple/file.svg -> __fixtures_build__/whole/simple/File.js
416423
__fixtures__/withPrettierRc/file.svg -> __fixtures_build__/whole/withPrettierRc/File.js
417424
__fixtures__/withSvgoYml/file.svg -> __fixtures_build__/whole/withSvgoYml/File.js

packages/cli/src/dirCommand.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function isCompilable(filename) {
3838
function defaultIndexTemplate(files) {
3939
const exportEntries = files.map(file => {
4040
const basename = path.basename(file, path.extname(file))
41-
return `export { default as ${basename} } from './${basename}'`
41+
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename
42+
return `export { default as ${exportName} } from './${basename}'`
4243
})
4344
return exportEntries.join('\n')
4445
}

packages/cli/src/index.test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,22 @@ describe('cli', () => {
167167
expect(result).toMatchSnapshot()
168168
}, 10000)
169169

170+
it('should add Svg prefix to index.js exports staring with number', async () => {
171+
const inDir = '__fixtures__/numeric'
172+
const outDir = `__fixtures_build__/prefix-exports`
173+
await del(outDir)
174+
await cli(`${inDir} --out-dir=${outDir}`)
175+
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
176+
expect(content).toMatchSnapshot()
177+
}, 10000)
178+
170179
it('should support custom index.js with directory output', async () => {
171180
const inDir = '__fixtures__/simple'
172181
const outDir = `__fixtures_build__/custom-index`
173182
await del(outDir)
174-
await cli(`${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`)
183+
await cli(
184+
`${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`,
185+
)
175186
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
176187
expect(content).toMatchSnapshot()
177188
}, 10000)
@@ -180,7 +191,9 @@ describe('cli', () => {
180191
const inDir = '__fixtures__/simple'
181192
const outDir = `__fixtures_build__/custom-index-arg`
182193
await del(outDir)
183-
await cli(`${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`)
194+
await cli(
195+
`${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`,
196+
)
184197
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
185198
expect(content).toMatchSnapshot()
186199
}, 10000)

0 commit comments

Comments
 (0)