Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt)!: only scan top level composables/ and support glob #6025

Merged
merged 6 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
78 changes: 45 additions & 33 deletions docs/content/2.guide/3.directory-structure/5.composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,21 @@ head.title: Composables directory

Nuxt 3 supports `composables/` directory to automatically import your Vue composables into your application using [auto-imports](/guide/concepts/auto-imports)!

## How files are scanned

Nuxt only scans files at the top level of the `composables/` directory (or index files within any subdirectories) for composables.

For example:

```bash
composables
| - useFoo.ts // scanned
| - useBar
| --- supportingFile.ts // not scanned
| --- index.ts // scanned
```

Only `useFoo.ts` and `useBar/index.ts` would be searched for imports - and if the latter is a default export, it would be registered as `useBar` rather than `index`.

To get auto imports for `useBar/supportingFile.ts`, you have to re-export the composables you need from the `useBar/index.ts` file.

```js [composables/useBar/index.ts]
export const useBar = () => {
}

// Enables auto import for this export
export { useBaz } from './supportingFile'
```

::alert{type=warning}
Auto import generating doesn't work with `export * from './supportingFile.ts'`, you must use named exports or a default export.
::

Under the hood, Nuxt auto generates the file `.nuxt/auto-imports.d.ts` to declare the types.

Be aware that you have to run `nuxi prepare`, `nuxi dev` or `nuxi build` in order to let Nuxt generates the types. If you create a composable without having the dev server running, typescript will throw an error `Cannot find name 'useBar'.`

## Example: (using named export)
## Example:

**Method 1:** Using named export

```js [composables/useFoo.ts]
export const useFoo = () => {
return useState('foo', () => 'bar')
}
```

## Example: (using default export)
**Method 2:** Using default export

```js [composables/use-foo.ts or composables/useFoo.ts]
// It will be available as useFoo() (camelCase of file name without extension)
Expand All @@ -59,7 +31,7 @@ export default function () {
}
```

You can now auto-import it:
**Usage:** You can now use auto imported composable in `.js`, `.ts` and `.vue` files

```vue [app.vue]
<template>
Expand All @@ -74,3 +46,43 @@ const foo = useFoo()
```

:LinkExample{link="/examples/auto-imports/composables"}

## How files are scanned

Nuxt only scans files at the top level of the `composables/` directory for composables.

For example:

```bash
composables
| - useFoo.ts // scanned
| - index.ts // scanned
| - nested
| --- utils.ts // not scanned
```

Only `composables/useFoo.ts` would be searched for imports.

To get auto imports for nested modules, you could either reexport them (recommended) or configure the scanner to scan nested directories:


**Example:** re-export the composables you need from the `composables/index.ts` file:

```js [composables/index.ts]
// Enables auto import for this export
export { useBaz } from './nested/useBaz.ts'
```

**Example:** Scan nested directories inside composables:

```ts [nuxt.config.ts]
export default defineConfig({
// ...
autoImports: {
dirs: [
// Scan composables from nested directories
'composables/**'
]
}
})
```
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"scule": "^0.2.1",
"semver": "^7.3.7",
"unctx": "^1.1.4",
"unimport": "^0.4.5",
"unimport": "^0.6.0",
"untyped": "^0.4.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"ufo": "^0.8.5",
"unctx": "^1.1.4",
"unenv": "^0.5.2",
"unimport": "^0.4.5",
"unimport": "^0.6.0",
"unplugin": "^0.7.2",
"untyped": "^0.4.4",
"vue": "^3.2.37",
Expand Down
22 changes: 20 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ __metadata:
semver: ^7.3.7
unbuild: latest
unctx: ^1.1.4
unimport: ^0.4.5
unimport: ^0.6.0
untyped: ^0.4.4
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -9831,7 +9831,7 @@ __metadata:
unbuild: latest
unctx: ^1.1.4
unenv: ^0.5.2
unimport: ^0.4.5
unimport: ^0.6.0
unplugin: ^0.7.2
untyped: ^0.4.4
vue: ^3.2.37
Expand Down Expand Up @@ -12920,6 +12920,24 @@ __metadata:
languageName: node
linkType: hard

"unimport@npm:^0.6.0":
version: 0.6.0
resolution: "unimport@npm:0.6.0"
dependencies:
"@rollup/pluginutils": ^4.2.1
escape-string-regexp: ^5.0.0
fast-glob: ^3.2.11
local-pkg: ^0.4.2
magic-string: ^0.26.2
mlly: ^0.5.5
pathe: ^0.3.2
scule: ^0.2.1
strip-literal: ^0.4.0
unplugin: ^0.7.2
checksum: 1dcacd10c289f9a851fbb020acfeeffc31b29da35f8d4ab92cd78e9a7267904d8f90184439b8fe656d81206d8c57669c411a455dfddba76615ed4ae46848e549
languageName: node
linkType: hard

"unique-filename@npm:^1.1.1":
version: 1.1.1
resolution: "unique-filename@npm:1.1.1"
Expand Down