Skip to content

Commit dde86a7

Browse files
authored
feat: add components/global/ directory (#113)
* feat: add components/global/ directory * chore: coverage
1 parent 27150d5 commit dde86a7

File tree

4 files changed

+1057
-941
lines changed

4 files changed

+1057
-941
lines changed

README.md

+27-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,31 @@ See [live demo](https://codesandbox.io/s/nuxt-components-cou9k) or [video exampl
6868

6969
## Dynamic Components
7070

71-
In order to use [dynamic components](https://vuejs.org/v2/guide/components.html#Dynamic-Components) such as `<component :is="myComponent" />`, you need to use the [global option](#global).
71+
In order to use [dynamic components](https://vuejs.org/v2/guide/components.html#Dynamic-Components) such as `<component :is="myComponent" />`, there is two options:
72+
- Using `components/global/` directory
73+
- Setting a custom path with the [global option](#global)
74+
75+
### Using `components/global/`
76+
77+
> This feature is only available in Nuxt `v2.14.8` or by upgrading this module to `v1.2.0`
78+
79+
Any component inside `components/global/` will be available globally (with lazy import) so you can directly use them in your dynamic components.
80+
81+
```bash
82+
components/
83+
global/
84+
Home.vue
85+
Post.vue
86+
```
87+
88+
You can now use `<component>`:
89+
90+
```html
91+
<component :is="'Home'" />
92+
<component :is="'Post'" />
93+
```
94+
95+
### Using global option
7296

7397
Considering this directory structure:
7498

@@ -77,7 +101,6 @@ components/
77101
dynamic/
78102
Home.vue
79103
Post.vue
80-
Archive.vue
81104
```
82105

83106
In our `nuxt.config` file, we add this path with `global: true` option:
@@ -94,9 +117,8 @@ export default {
94117
We can now use our dynamic components in our templates:
95118

96119
```html
97-
<div v-for="name in ['Home', 'Post', 'Archive']" :key="name">
98-
<component :is="name"></component>
99-
</div>
120+
<component :is="'Home'" />
121+
<component :is="'Post'" />
100122
```
101123

102124
Please note that the `global` option does not means components are added to main chunk but they are dynamically imported with webpack, [read more](#global).

src/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const componentsModule = <Module> function () {
4646
const { nuxt } = this
4747
const { components } = nuxt.options
4848

49+
/* istanbul ignore if */
4950
if (!components) {
5051
return
5152
}
@@ -62,11 +63,23 @@ const componentsModule = <Module> function () {
6263

6364
await nuxt.callHook('components:dirs', options.dirs)
6465

66+
// Add components/global/ directory
67+
try {
68+
const globalDir = getDir(nuxt.resolver.resolvePath('~/components/global'))
69+
options.dirs.push({
70+
path: globalDir,
71+
global: true
72+
})
73+
} catch (err) {
74+
/* istanbul ignore next */
75+
nuxt.options.watch.push(path.resolve(nuxt.options.srcDir, 'components', 'global'))
76+
}
77+
6578
const componentDirs = options.dirs.filter(isPureObjectOrString).map((dir) => {
6679
const dirOptions: ComponentsDir = typeof dir === 'object' ? dir : { path: dir }
6780

6881
let dirPath = dirOptions.path
69-
try { dirPath = getDir(nuxt.resolver.resolvePath(dirOptions.path)) } catch (err) { }
82+
try { dirPath = getDir(nuxt.resolver.resolvePath(dirOptions.path)) } catch (err) {}
7083

7184
const transpile = typeof dirOptions.transpile === 'boolean' ? dirOptions.transpile : 'auto'
7285

test/fixture/nuxt.config.ts

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

0 commit comments

Comments
 (0)