Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Svg prefix to index.js exports by default #383

Merged
merged 2 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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