@kubb/core 5.0.0-alpha.4 → 5.0.0-alpha.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/PluginDriver-BQwm8hDd.cjs +1729 -0
- package/dist/PluginDriver-BQwm8hDd.cjs.map +1 -0
- package/dist/PluginDriver-CgXFtmNP.js +1617 -0
- package/dist/PluginDriver-CgXFtmNP.js.map +1 -0
- package/dist/index.cjs +915 -1901
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +268 -264
- package/dist/index.js +894 -1863
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +164 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +74 -0
- package/dist/mocks.js +159 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-C6NCtNqM.d.ts +2151 -0
- package/package.json +11 -14
- package/src/FileManager.ts +131 -0
- package/src/FileProcessor.ts +84 -0
- package/src/Kubb.ts +174 -85
- package/src/PluginDriver.ts +941 -0
- package/src/constants.ts +33 -43
- package/src/createAdapter.ts +25 -0
- package/src/createKubb.ts +605 -0
- package/src/createPlugin.ts +31 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +58 -0
- package/src/defineGenerator.ts +88 -100
- package/src/defineLogger.ts +13 -3
- package/src/defineParser.ts +45 -0
- package/src/definePlugin.ts +90 -7
- package/src/defineResolver.ts +453 -0
- package/src/devtools.ts +14 -14
- package/src/index.ts +12 -17
- package/src/mocks.ts +234 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +29 -9
- package/src/storages/memoryStorage.ts +2 -2
- package/src/types.ts +821 -152
- package/src/utils/TreeNode.ts +47 -9
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/executeStrategies.ts +16 -13
- package/src/utils/getBarrelFiles.ts +88 -15
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +75 -0
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -50
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -49
- package/dist/hooks.js +0 -46
- package/dist/hooks.js.map +0 -1
- package/dist/types-Bbh1o0yW.d.ts +0 -1057
- package/src/BarrelManager.ts +0 -74
- package/src/PackageManager.ts +0 -180
- package/src/PluginManager.ts +0 -668
- package/src/PromiseManager.ts +0 -40
- package/src/build.ts +0 -420
- package/src/config.ts +0 -56
- package/src/defineAdapter.ts +0 -22
- package/src/defineStorage.ts +0 -56
- package/src/errors.ts +0 -1
- package/src/hooks/index.ts +0 -8
- package/src/hooks/useKubb.ts +0 -22
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginManager.ts +0 -11
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/formatters.ts +0 -56
- package/src/utils/getConfigs.ts +0 -30
- package/src/utils/getPlugins.ts +0 -23
- package/src/utils/linters.ts +0 -25
- package/src/utils/resolveOptions.ts +0 -93
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export type Storage = {
|
|
2
|
+
/**
|
|
3
|
+
* Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`).
|
|
4
|
+
*/
|
|
5
|
+
readonly name: string
|
|
6
|
+
/**
|
|
7
|
+
* Returns `true` when an entry for `key` exists in storage.
|
|
8
|
+
*/
|
|
9
|
+
hasItem(key: string): Promise<boolean>
|
|
10
|
+
/**
|
|
11
|
+
* Returns the stored string value, or `null` when `key` does not exist.
|
|
12
|
+
*/
|
|
13
|
+
getItem(key: string): Promise<string | null>
|
|
14
|
+
/**
|
|
15
|
+
* Persists `value` under `key`, creating any required structure.
|
|
16
|
+
*/
|
|
17
|
+
setItem(key: string, value: string): Promise<void>
|
|
18
|
+
/**
|
|
19
|
+
* Removes the entry for `key`. No-ops when the key does not exist.
|
|
20
|
+
*/
|
|
21
|
+
removeItem(key: string): Promise<void>
|
|
22
|
+
/**
|
|
23
|
+
* Returns all keys, optionally filtered to those starting with `base`.
|
|
24
|
+
*/
|
|
25
|
+
getKeys(base?: string): Promise<Array<string>>
|
|
26
|
+
/**
|
|
27
|
+
* Removes all entries, optionally scoped to those starting with `base`.
|
|
28
|
+
*/
|
|
29
|
+
clear(base?: string): Promise<void>
|
|
30
|
+
/**
|
|
31
|
+
* Optional teardown hook called after the build completes.
|
|
32
|
+
*/
|
|
33
|
+
dispose?(): Promise<void>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a storage factory. Call the returned function with optional options to get the storage instance.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* export const memoryStorage = createStorage(() => {
|
|
41
|
+
* const store = new Map<string, string>()
|
|
42
|
+
* return {
|
|
43
|
+
* name: 'memory',
|
|
44
|
+
* async hasItem(key) { return store.has(key) },
|
|
45
|
+
* async getItem(key) { return store.get(key) ?? null },
|
|
46
|
+
* async setItem(key, value) { store.set(key, value) },
|
|
47
|
+
* async removeItem(key) { store.delete(key) },
|
|
48
|
+
* async getKeys(base) {
|
|
49
|
+
* const keys = [...store.keys()]
|
|
50
|
+
* return base ? keys.filter((k) => k.startsWith(base)) : keys
|
|
51
|
+
* },
|
|
52
|
+
* async clear(base) { if (!base) store.clear() },
|
|
53
|
+
* }
|
|
54
|
+
* })
|
|
55
|
+
*/
|
|
56
|
+
export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
|
|
57
|
+
return (options) => build(options ?? ({} as TOptions))
|
|
58
|
+
}
|
package/src/defineGenerator.ts
CHANGED
|
@@ -1,106 +1,94 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
1
|
+
import type { PossiblePromise } from '@internals/utils'
|
|
2
|
+
import type { FileNode, OperationNode, SchemaNode } from '@kubb/ast'
|
|
3
|
+
import type { RendererFactory } from './createRenderer.ts'
|
|
4
|
+
import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
|
|
5
5
|
|
|
6
|
-
export type
|
|
6
|
+
export type { GeneratorContext } from './types.ts'
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
8
|
+
/**
|
|
9
|
+
* A generator is a named object with optional `schema`, `operation`, and `operations`
|
|
10
|
+
* methods. Each method receives the AST node as the first argument and a typed
|
|
11
|
+
* `ctx` object as the second, giving access to `ctx.config`, `ctx.resolver`,
|
|
12
|
+
* `ctx.adapter`, `ctx.options`, `ctx.upsertFile`, etc.
|
|
13
|
+
*
|
|
14
|
+
* Generators that return renderer elements (e.g. JSX) must declare a `renderer`
|
|
15
|
+
* factory so that core knows how to process the output without coupling core
|
|
16
|
+
* to any specific renderer package.
|
|
17
|
+
*
|
|
18
|
+
* Return a renderer element, an array of `FileNode`, or `void` to handle file
|
|
19
|
+
* writing manually via `ctx.upsertFile`.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
24
|
+
*
|
|
25
|
+
* export const typeGenerator = defineGenerator<PluginTs>({
|
|
26
|
+
* name: 'typescript',
|
|
27
|
+
* renderer: jsxRenderer,
|
|
28
|
+
* schema(node, ctx) {
|
|
29
|
+
* const { adapter, resolver, root, options } = ctx
|
|
30
|
+
* return <File ...><Type node={node} resolver={resolver} /></File>
|
|
31
|
+
* },
|
|
32
|
+
* operation(node, ctx) {
|
|
33
|
+
* const { options } = ctx
|
|
34
|
+
* return <File ...><OperationType node={node} /></File>
|
|
35
|
+
* },
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown> = {
|
|
40
|
+
/**
|
|
41
|
+
* Used in diagnostic messages and debug output.
|
|
42
|
+
*/
|
|
31
43
|
name: string
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Optional renderer factory that produces a {@link Renderer} for each render cycle.
|
|
46
|
+
*
|
|
47
|
+
* Generators that return renderer elements (e.g. JSX via `@kubb/renderer-jsx`) must set this
|
|
48
|
+
* to the matching renderer factory (e.g. `jsxRenderer` from `@kubb/renderer-jsx`).
|
|
49
|
+
*
|
|
50
|
+
* Generators that only return `Array<FileNode>` or `void` do not need to set this.
|
|
51
|
+
*
|
|
52
|
+
* Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
|
|
53
|
+
* declares a `renderer` (overrides the plugin-level fallback).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
58
|
+
* export const myGenerator = defineGenerator<PluginTs>({
|
|
59
|
+
* renderer: jsxRenderer,
|
|
60
|
+
* schema(node, ctx) { return <File ...>...</File> },
|
|
61
|
+
* })
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
renderer?: RendererFactory<TElement> | null
|
|
65
|
+
/**
|
|
66
|
+
* Called for each schema node in the AST walk.
|
|
67
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
68
|
+
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
69
|
+
*/
|
|
70
|
+
schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
|
|
71
|
+
/**
|
|
72
|
+
* Called for each operation node in the AST walk.
|
|
73
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
74
|
+
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
75
|
+
*/
|
|
76
|
+
operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
|
|
77
|
+
/**
|
|
78
|
+
* Called once after all operations have been walked.
|
|
79
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
80
|
+
* plus `ctx.options` with the plugin-level options for the batch call.
|
|
81
|
+
*/
|
|
82
|
+
operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
|
|
37
83
|
}
|
|
38
84
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
export type CoreGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
49
|
-
name: string
|
|
50
|
-
type: 'core'
|
|
51
|
-
version: '2'
|
|
52
|
-
operations(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
53
|
-
operation(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
54
|
-
schema(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type ReactGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
58
|
-
name: string
|
|
59
|
-
type: 'react'
|
|
60
|
-
version: '2'
|
|
61
|
-
Operations(props: OperationsV2Props<TPlugin>): FabricReactNode
|
|
62
|
-
Operation(props: OperationV2Props<TPlugin>): FabricReactNode
|
|
63
|
-
Schema(props: SchemaV2Props<TPlugin>): FabricReactNode
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export type Generator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>
|
|
67
|
-
|
|
68
|
-
export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(
|
|
69
|
-
generator: UserReactGeneratorV2<TPlugin>,
|
|
70
|
-
): ReactGeneratorV2<TPlugin>
|
|
71
|
-
|
|
72
|
-
export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(generator: UserCoreGeneratorV2<TPlugin>): CoreGeneratorV2<TPlugin>
|
|
73
|
-
|
|
74
|
-
export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(
|
|
75
|
-
generator: UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>,
|
|
76
|
-
): unknown {
|
|
77
|
-
if (generator.type === 'react') {
|
|
78
|
-
return {
|
|
79
|
-
version: '2',
|
|
80
|
-
Operations() {
|
|
81
|
-
return null
|
|
82
|
-
},
|
|
83
|
-
Operation() {
|
|
84
|
-
return null
|
|
85
|
-
},
|
|
86
|
-
Schema() {
|
|
87
|
-
return null
|
|
88
|
-
},
|
|
89
|
-
...generator,
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return {
|
|
94
|
-
version: '2',
|
|
95
|
-
async operations() {
|
|
96
|
-
return []
|
|
97
|
-
},
|
|
98
|
-
async operation() {
|
|
99
|
-
return []
|
|
100
|
-
},
|
|
101
|
-
async schema() {
|
|
102
|
-
return []
|
|
103
|
-
},
|
|
104
|
-
...generator,
|
|
105
|
-
}
|
|
85
|
+
/**
|
|
86
|
+
* Defines a generator. Returns the object as-is with correct `this` typings.
|
|
87
|
+
* `applyHookResult` handles renderer elements and `File[]` uniformly using
|
|
88
|
+
* the generator's declared `renderer` factory.
|
|
89
|
+
*/
|
|
90
|
+
export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(
|
|
91
|
+
generator: Generator<TOptions, TElement>,
|
|
92
|
+
): Generator<TOptions, TElement> {
|
|
93
|
+
return generator
|
|
106
94
|
}
|
package/src/defineLogger.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import type { Logger, LoggerOptions, UserLogger } from './types.ts'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a logger definition into a typed {@link Logger}.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* export const myLogger = defineLogger({
|
|
8
|
+
* name: 'my-logger',
|
|
9
|
+
* install(context, options) {
|
|
10
|
+
* context.on('kubb:info', (message) => console.log('ℹ', message))
|
|
11
|
+
* context.on('kubb:error', (error) => console.error('✗', error.message))
|
|
12
|
+
* },
|
|
13
|
+
* })
|
|
14
|
+
*/
|
|
3
15
|
export function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options> {
|
|
4
|
-
return
|
|
5
|
-
...logger,
|
|
6
|
-
}
|
|
16
|
+
return logger
|
|
7
17
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { FileNode } from '@kubb/ast'
|
|
2
|
+
|
|
3
|
+
type PrintOptions = {
|
|
4
|
+
extname?: FileNode['extname']
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type Parser<TMeta extends object = any> = {
|
|
8
|
+
name: string
|
|
9
|
+
/**
|
|
10
|
+
* File extensions this parser handles.
|
|
11
|
+
* Use `undefined` to create a catch-all fallback parser.
|
|
12
|
+
*
|
|
13
|
+
* @example Handled extensions
|
|
14
|
+
* `['.ts', '.js']`
|
|
15
|
+
*/
|
|
16
|
+
extNames: Array<FileNode['extname']> | undefined
|
|
17
|
+
/**
|
|
18
|
+
* Convert a resolved file to a string.
|
|
19
|
+
*/
|
|
20
|
+
parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Defines a parser with type safety.
|
|
25
|
+
*
|
|
26
|
+
* Use this function to create parsers that transform generated files to strings
|
|
27
|
+
* based on their extension.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { defineParser } from '@kubb/core'
|
|
32
|
+
*
|
|
33
|
+
* export const jsonParser = defineParser({
|
|
34
|
+
* name: 'json',
|
|
35
|
+
* extNames: ['.json'],
|
|
36
|
+
* parse(file) {
|
|
37
|
+
* const { extractStringsFromNodes } = await import('@kubb/ast')
|
|
38
|
+
* return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
|
|
39
|
+
* },
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta> {
|
|
44
|
+
return parser
|
|
45
|
+
}
|
package/src/definePlugin.ts
CHANGED
|
@@ -1,12 +1,95 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { KubbHooks } from './Kubb.ts'
|
|
2
|
+
import type { KubbPluginSetupContext, PluginFactoryOptions } from './types.ts'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Base hook handlers for all events except `kubb:plugin:setup`.
|
|
6
|
+
* These handlers have identical signatures regardless of the plugin's
|
|
7
|
+
* `PluginFactoryOptions` generic — they are split out so that the
|
|
8
|
+
* interface below only needs to override the one event that depends on
|
|
9
|
+
* the plugin type.
|
|
10
|
+
*/
|
|
11
|
+
type PluginHooksBase = {
|
|
12
|
+
[K in Exclude<keyof KubbHooks, 'kubb:plugin:setup'>]?: (...args: KubbHooks[K]) => void | Promise<void>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Plugin hook handlers.
|
|
17
|
+
*
|
|
18
|
+
* `kubb:plugin:setup` is typed with the plugin's own `PluginFactoryOptions` so
|
|
19
|
+
* `ctx.setResolver`, `ctx.setOptions`, `ctx.options` etc. use the correct types.
|
|
20
|
+
*
|
|
21
|
+
* Uses interface + method shorthand for `kubb:plugin:setup`
|
|
22
|
+
* checking, allowing `PluginHooks<PluginTs>` to be assignable to `PluginHooks`.
|
|
23
|
+
*
|
|
24
|
+
* @template TFactory - The plugin's `PluginFactoryOptions` type.
|
|
25
|
+
*/
|
|
26
|
+
export interface PluginHooks<TFactory extends PluginFactoryOptions = PluginFactoryOptions> extends PluginHooksBase {
|
|
27
|
+
'kubb:plugin:setup'?(ctx: KubbPluginSetupContext<TFactory>): void | Promise<void>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A hook-style plugin object produced by `definePlugin`.
|
|
32
|
+
* Instead of flat lifecycle methods, it groups all handlers under a `hooks:` property
|
|
33
|
+
* (matching Astro's integration naming convention).
|
|
34
|
+
*
|
|
35
|
+
* @template TFactory - The plugin's `PluginFactoryOptions` type.
|
|
36
|
+
*/
|
|
37
|
+
export type HookStylePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
38
|
+
/**
|
|
39
|
+
* Unique name for the plugin, following the same naming convention as `createPlugin`.
|
|
40
|
+
*/
|
|
41
|
+
name: string
|
|
42
|
+
/**
|
|
43
|
+
* Plugins that must be registered before this plugin executes.
|
|
44
|
+
* An error is thrown at startup when any listed dependency is missing.
|
|
45
|
+
*/
|
|
46
|
+
dependencies?: Array<string>
|
|
47
|
+
/**
|
|
48
|
+
* The options passed by the user when calling the plugin factory.
|
|
49
|
+
*/
|
|
50
|
+
options?: TFactory['options']
|
|
51
|
+
/**
|
|
52
|
+
* Lifecycle event handlers for this plugin.
|
|
53
|
+
* Any event from the global `KubbHooks` map can be subscribed to here.
|
|
54
|
+
*/
|
|
55
|
+
hooks: PluginHooks<TFactory>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns `true` when `plugin` is a hook-style plugin created with `definePlugin`.
|
|
60
|
+
*
|
|
61
|
+
* Used by `PluginDriver` to distinguish hook-style plugins from legacy `createPlugin` plugins
|
|
62
|
+
* so it can normalize them and register their handlers on the `AsyncEventEmitter`.
|
|
63
|
+
*/
|
|
64
|
+
export function isHookStylePlugin(plugin: unknown): plugin is HookStylePlugin {
|
|
65
|
+
return typeof plugin === 'object' && plugin !== null && 'hooks' in plugin
|
|
66
|
+
}
|
|
4
67
|
|
|
5
68
|
/**
|
|
6
|
-
*
|
|
69
|
+
* Creates a plugin factory using the new hook-style (`hooks:`) API.
|
|
70
|
+
*
|
|
71
|
+
* The returned factory is called with optional options and produces a `HookStylePlugin`
|
|
72
|
+
* that coexists with plugins created via the legacy `createPlugin` API in the same
|
|
73
|
+
* `kubb.config.ts`.
|
|
74
|
+
*
|
|
75
|
+
* Lifecycle handlers are registered on the `PluginDriver`'s `AsyncEventEmitter`, enabling
|
|
76
|
+
* both the plugin's own handlers and external tooling (CLI, devtools) to observe every event.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* // With PluginFactoryOptions (recommended for real plugins)
|
|
81
|
+
* export const pluginTs = definePlugin<PluginTs>((options) => ({
|
|
82
|
+
* name: 'plugin-ts',
|
|
83
|
+
* hooks: {
|
|
84
|
+
* 'kubb:plugin:setup'(ctx) {
|
|
85
|
+
* ctx.setResolver(resolverTs) // typed as Partial<ResolverTs>
|
|
86
|
+
* },
|
|
87
|
+
* },
|
|
88
|
+
* }))
|
|
89
|
+
* ```
|
|
7
90
|
*/
|
|
8
|
-
export function definePlugin<
|
|
9
|
-
|
|
10
|
-
): (options?:
|
|
11
|
-
return (options) =>
|
|
91
|
+
export function definePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions>(
|
|
92
|
+
factory: (options: TFactory['options']) => HookStylePlugin<TFactory>,
|
|
93
|
+
): (options?: TFactory['options']) => HookStylePlugin<TFactory> {
|
|
94
|
+
return (options) => factory(options ?? ({} as TFactory['options']))
|
|
12
95
|
}
|