Skip to content

Commit 85ab105

Browse files
committed
docs: fix type issues in twoslash blocks
1 parent 08f9689 commit 85ab105

File tree

9 files changed

+111
-16
lines changed

9 files changed

+111
-16
lines changed

docs/3.api/5.kit/1.modules.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export default defineNuxtModule({
3838
### Type
3939

4040
```ts twoslash
41-
function defineNuxtModule (definition: ModuleDefinition | NuxtModule): NuxtModule
41+
// @errors: 2391
42+
import type { ModuleDefinition, ModuleOptions, NuxtModule } from '@nuxt/schema'
43+
// ---cut---
44+
export function defineNuxtModule<TOptions extends ModuleOptions> (
45+
definition?: ModuleDefinition<TOptions, Partial<TOptions>, false> | NuxtModule<TOptions, Partial<TOptions>, false>,
46+
): NuxtModule<TOptions, TOptions, false>
4247
```
4348

4449
### Parameters

docs/3.api/5.kit/10.templates.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export default defineNuxtModule({
3838
### Type
3939

4040
```ts twoslash
41+
// @errors: 2391
42+
import type { NuxtTemplate, ResolvedNuxtTemplate } from '@nuxt/schema'
43+
// ---cut---
4144
function addTemplate (template: NuxtTemplate | string): ResolvedNuxtTemplate
4245
```
4346

@@ -213,6 +216,9 @@ export default defineNuxtModule({
213216
### Type
214217

215218
```ts twoslash
219+
// @errors: 2391
220+
import type { NuxtServerTemplate } from '@nuxt/schema'
221+
// ---cut---
216222
function addServerTemplate (template: NuxtServerTemplate): NuxtServerTemplate
217223
```
218224

@@ -262,11 +268,15 @@ Regenerate templates that match the filter. If no filter is provided, all templa
262268

263269
### Usage
264270

265-
```ts twoslash
271+
```ts
266272
import { defineNuxtModule, updateTemplates } from '@nuxt/kit'
273+
import { resolve } from 'pathe'
267274
268275
export default defineNuxtModule({
269276
setup (options, nuxt) {
277+
const updateTemplatePaths = [
278+
resolve(nuxt.options.srcDir, 'pages'),
279+
]
270280
// watch and rebuild routes template list when one of the pages changes
271281
nuxt.hook('builder:watch', async (event, relativePath) => {
272282
if (event === 'change') { return }

docs/3.api/5.kit/11.nitro.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ export default defineNuxtModule({
116116
### Type
117117

118118
```ts twoslash
119+
// @errors: 2391
120+
import type { NitroDevEventHandler } from 'nitropack'
121+
// ---cut---
119122
function addDevServerHandler (handler: NitroDevEventHandler): void
120123
```
121124

@@ -134,12 +137,12 @@ function addDevServerHandler (handler: NitroDevEventHandler): void
134137

135138
In some cases, you may want to create a server handler specifically for development purposes, such as a Tailwind config viewer.
136139

137-
```ts twoslash
140+
```ts
138141
import { joinURL } from 'ufo'
139142
import { defineNuxtModule, addDevServerHandler } from '@nuxt/kit'
140143
141144
export default defineNuxtModule({
142-
async setup(options) {
145+
async setup(options, nuxt) {
143146
const route = joinURL(nuxt.options.app?.baseURL, '/_tailwind')
144147
145148
// @ts-ignore
@@ -357,6 +360,8 @@ export function useApiSecret() {
357360
You can then use the `useApiSecret` function in your server code:
358361

359362
```ts twoslash [runtime/server/api/hello.ts]
363+
const useApiSecret = (): string => ''
364+
// ---cut---
360365
export default defineEventHandler(() => {
361366
const apiSecret = useApiSecret()
362367
// Do something with the apiSecret
@@ -433,6 +438,10 @@ export function hello() {
433438
You can then use the `hello` function in your server code.
434439

435440
```ts twoslash [runtime/server/api/hello.ts]
441+
function hello() {
442+
return 'Hello from server utils!'
443+
}
444+
// ---cut---
436445
export default defineEventHandler(() => {
437446
return hello() // Hello from server utils!
438447
})

docs/3.api/5.kit/14.builder.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export default defineNuxtModule({
3333
### Type
3434

3535
```ts twoslash
36+
// @errors: 2391
37+
import type { UserConfig as ViteConfig } from 'vite'
38+
import type { ExtendViteConfigOptions } from '@nuxt/kit'
39+
// ---cut---
3640
function extendViteConfig (callback: ((config: ViteConfig) => void), options?: ExtendViteConfigOptions): void
3741
```
3842

@@ -66,7 +70,7 @@ import { defineNuxtModule, extendWebpackConfig } from '@nuxt/kit'
6670
export default defineNuxtModule({
6771
setup () {
6872
extendWebpackConfig((config) => {
69-
config.module?.rules.push({
73+
config.module!.rules!.push({
7074
test: /\.txt$/,
7175
use: 'raw-loader',
7276
})
@@ -78,6 +82,10 @@ export default defineNuxtModule({
7882
### Type
7983

8084
```ts twoslash
85+
// @errors: 2391
86+
import type { Configuration as WebpackConfig } from 'webpack'
87+
import type { ExtendWebpackConfigOptions } from '@nuxt/kit'
88+
// ---cut---
8189
function extendWebpackConfig (callback: ((config: WebpackConfig) => void), options?: ExtendWebpackConfigOptions): void
8290
```
8391

@@ -106,6 +114,8 @@ Append Vite plugin to the config.
106114
### Usage
107115

108116
```ts twoslash
117+
// @errors: 2307
118+
// ---cut---
109119
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
110120
import { svg4VuePlugin } from 'vite-plugin-svg4vue'
111121
@@ -128,7 +138,11 @@ export default defineNuxtModule({
128138
### Type
129139

130140
```ts twoslash
131-
function addVitePlugin (pluginOrGetter: PluginOrGetter, options?: ExtendViteConfigOptions): void
141+
// @errors: 2391
142+
import type { Plugin as VitePlugin } from 'vite'
143+
import type { ExtendViteConfigOptions } from '@nuxt/kit'
144+
// ---cut---
145+
function addVitePlugin (pluginOrGetter: VitePlugin | VitePlugin[] | (() => VitePlugin | VitePlugin[]), options?: ExtendViteConfigOptions): void
132146
```
133147

134148
::tip
@@ -155,7 +169,7 @@ Append webpack plugin to the config.
155169

156170
### Usage
157171

158-
```ts twoslash
172+
```ts
159173
import EslintWebpackPlugin from 'eslint-webpack-plugin'
160174
import { addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
161175

@@ -183,7 +197,11 @@ export default defineNuxtModule({
183197
### Type
184198
185199
```ts twoslash
186-
function addWebpackPlugin (pluginOrGetter: PluginOrGetter, options?: ExtendWebpackConfigOptions): void
200+
// @errors: 2391
201+
import type { WebpackPluginInstance } from 'webpack'
202+
import type { ExtendWebpackConfigOptions } from '@nuxt/kit'
203+
// ---cut---
204+
function addWebpackPlugin (pluginOrGetter: WebpackPluginInstance | WebpackPluginInstance[] | (() => WebpackPluginInstance | WebpackPluginInstance[]), options?: ExtendWebpackConfigOptions): void
187205
```
188206

189207
::tip
@@ -211,6 +229,17 @@ Builder-agnostic version of `addVitePlugin` and `addWebpackPlugin`. It will add
211229
### Type
212230

213231
```ts twoslash
232+
// @errors: 2391
233+
import type { ExtendConfigOptions } from '@nuxt/kit'
234+
import type { Plugin as VitePlugin } from 'vite'
235+
import type { WebpackPluginInstance } from 'webpack'
236+
import type { RspackPluginInstance } from '@rspack/core'
237+
interface AddBuildPluginFactory {
238+
vite?: () => VitePlugin | VitePlugin[]
239+
webpack?: () => WebpackPluginInstance | WebpackPluginInstance[]
240+
rspack?: () => RspackPluginInstance | RspackPluginInstance[]
241+
}
242+
// ---cut---
214243
function addBuildPlugin (pluginFactory: AddBuildPluginFactory, options?: ExtendConfigOptions): void
215244
```
216245

docs/3.api/5.kit/3.compatibility.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Checks if constraints are met for the current Nuxt version. If not, returns an a
2020
import { defineNuxtModule, checkNuxtCompatibility } from '@nuxt/kit'
2121

2222
export default defineNuxtModule({
23-
async setup () {
23+
async setup (_options, nuxt) {
2424
const issues = await checkNuxtCompatibility({ nuxt: '^2.16.0' }, nuxt)
2525
if (issues.length) {
2626
console.warn('Nuxt compatibility issues found:\n' + issues.toString())
@@ -54,7 +54,10 @@ Asserts that constraints are met for the current Nuxt version. If not, throws an
5454

5555
### Type
5656

57-
```ts
57+
```ts twoslash
58+
// @errors: 2391
59+
import type { Nuxt, NuxtCompatibility } from '@nuxt/schema'
60+
// ---cut---
5861
function assertNuxtCompatibility(constraints: NuxtCompatibility, nuxt?: Nuxt): Promise<true>;
5962
```
6063

@@ -73,8 +76,8 @@ Checks if constraints are met for the current Nuxt version. Return `true` if all
7376
```ts twoslash
7477
import { defineNuxtModule, hasNuxtCompatibility } from '@nuxt/kit'
7578

76-
export defineNuxtModule({
77-
async setup () {
79+
export default defineNuxtModule({
80+
async setup (_options, nuxt) {
7881
const usingNewPostcss = await hasNuxtCompatibility({ nuxt: '^2.16.0' }, nuxt)
7982
if (usingNewPostcss) {
8083
// do something
@@ -83,9 +86,9 @@ export defineNuxtModule({
8386
}
8487
}
8588
})
89+
```
8690

8791
### Type
88-
```
8992

9093
```ts
9194
function hasNuxtCompatibility(constraints: NuxtCompatibility, nuxt?: Nuxt): Promise<boolean>;
@@ -112,6 +115,7 @@ export default defineNuxtModule({
112115
// do something for Nuxt 3
113116
} else {
114117
// do something else for other versions
118+
}
115119
}
116120
})
117121
```

docs/3.api/5.kit/6.context.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const setupSomeFeature = () => {
3434
### Type
3535

3636
```ts twoslash
37+
// @errors: 2391
38+
import type { Nuxt } from '@nuxt/schema'
39+
// ---cut---
3740
function useNuxt(): Nuxt
3841
```
3942

@@ -67,6 +70,12 @@ export const setupTranspilation = () => {
6770
```
6871

6972
```ts twoslash [module.ts]
73+
// @module: esnext
74+
// @filename: setupTranspilation.ts
75+
export const setupTranspilation = () => {}
76+
// @filename: module.ts
77+
import { defineNuxtModule } from '@nuxt/kit'
78+
// ---cut---
7079
import { setupTranspilation } from './setupTranspilation'
7180
7281
export default defineNuxtModule({
@@ -102,6 +111,9 @@ function setupSomething () {
102111
### Type
103112

104113
```ts twoslash
114+
// @errors: 2391
115+
import type { Nuxt } from '@nuxt/schema'
116+
// ---cut---
105117
function tryUseNuxt(): Nuxt | null
106118
```
107119

@@ -116,23 +128,39 @@ The Nuxt instance as described in the `useNuxt` section.
116128
::code-group
117129

118130
```ts twoslash [requireSiteConfig.ts]
131+
declare module '@nuxt/schema' {
132+
interface NuxtOptions {
133+
siteConfig: SiteConfig
134+
}
135+
}
136+
// ---cut---
119137
import { tryUseNuxt } from '@nuxt/kit'
120138
121139
interface SiteConfig {
122-
title: string | null
140+
title?: string
123141
}
124142
125143
export const requireSiteConfig = (): SiteConfig => {
126144
const nuxt = tryUseNuxt()
127145
if (!nuxt) {
128-
return { title: null }
146+
return {}
129147
}
130148
return nuxt.options.siteConfig
131149
}
132150
```
133151

134152
```ts twoslash [module.ts]
135-
import { useNuxt } from '@nuxt/kit'
153+
// @module: esnext
154+
// @filename: requireSiteConfig.ts
155+
interface SiteConfig {
156+
title?: string
157+
}
158+
export const requireSiteConfig = (): SiteConfig => {
159+
return {}
160+
}
161+
// @filename: module.ts
162+
// ---cut---
163+
import { defineNuxtModule, useNuxt } from '@nuxt/kit'
136164
import { requireSiteConfig } from './requireSiteConfig'
137165
138166
export default defineNuxtModule({

docs/3.api/5.kit/7.pages.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export default defineNuxtModule({
157157
```
158158

159159
```ts twoslash [runtime/auth.ts]
160+
function isAuthenticated(): boolean { return false }
161+
// ---cut---
160162
export default defineNuxtRouteMiddleware((to, from) => {
161163
// isAuthenticated() is an example method verifying if a user is authenticated
162164
if (to.path !== '/login' && isAuthenticated() === false) {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"@types/babel__helper-plugin-utils": "7.10.3",
8787
"@types/node": "22.15.3",
8888
"@types/semver": "7.7.0",
89+
"@rspack/core": "1.3.8",
8990
"@unhead/vue": "2.0.8",
9091
"@vitest/coverage-v8": "3.1.2",
9192
"@vue/test-utils": "2.4.6",
@@ -131,6 +132,7 @@
131132
"typescript": "5.8.3",
132133
"ufo": "1.6.1",
133134
"unbuild": "3.5.0",
135+
"vite": "6.3.4",
134136
"vitest": "3.1.2",
135137
"vitest-environment-nuxt": "1.0.1",
136138
"vue": "3.5.13",

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy