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

feat(kit): add more specific typings for schema #1988

Merged
merged 7 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion packages/kit/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export default defineBuildConfig({
'webpack',
'vite',
'nuxt',
'nuxt3'
'nuxt3',
// type imports
'@vue/compiler-core',
'vue-meta',
'rollup-plugin-visualizer',
'webpack-bundle-analyzer',
'vue'
]
})
40 changes: 40 additions & 0 deletions packages/kit/src/config/schema/_adhoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export default {
/**
* Configure Nuxt component auto-registration.
*
* Any components in the directories configured here can be used throughout your
* pages, layouts (and other components) without needing to explicitly import them.
*
* @default {{ dirs: [`~/components`] }}
* @see [Nuxt 3](https://v3.nuxtjs.org/docs/directory-structure/components) and
* [Nuxt 2](https://nuxtjs.org/docs/directory-structure/components/) documentation
* @type {boolean | typeof import('../src/types/components').ComponentsOptions | typeof import('../src/types/components').ComponentsOptions['dirs']}
pi0 marked this conversation as resolved.
Show resolved Hide resolved
*/
components: {
$resolve: (val, get) => {
if (!val) {
// Nuxt 2 and Nuxt 3 have different default values when this option is not set,
// so we defer to the module's own defaults here.
return undefined
}
if (val === undefined || val === true) {
return { dirs: ['~/components'] }
}
if (Array.isArray(val)) {
return { dirs: val }
}
return val
}
},

/**
* Configure how Nuxt auto-imports composables into your application.
*
* @see [Nuxt 3 documentation](https://v3.nuxtjs.org/docs/directory-structure/composables)
* @type {typeof import('../src/types/imports').AutoImportsOptions}
*/
autoImports: {
global: false,
dirs: []
},
}
16 changes: 11 additions & 5 deletions packages/kit/src/config/schema/_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default {
* Properties that will be set directly on `Vue.config` for vue@2.
*
* @see [vue@2 Documentation](https://vuejs.org/v2/api/#Global-Config)
* @version 2
* @type {import('vue/types/vue').VueConfiguration}
* @version 2
*/
config: {
silent: { $resolve: (val, get) => val ?? !get('dev') },
Expand All @@ -19,6 +20,7 @@ export default {
/**
* Options for the Vue compiler that will be passed at build time
* @see [documentation](https://v3.vuejs.org/api/application-config.html)
* @type {import('@vue/compiler-core').CompilerOptions}
* @version 3
*/
compilerOptions: {}
Expand Down Expand Up @@ -87,6 +89,7 @@ export default {
* Options to pass directly to `vue-meta`.
*
* @see [documentation](https://vue-meta.nuxtjs.org/api/#plugin-options).
* @type {import('vue-meta').VueMetaOptions}
* @version 2
*/
vueMeta: null,
Expand All @@ -95,6 +98,7 @@ export default {
* Set default configuration for `<head>` on every page.
*
* @see [documentation](https://vue-meta.nuxtjs.org/api/#metainfo-properties) for specifics.
* @type {import('vue-meta').MetaInfo}
* @version 2
*/
head: {
Expand Down Expand Up @@ -133,16 +137,13 @@ export default {
* ]
* }
* ```
* @type {import('nuxt3/dist/meta/runtime/types').MetaObject}
* @version 3
*/
meta: {
/** Each item in the array maps to a newly-created `<meta>` element, where object properties map to attributes. */
meta: [],
/** Each item in the array maps to a newly-created `<link>` element, where object properties map to attributes. */
link: [],
/** Each item in the array maps to a newly-created `<style>` element, where object properties map to attributes. */
style: [],
/** Each item in the array maps to a newly-created `<script>` element, where object properties map to attributes. */
script: []
},

Expand Down Expand Up @@ -177,6 +178,7 @@ export default {
* { src: '~/plugins/server-only.js', mode: 'server' } // only on server side
* ]
* ```
* @type {typeof import('../src/types/nuxt').NuxtPlugin[]}
* @version 2
*/
plugins: [],
Expand All @@ -185,6 +187,7 @@ export default {
* You may want to extend plugins or change their order. For this, you can pass
* a function using `extendPlugins`. It accepts an array of plugin objects and
* should return an array of plugin objects.
* @type {(plugins: Array<{ src: string, mode?: 'client' | 'server' }>) => Array<{ src: string, mode?: 'client' | 'server' }>}
* @version 2
*/
extendPlugins: null,
Expand All @@ -208,6 +211,7 @@ export default {
* '@/assets/css/main.scss'
* ]
* ```
* @type {string[]}
* @version 2
* @version 3
*/
Expand All @@ -217,6 +221,7 @@ export default {
* An object where each key name maps to a path to a layout .vue file.
*
* Normally there is no need to configure this directly.
* @type {Record<string, string>}
* @version 2
*/
layouts: {},
Expand All @@ -225,6 +230,7 @@ export default {
* Set a custom error page layout.
*
* Normally there is no need to configure this directly.
* @type {string}
* @version 2
*/
ErrorPage: null,
Expand Down
19 changes: 18 additions & 1 deletion packages/kit/src/config/schema/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export default {
* middleware, and so on - defaulting to `jiti` (which has support for TypeScript and ESM syntax).
*
* @see [jiti](https://github.com/unjs/jiti)
* @type {'jiti' | 'native' | ((p: string | { filename: string }) => NodeRequire)}
* @version 2
*/
createRequire: {
Expand All @@ -153,6 +154,7 @@ export default {
* or as static HTML files suitable for a CDN or other static file server (`static`).
*
* This is unrelated to `ssr`.
* @type {'server' | 'static'}
* @version 2
*/
target: {
Expand Down Expand Up @@ -194,6 +196,7 @@ export default {
* If you have set `modern: true` and are serving your app, modern will be set to `'server'`.
*
* @see [concept of modern mode](https://philipwalton.com/articles/deploying-es2015-code-in-production-today/)
* @type {'server' | 'client' | boolean}
* @version 2
*/
modern: undefined,
Expand Down Expand Up @@ -222,6 +225,7 @@ export default {
* function () {}
* ]
* ```
* @type {typeof import('../src/types/module').ModuleInstallOptions[]}
* @version 2
* @version 3
*/
Expand Down Expand Up @@ -258,6 +262,7 @@ export default {
* decreases the size of `node_modules` in production deployments. Please refer to each
* module's documentation to see if it is recommended to use `modules` or `buildModules`.
*
* @type {typeof import('../src/types/module').ModuleInstallOptions[]}
* @version 2
* @version 3
*/
Expand All @@ -284,11 +289,17 @@ export default {
* @version 2
*/
globals: {
/** @type {(globalName: string) => string} */
id: globalName => `__${globalName}`,
/** @type {(globalName: string) => string} */
nuxt: globalName => `$${globalName}`,
/** @type {(globalName: string) => string} */
context: globalName => `__${globalName.toUpperCase()}__`,
/** @type {(globalName: string) => string} */
pluginPrefix: globalName => globalName,
/** @type {(globalName: string) => string} */
readyCallback: globalName => `on${pascalCase(globalName)}Ready`,
/** @type {(globalName: string) => string} */
loadedCallback: globalName => `_on${pascalCase(globalName)}Loaded`
},

Expand Down Expand Up @@ -508,6 +519,7 @@ export default {
* </style>
* ```
*
* @type {Record<string, string>}
* @version 2
* @version 3
*/
Expand Down Expand Up @@ -570,6 +582,7 @@ export default {
* ```js
* watch: ['~/custom/*.js']
* ```
* @type {string[]}
* @version 2
*/
watch: {
Expand Down Expand Up @@ -611,6 +624,7 @@ export default {
* Your preferred code editor to launch when debugging.
*
* @see [documentation](https://github.com/yyx990803/launch-editor#supported-editors)
* @type {string}
* @version 2
*/
editor: undefined,
Expand Down Expand Up @@ -644,6 +658,7 @@ export default {
* ```
* @version 2
* @version 3
* @type {typeof import('../src/types/hooks').NuxtHooks}
*/
hooks: null,

Expand All @@ -669,6 +684,7 @@ export default {
* }
* }
* ```
* @type {typeof import('../src/types/runtime-config').PrivateRuntimeConfig}
* @version 2
* @version 3
*/
Expand All @@ -693,10 +709,11 @@ export default {
* }
* }
* ```
* @type {typeof import('../src/types/runtime-config').PublicRuntimeConfig}
* @version 2
* @version 3
*/
publicRuntimeConfig: {
$resolve: (val, get) => defu(val, { app: get('app') })
}
},
}
4 changes: 3 additions & 1 deletion packages/kit/src/config/schema/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default {
* analyzerMode: 'static'
* }
* ```
* @type {boolean | typeof import('webpack-bundle-analyzer').BundleAnalyzerPlugin.Options | typeof import('rollup-plugin-visualizer').PluginVisualizerOptions}
*/
analyze: false,

Expand Down Expand Up @@ -446,7 +447,8 @@ export default {
transpile: [({ isLegacy }) => isLegacy && 'ky']
* ```
* @version 2
* @version 3 webpack only
* @version 3
* @type {Array<string | RegExp | Function>}
*/
transpile: {
$resolve: val => [].concat(val).filter(Boolean)
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/config/schema/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
/**
* Add a message to the CLI banner by adding a string to this array.
* @type {string[]}
* @version 2
*/
badgeMessages: [],
Expand Down
10 changes: 9 additions & 1 deletion packages/kit/src/config/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import _adhoc from './_adhoc'
import _app from './_app'
import _common from './_common'
import _internal from './_internal'
Expand Down Expand Up @@ -30,6 +31,7 @@ TODO for top level normalizations: (nuxt2)
*/

export default {
..._adhoc,
..._app,
..._common,
..._internal,
Expand All @@ -40,5 +42,11 @@ export default {
server,
cli,
generate,
typescript
typescript,
// TODO: split out into separate file
/**
* Configuration that will be passed directly to Vite.
* @type {boolean | typeof import('vite').InlineConfig}
*/
vite: undefined,
}
2 changes: 1 addition & 1 deletion packages/kit/src/types/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ export interface ComponentsDir extends ScanDir {

export interface ComponentsOptions {
dirs: (string | ComponentsDir)[]
loader: Boolean
loader?: Boolean
}
18 changes: 1 addition & 17 deletions packages/kit/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
import { ConfigSchema as _ConfigSchema } from '../../schema/config'
import { ModuleInstallOptions } from './module'
import { NuxtHooks } from './hooks'
import { AutoImportsOptions } from './imports'
import { ComponentsOptions } from './components'

export interface ConfigSchema extends _ConfigSchema {
hooks: NuxtHooks,
modules: ModuleInstallOptions[]
buildModules: ModuleInstallOptions[]
components: boolean | ComponentsOptions | ComponentsOptions['dirs']
[key: string]: any

// TODO: Move to schema when untyped supports type annotation
vite: boolean | import('vite').InlineConfig
autoImports: AutoImportsOptions
}
import { ConfigSchema } from '../../schema/config'

export interface NuxtOptions extends ConfigSchema { }

Expand Down
5 changes: 3 additions & 2 deletions packages/nuxt3/src/components/module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { statSync } from 'fs'
import { resolve } from 'pathe'
import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin } from '@nuxt/kit'
import type { Component, ComponentsDir } from '@nuxt/kit'
import type { Component, ComponentsDir, ComponentsOptions } from '@nuxt/kit'
import { componentsTemplate, componentsTypeTemplate } from './templates'
import { scanComponents } from './scan'
import { loaderPlugin } from './loader'

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 } }

export default defineNuxtModule({
export default defineNuxtModule<ComponentsOptions>({
name: 'components',
configKey: 'components',
defaults: {
dirs: ['~/components']
},
Expand Down
13 changes: 8 additions & 5 deletions packages/nuxt3/src/meta/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ export interface MetaObject extends Record<string, any> {
* @default `'width=device-width, initial-scale=1'`
*/
viewport?: string
}

declare module '@nuxt/kit' {
interface ConfigSchema {
meta: MetaObject,
}
/** Each item in the array maps to a newly-created `<meta>` element, where object properties map to attributes. */
meta: Array<Record<string, any>>
/** Each item in the array maps to a newly-created `<link>` element, where object properties map to attributes. */
link: Array<Record<string, any>>
/** Each item in the array maps to a newly-created `<style>` element, where object properties map to attributes. */
style: Array<Record<string, any>>
/** Each item in the array maps to a newly-created `<script>` element, where object properties map to attributes. */
script: Array<Record<string, any>>
}
Loading