Skip to content
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
32 changes: 19 additions & 13 deletions src/presets/barrel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,47 @@ export const barrel: Preset<{
const cwd = path.dirname(meta.filename)

const ext = meta.filename.split('.').slice(-1)[0]
const pattern = opts.include || `*.${ext}`
const pattern = opts.include || `*.{${ext},${ext}x}`

const relativeFiles = glob
.sync(pattern, {cwd, ignore: opts.exclude})
.filter(f => path.resolve(cwd, f) !== path.resolve(meta.filename))
.map(f => `./${f}`.replace(/(\.\/)+\./g, '.'))
.filter(file => ['.js', '.mjs', '.ts', '.tsx'].includes(path.extname(file)))
.map(f => f.replace(/\.\w+$/, ''))
.map(f => {
const base = f.replace(/\.\w+$/, '')

const firstLetter = /[a-z]/i.exec(f)?.[0]
const camelCase = lodash
.camelCase(base)
.replace(/^([^a-z])/, '_$1')
.replace(/Index$/, '')
const identifier = firstLetter === firstLetter?.toUpperCase() ? lodash.upperFirst(camelCase) : camelCase

return {
import: ['.js', '.mjs', '.ts', '.tsx'].includes(path.extname(f)) ? base : f,
identifier,
}
})

const expectedContent = match(opts.import)
.case(undefined, () => {
return relativeFiles.map(f => `export * from '${f}'`).join('\n')
return relativeFiles.map(f => `export * from '${f.import}'`).join('\n')
})
.case(String, s => {
const importPrefix = s === 'default' ? '' : '* as '
const withIdentifiers = lodash
.chain(relativeFiles)
.map(f => ({
file: f,
identifier: lodash
.camelCase(f)
.replace(/^([^a-z])/, '_$1')
.replace(/Index$/, ''),
}))
.groupBy(info => info.identifier)
.values()
.flatMap(group =>
group.length === 1 ? group : group.map((info, i) => ({...info, identifier: `${info.identifier}_${i + 1}`})),
)
.value()

const imports = withIdentifiers.map(i => `import ${importPrefix}${i.identifier} from '${i.file}'`).join('\n')
const imports = withIdentifiers.map(i => `import ${importPrefix}${i.identifier} from '${i.import}'`).join('\n')
const exportProps = match(opts.export)
.case({name: String, keys: 'path'}, () =>
withIdentifiers.map(i => `${JSON.stringify(i.file)}: ${i.identifier}`),
withIdentifiers.map(i => `${JSON.stringify(i.import)}: ${i.identifier}`),
)
.default(() => withIdentifiers.map(i => i.identifier))
.get()
Expand Down
46 changes: 46 additions & 0 deletions test/presets/barrel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,49 @@ test(`index files are sensibly-named`, () => {
"
`)
})

test(`supports asset imports`, () => {
Object.assign(mockFs, {
'a.jpg': '',
'b.png': '',
})

expect(
preset.barrel({
meta: {filename: 'index.ts', existingContent: ''},
options: {include: '*.{jpg,png}', import: 'default'},
}),
).toMatchInlineSnapshot(`
"import a from './a.jpg'
import b from './b.png'

export {
a,
b
}
"
`)
})

test(`respects pascale case imports`, () => {
Object.assign(mockFs, {
'AntHill.tsx': '',
'BeeHive.ts': '',
})

expect(
preset.barrel({
meta: {filename: 'index.ts', existingContent: ''},
options: {import: 'star'},
}),
).toMatchInlineSnapshot(`
"import * as AntHill from './AntHill'
import * as BeeHive from './BeeHive'

export {
AntHill,
BeeHive
}
"
`)
})