Skip to content

Commit

Permalink
feat: add Svg prefix to exports that starts with a number (#383)
Browse files Browse the repository at this point in the history
Closes #379
  • Loading branch information
TrySound authored and gregberge committed Jan 18, 2020
1 parent f734dda commit fd120d1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
20 changes: 20 additions & 0 deletions __fixtures__/numeric/2.file.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions __fixtures__/numeric/file.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions packages/cli/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cli should add Svg prefix to index.js exports staring with number 1`] = `
"export { default as Svg2File } from './2File'
export { default as File } from './File'"
`;

exports[`cli should not override config with cli defaults 1`] = `
"import React from \\"react\\";
Expand Down Expand Up @@ -412,6 +417,8 @@ __fixtures__/complex/telegram.svg -> __fixtures_build__/whole/complex/Telegram.j
__fixtures__/nesting/a/c/three.svg -> __fixtures_build__/whole/nesting/a/c/Three.js
__fixtures__/nesting/a/two.svg -> __fixtures_build__/whole/nesting/a/Two.js
__fixtures__/nesting/one.svg -> __fixtures_build__/whole/nesting/One.js
__fixtures__/numeric/2.file.svg -> __fixtures_build__/whole/numeric/2File.js
__fixtures__/numeric/file.svg -> __fixtures_build__/whole/numeric/File.js
__fixtures__/simple/file.svg -> __fixtures_build__/whole/simple/File.js
__fixtures__/withPrettierRc/file.svg -> __fixtures_build__/whole/withPrettierRc/File.js
__fixtures__/withSvgoYml/file.svg -> __fixtures_build__/whole/withSvgoYml/File.js
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/dirCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export function isCompilable(filename) {
function defaultIndexTemplate(files) {
const exportEntries = files.map(file => {
const basename = path.basename(file, path.extname(file))
return `export { default as ${basename} } from './${basename}'`
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename
return `export { default as ${exportName} } from './${basename}'`
})
return exportEntries.join('\n')
}
Expand Down
17 changes: 15 additions & 2 deletions packages/cli/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,22 @@ describe('cli', () => {
expect(result).toMatchSnapshot()
}, 10000)

it('should add Svg prefix to index.js exports staring with number', async () => {
const inDir = '__fixtures__/numeric'
const outDir = `__fixtures_build__/prefix-exports`
await del(outDir)
await cli(`${inDir} --out-dir=${outDir}`)
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
expect(content).toMatchSnapshot()
}, 10000)

it('should support custom index.js with directory output', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/custom-index`
await del(outDir)
await cli(`${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`)
await cli(
`${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`,
)
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
expect(content).toMatchSnapshot()
}, 10000)
Expand All @@ -180,7 +191,9 @@ describe('cli', () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/custom-index-arg`
await del(outDir)
await cli(`${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`)
await cli(
`${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`,
)
const content = fs.readFileSync(path.join(outDir, 'index.js'), 'utf-8')
expect(content).toMatchSnapshot()
}, 10000)
Expand Down

1 comment on commit fd120d1

@vercel
Copy link

@vercel vercel bot commented on fd120d1 Jan 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.