From dbbce5fcfa6ffbd3e94898fd2d65f35ef9eb35e0 Mon Sep 17 00:00:00 2001 From: "Xin Du (Clark)" Date: Mon, 18 Oct 2021 13:49:02 +0100 Subject: [PATCH] feat(kit): add components types (#1164) --- packages/kit/src/index.ts | 1 + .../types.ts => kit/src/types/components.ts} | 19 +------------------ packages/kit/src/types/config.ts | 2 ++ packages/kit/src/types/hooks.ts | 7 ++++++- packages/nuxt3/src/components/loader.ts | 2 +- packages/nuxt3/src/components/module.ts | 2 +- packages/nuxt3/src/components/scan.ts | 2 +- packages/nuxt3/src/components/templates.ts | 3 +-- 8 files changed, 14 insertions(+), 24 deletions(-) rename packages/{nuxt3/src/components/types.ts => kit/src/types/components.ts} (62%) diff --git a/packages/kit/src/index.ts b/packages/kit/src/index.ts index ee67fbb024..6df45e514c 100644 --- a/packages/kit/src/index.ts +++ b/packages/kit/src/index.ts @@ -20,3 +20,4 @@ export * from './types/config' export * from './types/hooks' export * from './types/module' export * from './types/nuxt' +export * from './types/components' diff --git a/packages/nuxt3/src/components/types.ts b/packages/kit/src/types/components.ts similarity index 62% rename from packages/nuxt3/src/components/types.ts rename to packages/kit/src/types/components.ts index 8ed0ef7d68..0154263842 100644 --- a/packages/nuxt3/src/components/types.ts +++ b/packages/kit/src/types/components.ts @@ -39,24 +39,7 @@ export interface ComponentsDir extends ScanDir { transpile?: 'auto' | boolean } -export interface Options { +export interface ComponentsOptions { dirs: (string | ComponentsDir)[] loader: Boolean } - -type componentsDirHook = (dirs: Options['dirs']) => void | Promise -type componentsExtendHook = (components: (Component | ComponentsDir | ScanDir)[]) => void | Promise - -declare module '@nuxt/kit' { - interface NuxtOptions { - components: boolean | Options | Options['dirs'] - } - interface NuxtHooks { - 'components:dirs'?: componentsDirHook - 'components:extend'?: componentsExtendHook - components?: { - dirs?: componentsDirHook - extend?: componentsExtendHook - } - } -} diff --git a/packages/kit/src/types/config.ts b/packages/kit/src/types/config.ts index eb73614ec6..01903df424 100644 --- a/packages/kit/src/types/config.ts +++ b/packages/kit/src/types/config.ts @@ -1,11 +1,13 @@ import { ConfigSchema as _ConfigSchema } from '../../schema/config' import { ModuleInstallOptions } from './module' import { NuxtHooks } from './hooks' +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 diff --git a/packages/kit/src/types/hooks.ts b/packages/kit/src/types/hooks.ts index c0f0ffacff..77f2a52ea9 100644 --- a/packages/kit/src/types/hooks.ts +++ b/packages/kit/src/types/hooks.ts @@ -3,7 +3,8 @@ import type { Compiler, Configuration, Stats } from 'webpack' import type { TSConfig } from 'pkg-types' import type { NuxtConfig, NuxtOptions } from '..' 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 @@ -36,6 +37,10 @@ export interface NuxtHooks { 'app:templatesGenerated': (app: NuxtApp) => HookResult 'builder:generateApp': () => HookResult + // components + 'components:dirs': (dirs: ComponentsOptions['dirs']) => HookResult + 'components:extend': (components: (Component | ComponentsDir | ScanDir)[]) => HookResult + // @nuxt/builder 'build:before': (builder: Builder, buildOptions: NuxtOptions['build']) => HookResult diff --git a/packages/nuxt3/src/components/loader.ts b/packages/nuxt3/src/components/loader.ts index 4bbd454846..5f17ad9d91 100644 --- a/packages/nuxt3/src/components/loader.ts +++ b/packages/nuxt3/src/components/loader.ts @@ -1,6 +1,6 @@ import { createUnplugin } from 'unplugin' import { parseQuery, parseURL } from 'ufo' -import { Component } from './types' +import { Component } from '@nuxt/kit' interface LoaderOptions { getComponents(): Component[] diff --git a/packages/nuxt3/src/components/module.ts b/packages/nuxt3/src/components/module.ts index 58dba6b6bf..a7f4295483 100644 --- a/packages/nuxt3/src/components/module.ts +++ b/packages/nuxt3/src/components/module.ts @@ -1,9 +1,9 @@ import { statSync } from 'fs' import { resolve } from 'pathe' import { defineNuxtModule, resolveAlias, addVitePlugin, addWebpackPlugin } from '@nuxt/kit' +import type { Component, ComponentsDir } from '@nuxt/kit' import { componentsTemplate, componentsTypeTemplate } from './templates' import { scanComponents } from './scan' -import type { Component, ComponentsDir } from './types' import { loaderPlugin } from './loader' const isPureObjectOrString = (val: any) => (!Array.isArray(val) && typeof val === 'object') || typeof val === 'string' diff --git a/packages/nuxt3/src/components/scan.ts b/packages/nuxt3/src/components/scan.ts index 25014fb14b..0cea532fa1 100644 --- a/packages/nuxt3/src/components/scan.ts +++ b/packages/nuxt3/src/components/scan.ts @@ -1,7 +1,7 @@ import { basename, extname, join, dirname, relative } from 'pathe' import globby from 'globby' 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 { return pathB.split(/[\\/]/).filter(Boolean).length - pathA.split(/[\\/]/).filter(Boolean).length diff --git a/packages/nuxt3/src/components/templates.ts b/packages/nuxt3/src/components/templates.ts index 6ae0e35dd5..9112d9c428 100644 --- a/packages/nuxt3/src/components/templates.ts +++ b/packages/nuxt3/src/components/templates.ts @@ -1,7 +1,6 @@ import { relative } from 'pathe' - -import type { Component } from './types' +import type { Component } from '@nuxt/kit' export type ComponentsTemplateOptions = { buildDir?: string