mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
Compare commits
13 Commits
6c7f539f99
...
58c4004ee9
Author | SHA1 | Date | |
---|---|---|---|
|
58c4004ee9 | ||
|
edc299a043 | ||
|
ad3ab4d310 | ||
|
7b69dc9463 | ||
|
bbf481fb26 | ||
|
6666197364 | ||
|
dc229914c7 | ||
|
d2540e45d0 | ||
|
9bb36478ff | ||
|
519df56854 | ||
|
dad9d00008 | ||
|
cb9a4ec847 | ||
|
bcef9571dd |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -248,7 +248,7 @@ jobs:
|
||||
TEST_PAYLOAD: ${{ matrix.payload }}
|
||||
SKIP_BUNDLE_SIZE: ${{ github.event_name != 'push' || matrix.env == 'dev' || matrix.builder == 'webpack' || matrix.context == 'default' || matrix.payload == 'js' || runner.os == 'Windows' }}
|
||||
|
||||
- uses: codecov/codecov-action@5c47607acb93fed5485fdbf7232e8a31425f672a # v5.0.2
|
||||
- uses: codecov/codecov-action@985343d70564a82044c1b7fcb84c2fa05405c1a2 # v5.0.4
|
||||
if: github.event_name != 'push' && matrix.env == 'built' && matrix.builder == 'vite' && matrix.context == 'default' && matrix.os == 'ubuntu-latest' && matrix.manifest == 'manifest-on'
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
|
98
docs/2.guide/2.directory-structure/1.shared.md
Normal file
98
docs/2.guide/2.directory-structure/1.shared.md
Normal file
@ -0,0 +1,98 @@
|
||||
---
|
||||
title: 'shared'
|
||||
head.title: 'shared/'
|
||||
description: 'Use the shared/ directory to share functionality between the Vue app and the Nitro server.'
|
||||
navigation.icon: 'i-ph-folder'
|
||||
---
|
||||
|
||||
The `shared/` directory allows you to share code that can be used in both the Vue app and the Nitro server.
|
||||
|
||||
::tip
|
||||
The `shared/` directory is available in Nuxt v3.14+.
|
||||
::
|
||||
|
||||
::important
|
||||
Code in the `shared/` directory cannot import any Vue or Nitro code.
|
||||
::
|
||||
|
||||
## Usage
|
||||
|
||||
**Method 1:** Using named export
|
||||
|
||||
```ts twoslash [shared/utils/capitalize.ts]
|
||||
export const capitalize = (input: string) => {
|
||||
return input[0].toUpperCase() + input.slice(1)
|
||||
}
|
||||
```
|
||||
|
||||
**Method 2:** Using default export
|
||||
|
||||
```ts twoslash [shared/utils/capitalize.ts]
|
||||
export default function capitalize (input: string) {
|
||||
return input[0].toUpperCase() + input.slice(1)
|
||||
}
|
||||
```
|
||||
|
||||
**Usage:** You can now use auto-imported utility functions in `.js`, `.ts` and `.vue` files within your Vue app and the `server/` directory.
|
||||
|
||||
If you have [set `compatibilityVersion: 4` in your `nuxt.config.ts`](/docs/getting-started/upgrade#opting-in-to-nuxt-4), you can use the auto-imported functions in the `app/` directory. This is part of Nuxt's progressive compatibility features preparing for version 4.
|
||||
|
||||
```vue [app.vue]
|
||||
<script setup lang="ts">
|
||||
const hello = capitalize('hello')
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
{{ hello }}
|
||||
</div>
|
||||
</template>
|
||||
```
|
||||
|
||||
```ts [server/api/hello.get.ts]
|
||||
export default defineEventHandler((event) => {
|
||||
return {
|
||||
hello: capitalize('hello')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Auto Imports
|
||||
|
||||
Only files in the `shared/utils/` and `shared/types/` directories will be auto-imported. Files nested within subdirectories of these directories will not be auto-imported.
|
||||
|
||||
::tip
|
||||
The way `shared/utils` and `shared/types` auto-imports work and are scanned is identical to the [`composables/`](/docs/guide/directory-structure/composables) and [`utils/`](/docs/guide/directory-structure/utils) directories.
|
||||
::
|
||||
|
||||
:read-more{to="/docs/guide/directory-structure/composables#how-files-are-scanned"}
|
||||
|
||||
```bash [Directory Structure]
|
||||
-| shared/
|
||||
---| capitalize.ts # Not auto-imported
|
||||
---| formatters
|
||||
-----| lower.ts # Not auto-imported
|
||||
---| utils/
|
||||
-----| lower.ts # Auto-imported
|
||||
-----| formatters
|
||||
-------| upper.ts # Not auto-imported
|
||||
---| types/
|
||||
-----| bar.d.ts # Auto-imported
|
||||
```
|
||||
|
||||
Any other files you create in the `shared/` folder must be manually imported using the `#shared` alias (automatically configured by Nuxt):
|
||||
|
||||
```ts
|
||||
// For files directly in the shared directory
|
||||
import capitalize from '#shared/capitalize'
|
||||
|
||||
// For files in nested directories
|
||||
import lower from '#shared/formatters/lower'
|
||||
|
||||
// For files nested in a folder within utils
|
||||
import upper from '#shared/utils/formatters/upper'
|
||||
```
|
||||
|
||||
This alias ensures consistent imports across your application, regardless of the importing file's location.
|
||||
|
||||
:read-more{to="/docs/guide/concepts/auto-imports"}
|
@ -91,7 +91,7 @@
|
||||
"devalue": "5.1.1",
|
||||
"eslint": "9.15.0",
|
||||
"eslint-plugin-no-only-tests": "3.3.0",
|
||||
"eslint-plugin-perfectionist": "4.0.2",
|
||||
"eslint-plugin-perfectionist": "4.0.3",
|
||||
"eslint-typegen": "0.3.2",
|
||||
"h3": "npm:h3-nightly@2.0.0-1718872656.6765a6e",
|
||||
"happy-dom": "15.11.6",
|
||||
@ -118,7 +118,7 @@
|
||||
"vue-router": "4.4.5",
|
||||
"vue-tsc": "2.1.10"
|
||||
},
|
||||
"packageManager": "pnpm@9.13.2",
|
||||
"packageManager": "pnpm@9.14.1",
|
||||
"engines": {
|
||||
"node": "^16.10.0 || >=18.0.0"
|
||||
},
|
||||
|
@ -114,8 +114,8 @@ importers:
|
||||
specifier: 3.3.0
|
||||
version: 3.3.0
|
||||
eslint-plugin-perfectionist:
|
||||
specifier: 4.0.2
|
||||
version: 4.0.2(eslint@9.15.0(jiti@2.4.0))(typescript@5.6.3)
|
||||
specifier: 4.0.3
|
||||
version: 4.0.3(eslint@9.15.0(jiti@2.4.0))(typescript@5.6.3)
|
||||
eslint-typegen:
|
||||
specifier: 0.3.2
|
||||
version: 0.3.2(eslint@9.15.0(jiti@2.4.0))
|
||||
@ -4373,8 +4373,8 @@ packages:
|
||||
resolution: {integrity: sha512-brcKcxGnISN2CcVhXJ/kEQlNa0MEfGRtwKtWA16SkqXHKitaKIMrfemJKLKX1YqDU5C/5JY3PvZXd5jEW04e0Q==}
|
||||
engines: {node: '>=5.0.0'}
|
||||
|
||||
eslint-plugin-perfectionist@4.0.2:
|
||||
resolution: {integrity: sha512-zWdgyg2SdHqhp/P9d9vKwo5qD9is28xMAGzBslHqkZz5mVIikjyz1qvuJ4yS7Wrsf4KlbGorORefb4Kbe7Puzg==}
|
||||
eslint-plugin-perfectionist@4.0.3:
|
||||
resolution: {integrity: sha512-CyafnreF6boy4lf1XaF72U8NbkwrfjU/mOf1y6doaDMS9zGXhUU1DSk+ZPf/rVwCf1PL1m+rhHqFs+IcB8kDmA==}
|
||||
engines: {node: ^18.0.0 || >=20.0.0}
|
||||
peerDependencies:
|
||||
eslint: '>=8.0.0'
|
||||
@ -11519,7 +11519,7 @@ snapshots:
|
||||
|
||||
eslint-plugin-no-only-tests@3.3.0: {}
|
||||
|
||||
eslint-plugin-perfectionist@4.0.2(eslint@9.15.0(jiti@2.4.0))(typescript@5.6.3):
|
||||
eslint-plugin-perfectionist@4.0.3(eslint@9.15.0(jiti@2.4.0))(typescript@5.6.3):
|
||||
dependencies:
|
||||
'@typescript-eslint/types': 8.15.0
|
||||
'@typescript-eslint/utils': 8.15.0(eslint@9.15.0(jiti@2.4.0))(typescript@5.6.3)
|
||||
|
Loading…
Reference in New Issue
Block a user