feat(kit): add components types (#1164)

This commit is contained in:
Xin Du (Clark) 2021-10-18 13:49:02 +01:00 committed by GitHub
parent 0c937339b9
commit dbbce5fcfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 14 additions and 24 deletions

View File

@ -20,3 +20,4 @@ export * from './types/config'
export * from './types/hooks' export * from './types/hooks'
export * from './types/module' export * from './types/module'
export * from './types/nuxt' export * from './types/nuxt'
export * from './types/components'

View File

@ -39,24 +39,7 @@ export interface ComponentsDir extends ScanDir {
transpile?: 'auto' | boolean transpile?: 'auto' | boolean
} }
export interface Options { export interface ComponentsOptions {
dirs: (string | ComponentsDir)[] dirs: (string | ComponentsDir)[]
loader: Boolean loader: Boolean
} }
type componentsDirHook = (dirs: Options['dirs']) => void | Promise<void>
type componentsExtendHook = (components: (Component | ComponentsDir | ScanDir)[]) => void | Promise<void>
declare module '@nuxt/kit' {
interface NuxtOptions {
components: boolean | Options | Options['dirs']
}
interface NuxtHooks {
'components:dirs'?: componentsDirHook
'components:extend'?: componentsExtendHook
components?: {
dirs?: componentsDirHook
extend?: componentsExtendHook
}
}
}

View File

@ -1,11 +1,13 @@
import { ConfigSchema as _ConfigSchema } from '../../schema/config' import { ConfigSchema as _ConfigSchema } from '../../schema/config'
import { ModuleInstallOptions } from './module' import { ModuleInstallOptions } from './module'
import { NuxtHooks } from './hooks' import { NuxtHooks } from './hooks'
import { ComponentsOptions } from './components'
export interface ConfigSchema extends _ConfigSchema { export interface ConfigSchema extends _ConfigSchema {
hooks: NuxtHooks, hooks: NuxtHooks,
modules: ModuleInstallOptions[] modules: ModuleInstallOptions[]
buildModules: ModuleInstallOptions[] buildModules: ModuleInstallOptions[]
components: boolean | ComponentsOptions | ComponentsOptions['dirs']
[key: string]: any [key: string]: any
// TODO: Move to schema when untyped supports type annotation // TODO: Move to schema when untyped supports type annotation

View File

@ -3,7 +3,8 @@ import type { Compiler, Configuration, Stats } from 'webpack'
import type { TSConfig } from 'pkg-types' import type { TSConfig } from 'pkg-types'
import type { NuxtConfig, NuxtOptions } from '..' import type { NuxtConfig, NuxtOptions } from '..'
import type { ModuleContainer } from '../module/container' import type { ModuleContainer } from '../module/container'
import type { NuxtTemplate, Nuxt, NuxtApp } from '../types/nuxt' import type { NuxtTemplate, Nuxt, NuxtApp } from './nuxt'
import type { Component, ComponentsDir, ScanDir, ComponentsOptions } from './components'
type HookResult = Promise<void> | void type HookResult = Promise<void> | void
@ -36,6 +37,10 @@ export interface NuxtHooks {
'app:templatesGenerated': (app: NuxtApp) => HookResult 'app:templatesGenerated': (app: NuxtApp) => HookResult
'builder:generateApp': () => HookResult 'builder:generateApp': () => HookResult
// components
'components:dirs': (dirs: ComponentsOptions['dirs']) => HookResult
'components:extend': (components: (Component | ComponentsDir | ScanDir)[]) => HookResult
// @nuxt/builder // @nuxt/builder
'build:before': 'build:before':
(builder: Builder, buildOptions: NuxtOptions['build']) => HookResult (builder: Builder, buildOptions: NuxtOptions['build']) => HookResult

View File

@ -1,6 +1,6 @@
import { createUnplugin } from 'unplugin' import { createUnplugin } from 'unplugin'
import { parseQuery, parseURL } from 'ufo' import { parseQuery, parseURL } from 'ufo'
import { Component } from './types' import { Component } from '@nuxt/kit'
interface LoaderOptions { interface LoaderOptions {
getComponents(): Component[] getComponents(): Component[]

View File

@ -1,9 +1,9 @@
import { statSync } from 'fs' import { statSync } from 'fs'
import { resolve } from 'pathe' import { resolve } from 'pathe'
import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin } from '@nuxt/kit' import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin } from '@nuxt/kit'
import type { Component, ComponentsDir } from '@nuxt/kit'
import { componentsTemplate, componentsTypeTemplate } from './templates' import { componentsTemplate, componentsTypeTemplate } from './templates'
import { scanComponents } from './scan' import { scanComponents } from './scan'
import type { Component, ComponentsDir } from './types'
import { loaderPlugin } from './loader' import { loaderPlugin } from './loader'
const isPureObjectOrString = (val: any) => (!Array.isArray(val) && typeof val === 'object') || typeof val === 'string' const isPureObjectOrString = (val: any) => (!Array.isArray(val) && typeof val === 'object') || typeof val === 'string'

View File

@ -1,7 +1,7 @@
import { basename, extname, join, dirname, relative } from 'pathe' import { basename, extname, join, dirname, relative } from 'pathe'
import globby from 'globby' import globby from 'globby'
import { pascalCase, splitByCase } from 'scule' import { pascalCase, splitByCase } from 'scule'
import type { ScanDir, Component } from './types' import type { ScanDir, Component } from '@nuxt/kit'
export function sortDirsByPathLength ({ path: pathA }: ScanDir, { path: pathB }: ScanDir): number { export function sortDirsByPathLength ({ path: pathA }: ScanDir, { path: pathB }: ScanDir): number {
return pathB.split(/[\\/]/).filter(Boolean).length - pathA.split(/[\\/]/).filter(Boolean).length return pathB.split(/[\\/]/).filter(Boolean).length - pathA.split(/[\\/]/).filter(Boolean).length

View File

@ -1,7 +1,6 @@
import { relative } from 'pathe' import { relative } from 'pathe'
import type { Component } from '@nuxt/kit'
import type { Component } from './types'
export type ComponentsTemplateOptions = { export type ComponentsTemplateOptions = {
buildDir?: string buildDir?: string