Skip to content

Commit 0b76ddd

Browse files
authored
feat: support dir.extensions (#32)
1 parent 8fcf984 commit 0b76ddd

File tree

6 files changed

+40
-15
lines changed

6 files changed

+40
-15
lines changed

README.md

+33-14
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ If you want to keep the filename as `Bar.vue`, consider using the `prefix` optio
127127
```js
128128
components: [
129129
'~/components/',
130-
{
131-
path: '~/components/foo/',
132-
prefix: 'foo'
133-
}
130+
{ path: '~/components/foo/', prefix: 'foo' }
134131
]
135132
```
136133
@@ -163,12 +160,39 @@ Path (absolute or relative) to the directory containing your components.
163160
164161
You can use nuxt aliases (`~` or `@`) to refer to directories inside project or directly use a npm package path similar to require.
165162
163+
#### extensions
164+
165+
- Type: `Array<string>`
166+
- Default:
167+
- Extensions supported by nuxt builder (`builder.supportedExtensions`)
168+
- Default supported extensions `['vue', 'js']` or `['vue', 'js', 'ts', 'tsx']` depending on your environment
169+
170+
**Example:** Support multi-file component structure
171+
172+
If you prefer to split your SFCs into `.js`, `.vue` and `.css`, you can only enable `.vue` files to be scanned:
173+
174+
```
175+
├── src
176+
│ └── components
177+
│ └── componentC
178+
│ └── componentC.vue
179+
│ └── componentC.js
180+
│ └── componentC.scss
181+
```
182+
183+
```js
184+
// nuxt.config.js
185+
export default {
186+
components: [
187+
{ path: '~/components', extensions: ['vue'] }
188+
]
189+
}
190+
```
191+
166192
#### pattern
167193
168194
- Type: `string` ([glob pattern]( https://github.com/isaacs/node-glob#glob-primer))
169195
- Default: `**/*.${extensions.join(',')}`
170-
- `extensions` being Nuxt `builder.supportedExtensions`
171-
- Resulting in `**/*.{vue,js}` or `**/*.{vue,js,ts,tsx}` depending on your environment
172196
173197
Accept Pattern that will be run against specified `path`.
174198
@@ -192,15 +216,10 @@ Example below adds `awesome-`/`Awesome` prefix to the name of components in `awe
192216
```js
193217
// nuxt.config.js
194218
export default {
195-
components: {
196-
dirs: [
219+
components: [
197220
'~/components',
198-
{
199-
path: '~/components/awesome/',
200-
prefix: 'awesome'
201-
}
202-
]
203-
}
221+
{ path: '~/components/awesome/', prefix: 'awesome' }
222+
]
204223
}
205224
```
206225

src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ declare module '@nuxt/types/config/hooks' {
2525

2626
export interface ComponentsDir extends ScanDir {
2727
watch?: boolean
28+
extensions?: string[]
2829
transpile?: 'auto' | boolean
2930
}
3031

@@ -64,11 +65,14 @@ export default <Module> function () {
6465
console.warn('Components directory not found: `' + dirPath + '`')
6566
}
6667

68+
const extensions = dirOptions.extensions || builder.supportedExtensions
69+
6770
return {
6871
...dirOptions,
6972
enabled,
7073
path: dirPath,
71-
pattern: dirOptions.pattern || `**/*.{${builder.supportedExtensions.join(',')}}`,
74+
extensions,
75+
pattern: dirOptions.pattern || `**/*.{${extensions.join(',')},}`,
7276
ignore: nuxtIgnorePatterns.concat(dirOptions.ignore || []),
7377
transpile: (transpile === 'auto' ? dirPath.includes('node_modules') : transpile)
7478
}

test/fixture/components/multifile/ComponentC/ComponentC.vue

Whitespace-only changes.

test/fixture/components/multifile/ComponentC/index.js

Whitespace-only changes.

test/fixture/nuxt.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const config: Configuration = {
1212
components: {
1313
dirs: [
1414
'~/components',
15+
{ path: '~/components/multifile', extensions: ['vue'] },
1516
'~/non-existent',
1617
{ path: '@/components/base', prefix: 'Base' },
1718
{ path: '@/components/icons', prefix: 'Icon', transpile: true /* Only for coverage purpose */ }

test/unit/scanner.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test('scanner', async () => {
55

66
const expectedComponents = [
77
'BaseButton',
8+
'ComponentC',
89
'BaseSecondButton',
910
'IconHome',
1011
'Bar',

0 commit comments

Comments
 (0)