From f14554c549b12be8307058991ea3519a7578c210 Mon Sep 17 00:00:00 2001 From: Misha Kaletsky <15040698+mmkal@users.noreply.github.com> Date: Mon, 2 Jan 2023 19:06:56 +0000 Subject: [PATCH] Support asset imports and pascale-casing --- src/presets/barrel.ts | 32 +++++++++++++++----------- test/presets/barrel.test.ts | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/presets/barrel.ts b/src/presets/barrel.ts index f40e46e..a63b5c4 100644 --- a/src/presets/barrel.ts +++ b/src/presets/barrel.ts @@ -39,30 +39,36 @@ 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 => @@ -70,10 +76,10 @@ export const barrel: Preset<{ ) .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() diff --git a/test/presets/barrel.test.ts b/test/presets/barrel.test.ts index 6240e2e..4951122 100644 --- a/test/presets/barrel.test.ts +++ b/test/presets/barrel.test.ts @@ -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 + } + " + `) +})