fix(nuxt): expose `nuxt/schema` subpath for augmentation (#18922)

This commit is contained in:
Daniel Roe 2023-02-13 22:42:04 +00:00 committed by GitHub
parent e362061587
commit 6016aef859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 41 additions and 34 deletions

View File

@ -54,7 +54,7 @@ It is also possible to type app config manually. There are two possible things y
`AppConfigInput` might be used by module authors who are declaring what valid _input_ options are when setting app config. This will not affect the type of `useAppConfig()`.
```ts [index.d.ts]
declare module '@nuxt/schema' {
declare module 'nuxt/schema' {
interface AppConfigInput {
/** Theme configuration */
theme?: {
@ -77,7 +77,7 @@ Be careful when typing `AppConfig` as you will overwrite the types Nuxt infers f
::
```ts [index.d.ts]
declare module '@nuxt/schema' {
declare module 'nuxt/schema' {
interface AppConfig {
// This will entirely replace the existing inferred `theme` property
theme: {

View File

@ -147,7 +147,7 @@ Nuxt tries to automatically generate a typescript interface from provided runtim
It is also possible to type your runtime config manually:
```ts [index.d.ts]
declare module '@nuxt/schema' {
declare module 'nuxt/schema' {
interface RuntimeConfig {
apiSecret: string
public: {

View File

@ -1,4 +1,4 @@
import type { NuxtConfig } from '@nuxt/schema'
export { NuxtConfig } from '@nuxt/schema'
import type { NuxtConfig } from 'nuxt/schema'
export { NuxtConfig } from 'nuxt/schema'
export declare function defineNuxtConfig(config: NuxtConfig): NuxtConfig

View File

@ -17,6 +17,10 @@
"import": "./config.mjs",
"require": "./config.cjs"
},
"./schema": {
"types": "./schema.d.ts",
"import": "./schema.mjs"
},
"./app": "./dist/app/index.mjs",
"./package.json": "./package.json"
},
@ -30,7 +34,8 @@
"bin",
"types.d.ts",
"dist",
"config.*"
"config.*",
"schema.*"
],
"scripts": {
"prepack": "unbuild"

1
packages/nuxt/schema.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from '@nuxt/schema'

1
packages/nuxt/schema.mjs Normal file
View File

@ -0,0 +1 @@
export * from '@nuxt/schema'

View File

@ -1,10 +1,10 @@
import { defineComponent, createStaticVNode, computed, ref, watch } from 'vue'
import { debounce } from 'perfect-debounce'
import { hash } from 'ohash'
import type { MetaObject } from '@nuxt/schema'
import { appendHeader } from 'h3'
// eslint-disable-next-line import/no-restricted-paths
import type { NuxtIslandResponse } from '../../core/runtime/nitro/renderer'
import type { MetaObject } from 'nuxt/schema'
import { useNuxtApp } from '#app/nuxt'
import { useRequestEvent } from '#app/composables/ssr'
import { useHead } from '#app/composables/head'

View File

@ -1,6 +1,6 @@
import type { AppConfig } from '@nuxt/schema'
import { reactive } from 'vue'
import { useNuxtApp } from './nuxt'
import type { AppConfig } from 'nuxt/schema'
// @ts-ignore
import __appConfig from '#build/app.config.mjs'

View File

@ -3,12 +3,12 @@ import { getCurrentInstance, reactive } from 'vue'
import type { App, onErrorCaptured, VNode, Ref } from 'vue'
import type { Hookable } from 'hookable'
import { createHooks } from 'hookable'
import type { RuntimeConfig, AppConfigInput } from '@nuxt/schema'
import { getContext } from 'unctx'
import type { SSRContext } from 'vue-bundle-renderer/runtime'
import type { H3Event } from 'h3'
// eslint-disable-next-line import/no-restricted-paths
import type { NuxtIslandContext } from '../core/runtime/nitro/renderer'
import type { RuntimeConfig, AppConfigInput } from 'nuxt/schema'
const nuxtAppCtx = getContext<NuxtApp>('nuxt-app')

View File

@ -1,12 +1,12 @@
import { pathToFileURL } from 'node:url'
import { createUnplugin } from 'unplugin'
import { parseQuery, parseURL } from 'ufo'
import type { Component, ComponentsOptions } from '@nuxt/schema'
import { genDynamicImport, genImport } from 'knitwork'
import MagicString from 'magic-string'
import { pascalCase } from 'scule'
import { resolve } from 'pathe'
import { distDir } from '../dirs'
import type { Component, ComponentsOptions } from 'nuxt/schema'
interface LoaderOptions {
getComponents (): Component[]

View File

@ -1,12 +1,12 @@
import { statSync } from 'node:fs'
import { relative, resolve } from 'pathe'
import { defineNuxtModule, resolveAlias, addTemplate, addPluginTemplate, updateTemplates } from '@nuxt/kit'
import type { Component, ComponentsDir, ComponentsOptions } from '@nuxt/schema'
import { distDir } from '../dirs'
import { componentsPluginTemplate, componentsTemplate, componentsIslandsTemplate, componentsTypeTemplate } from './templates'
import { scanComponents } from './scan'
import { loaderPlugin } from './loader'
import { TreeShakeTemplatePlugin } from './tree-shake'
import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'
const isPureObjectOrString = (val: any) => (!Array.isArray(val) && typeof val === 'object') || typeof val === 'string'
const isDirectory = (p: string) => { try { return statSync(p).isDirectory() } catch (_e) { return false } }

View File

@ -1,11 +1,11 @@
import { basename, extname, join, dirname, relative } from 'pathe'
import { globby } from 'globby'
import { pascalCase, splitByCase } from 'scule'
import type { Component, ComponentsDir } from '@nuxt/schema'
import { isIgnored } from '@nuxt/kit'
// eslint-disable-next-line vue/prefer-import-from-vue
import { hyphenate } from '@vue/shared'
import { withTrailingSlash } from 'ufo'
import type { Component, ComponentsDir } from 'nuxt/schema'
/**
* Scan the components inside different components folders

View File

@ -1,6 +1,6 @@
import { isAbsolute, relative } from 'pathe'
import type { Component, Nuxt, NuxtPluginTemplate, NuxtTemplate } from '@nuxt/schema'
import { genDynamicImport, genExport, genImport, genObjectFromRawEntries } from 'knitwork'
import type { Component, Nuxt, NuxtPluginTemplate, NuxtTemplate } from 'nuxt/schema'
export interface ComponentsTemplateContext {
nuxt: Nuxt

View File

@ -5,9 +5,9 @@ import { walk } from 'estree-walker'
import type { CallExpression, Property, Identifier, ImportDeclaration, MemberExpression, Literal, ReturnStatement, VariableDeclaration, ObjectExpression, Node } from 'estree'
import { createUnplugin } from 'unplugin'
import escapeStringRegexp from 'escape-string-regexp'
import type { Component } from '@nuxt/schema'
import { resolve } from 'pathe'
import { distDir } from '../dirs'
import type { Component } from 'nuxt/schema'
interface TreeShakeTemplatePluginOptions {
sourcemap?: boolean

View File

@ -1,11 +1,11 @@
import { promises as fsp } from 'node:fs'
import { dirname, resolve, join } from 'pathe'
import { defu } from 'defu'
import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from '@nuxt/schema'
import { findPath, resolveFiles, normalizePlugin, normalizeTemplate, compileTemplate, templateUtils, tryResolveModule, resolvePath, resolveAlias } from '@nuxt/kit'
import * as defaultTemplates from './templates'
import { getNameFromPath, hasSuffix, uniqueBy } from './utils'
import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from 'nuxt/schema'
export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp {
return defu(options, {

View File

@ -1,9 +1,9 @@
import chokidar from 'chokidar'
import type { Nuxt } from '@nuxt/schema'
import { importModule, isIgnored } from '@nuxt/kit'
import { debounce } from 'perfect-debounce'
import { normalize } from 'pathe'
import { createApp, generateApp as _generateApp } from './app'
import type { Nuxt } from 'nuxt/schema'
export async function build (nuxt: Nuxt) {
const app = createApp(nuxt)

View File

@ -2,7 +2,6 @@ import { existsSync, promises as fsp } from 'node:fs'
import { resolve, join } from 'pathe'
import { createNitro, createDevServer, build, prepare, copyPublicAssets, writeTypes, scanHandlers, prerender } from 'nitropack'
import type { NitroConfig, Nitro } from 'nitropack'
import type { Nuxt } from '@nuxt/schema'
import { logger, resolvePath } from '@nuxt/kit'
import escapeRE from 'escape-string-regexp'
import { defu } from 'defu'
@ -12,6 +11,7 @@ import { createHeadCore } from 'unhead'
import { renderSSRHead } from '@unhead/ssr'
import { distDir } from '../dirs'
import { ImportProtectionPlugin } from './plugins/import-protection'
import type { Nuxt } from 'nuxt/schema'
export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// Resolve config

View File

@ -1,6 +1,5 @@
import { join, normalize, resolve } from 'pathe'
import { createHooks, createDebugger } from 'hookable'
import type { Nuxt, NuxtOptions, NuxtHooks } from '@nuxt/schema'
import type { LoadNuxtOptions } from '@nuxt/kit'
import { loadNuxtConfig, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule, addPlugin } from '@nuxt/kit'
@ -22,6 +21,7 @@ import { DevOnlyPlugin } from './plugins/dev-only'
import { addModuleTranspiles } from './modules'
import { initNitro } from './nitro'
import schemaModule from './schema'
import type { Nuxt, NuxtOptions, NuxtHooks } from 'nuxt/schema'
export function createNuxt (options: NuxtOptions): Nuxt {
const hooks = createHooks<NuxtHooks>()

View File

@ -2,8 +2,8 @@ import { createRequire } from 'node:module'
import { createUnplugin } from 'unplugin'
import { logger } from '@nuxt/kit'
import { isAbsolute, join, relative } from 'pathe'
import type { Nuxt } from '@nuxt/schema'
import escapeRE from 'escape-string-regexp'
import type { Nuxt } from 'nuxt/schema'
const _require = createRequire(import.meta.url)

View File

@ -1,7 +1,7 @@
import type { Nuxt, NuxtApp } from '@nuxt/schema'
import { normalize } from 'pathe'
import { createTransformer } from 'unctx/transform'
import { createUnplugin } from 'unplugin'
import type { Nuxt, NuxtApp } from 'nuxt/schema'
const TRANSFORM_MARKER = '/* _processed_nuxt_unctx_transform */\n'

View File

@ -152,7 +152,7 @@ export default defineNuxtModule({
`
export type CustomAppConfig = Exclude<NuxtCustomSchema['appConfig'], undefined>
declare module '@nuxt/schema' {
declare module 'nuxt/schema' {
interface NuxtConfig extends NuxtCustomSchema {}
interface NuxtOptions extends NuxtCustomSchema {}
interface AppConfigInput extends CustomAppConfig {}

View File

@ -1,4 +1,3 @@
import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema'
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork'
import { isAbsolute, join, relative, resolve } from 'pathe'
import { resolveSchema, generateTypes } from 'untyped'
@ -7,6 +6,7 @@ import { hash } from 'ohash'
import { camelCase } from 'scule'
import { resolvePath } from 'mlly'
import { filename } from 'pathe/utils'
import type { Nuxt, NuxtApp, NuxtTemplate } from 'nuxt/schema'
export interface TemplateContext {
nuxt: Nuxt
@ -126,8 +126,8 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)])
return [
"import { NuxtModule } from '@nuxt/schema'",
"declare module '@nuxt/schema' {",
"import { NuxtModule } from 'nuxt/schema'",
"declare module 'nuxt/schema' {",
' interface NuxtConfig {',
...modules.map(([configKey, importName]) =>
` [${configKey}]?: typeof ${genDynamicImport(importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
@ -200,7 +200,7 @@ ${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from $
declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
type ResolvedAppConfig = Defu<typeof inlineConfig, [${app.configs.map((_id: string, index: number) => `typeof cfg${index}`).join(', ')}]>
declare module '@nuxt/schema' {
declare module 'nuxt/schema' {
interface AppConfig extends ResolvedAppConfig { }
}
`

View File

@ -1,6 +1,6 @@
import type { HeadEntryOptions, UseHeadInput, ActiveHeadEntry } from '@vueuse/head'
import type { HeadAugmentations } from '@nuxt/schema'
import { useSeoMeta as _useSeoMeta } from '@vueuse/head'
import type { HeadAugmentations } from 'nuxt/schema'
import { useNuxtApp } from '#app/nuxt'
/**

View File

@ -1,5 +1,5 @@
import type { UseHeadInput } from '@vueuse/head'
import type { HeadAugmentations } from '@nuxt/schema'
import type { HeadAugmentations } from 'nuxt/schema'
export * from './composables'

View File

@ -2,9 +2,9 @@ import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate, resolve
import { isAbsolute, join, relative, resolve, normalize } from 'pathe'
import type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports } from 'unimport'
import type { ImportsOptions, ImportPresetWithDeprecation } from '@nuxt/schema'
import { TransformPlugin } from './transform'
import { defaultPresets } from './presets'
import type { ImportsOptions, ImportPresetWithDeprecation } from 'nuxt/schema'
export default defineNuxtModule<Partial<ImportsOptions>>({
meta: {

View File

@ -2,8 +2,8 @@ import { pathToFileURL } from 'node:url'
import { createUnplugin } from 'unplugin'
import { parseQuery, parseURL } from 'ufo'
import type { Unimport } from 'unimport'
import type { ImportsOptions } from '@nuxt/schema'
import { normalize } from 'pathe'
import type { ImportsOptions } from 'nuxt/schema'
export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: { ctx: Unimport, options: Partial<ImportsOptions>, sourcemap?: boolean }) => {
return {

View File

@ -3,12 +3,12 @@ import { defineNuxtModule, addTemplate, addPlugin, addVitePlugin, addWebpackPlug
import { relative, resolve } from 'pathe'
import { genString, genImport, genObjectFromRawEntries } from 'knitwork'
import escapeRE from 'escape-string-regexp'
import type { NuxtApp, NuxtPage } from '@nuxt/schema'
import { joinURL } from 'ufo'
import { distDir } from '../dirs'
import { resolvePagesRoutes, normalizeRoutes } from './utils'
import type { PageMetaPluginOptions } from './page-meta'
import { PageMetaPlugin } from './page-meta'
import type { NuxtApp, NuxtPage } from 'nuxt/schema'
export default defineNuxtModule({
meta: {

View File

@ -1,6 +1,6 @@
import type { RouterConfig } from '@nuxt/schema'
import type { RouterScrollBehavior, RouteLocationNormalized } from 'vue-router'
import { nextTick } from 'vue'
import type { RouterConfig } from 'nuxt/schema'
import { useNuxtApp } from '#app/nuxt'
// @ts-ignore
import { appPageTransition as defaultPageTransition } from '#build/nuxt.config.mjs'

View File

@ -1,12 +1,12 @@
import { extname, normalize, relative, resolve } from 'pathe'
import { encodePath } from 'ufo'
import type { NuxtPage } from '@nuxt/schema'
import { resolveFiles, useNuxt } from '@nuxt/kit'
import { genImport, genDynamicImport, genArrayFromRaw, genSafeVariableName } from 'knitwork'
import escapeRE from 'escape-string-regexp'
import { filename } from 'pathe/utils'
import { hash } from 'ohash'
import { uniqueBy } from '../core/utils'
import type { NuxtPage } from 'nuxt/schema'
enum SegmentParserState {
initial,

View File

@ -1,7 +1,7 @@
import { resolve } from 'node:path'
import type { ComponentsDir } from '@nuxt/schema'
import { expect, it, vi } from 'vitest'
import { scanComponents } from '../src/components/scan'
import type { ComponentsDir } from 'nuxt/schema'
const fixtureDir = resolve(__dirname, 'fixture')
const rFixture = (...p: string[]) => resolve(fixtureDir, ...p)

View File

@ -1,7 +1,7 @@
/// <reference types="nitropack" />
export * from './dist/index'
import type { SchemaDefinition } from '@nuxt/schema'
import type { SchemaDefinition } from 'nuxt/schema'
declare global {
const defineNuxtConfig: typeof import('nuxt/config')['defineNuxtConfig']