@kubb/plugin-barrel 5.0.0-beta.62 → 5.0.0-beta.64

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as __name } from "./chunk-C0LytTxp.js";
1
+ import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
2
2
  import { Plugin } from "@kubb/core";
3
3
 
4
4
  //#region src/types.d.ts
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import "./chunk-C0LytTxp.js";
1
+ import "./rolldown-runtime-C0LytTxp.js";
2
2
  import path, { extname, resolve } from "node:path";
3
3
  import { definePlugin } from "@kubb/core";
4
4
  import * as factory from "@kubb/ast/factory";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/plugin-barrel",
3
- "version": "5.0.0-beta.62",
3
+ "version": "5.0.0-beta.64",
4
4
  "description": "Barrel-file plugin for Kubb. Automatically generates index.ts re-export files per plugin output directory and an optional root barrel after all plugins complete.",
5
5
  "keywords": [
6
6
  "barrel",
@@ -17,7 +17,6 @@
17
17
  "directory": "packages/plugin-barrel"
18
18
  },
19
19
  "files": [
20
- "src",
21
20
  "dist",
22
21
  "*.d.ts",
23
22
  "*.d.cts",
@@ -42,14 +41,14 @@
42
41
  "registry": "https://registry.npmjs.org/"
43
42
  },
44
43
  "dependencies": {
45
- "@kubb/ast": "5.0.0-beta.62",
46
- "@kubb/core": "5.0.0-beta.62"
44
+ "@kubb/ast": "5.0.0-beta.64",
45
+ "@kubb/core": "5.0.0-beta.64"
47
46
  },
48
47
  "devDependencies": {
49
48
  "@internals/utils": "0.0.0"
50
49
  },
51
50
  "peerDependencies": {
52
- "@kubb/core": "5.0.0-beta.62"
51
+ "@kubb/core": "5.0.0-beta.64"
53
52
  },
54
53
  "engines": {
55
54
  "node": ">=22"
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { pluginBarrel, pluginBarrelName } from './plugin.ts'
2
- export type { BarrelType, BarrelConfig, PluginBarrelConfig } from './types.ts'
package/src/plugin.ts DELETED
@@ -1,162 +0,0 @@
1
- import path from 'node:path'
2
- import { resolve } from 'node:path'
3
- import type { FileNode } from '@kubb/ast'
4
- import { definePlugin } from '@kubb/core'
5
- import type { Config, NormalizedPlugin, Plugin } from '@kubb/core'
6
- import type { BarrelConfig, PluginBarrelConfig } from './types.ts'
7
- import { getBarrelFiles, getPluginOutputPrefix, isExcludedPath } from './utils.ts'
8
-
9
- /**
10
- * Applies a plugin's configured `output.banner`/`footer` to a barrel file, flagged as `isBarrel`.
11
- *
12
- * Resolves through the plugin's own resolver, and only when the plugin explicitly sets a
13
- * banner/footer, so barrels stay banner-free by default and never inherit the implicit
14
- * "Generated by Kubb" notice.
15
- */
16
- function withBarrelBannerFooter({ file, plugin, config }: { file: FileNode; plugin: NormalizedPlugin; config: Config }): FileNode {
17
- const output = plugin.options?.output
18
- const resolver = plugin.resolver
19
- if (!resolver) return file
20
-
21
- const hasBanner = output?.banner !== undefined
22
- const hasFooter = output?.footer !== undefined
23
- if (!hasBanner && !hasFooter) return file
24
-
25
- const context = { output, config, file: { path: file.path, baseName: file.baseName, isBarrel: true } }
26
- return {
27
- ...file,
28
- banner: hasBanner ? resolver.resolveBanner(undefined, context) : file.banner,
29
- footer: hasFooter ? resolver.resolveFooter(undefined, context) : file.footer,
30
- }
31
- }
32
-
33
- declare global {
34
- namespace Kubb {
35
- interface PluginOptionsRegistry {
36
- output: {
37
- /**
38
- * Barrel configuration for this plugin's output.
39
- * Set to `false` to disable barrel generation for this plugin entirely. Doing so also
40
- * excludes the plugin's files from the root barrel.
41
- *
42
- * Falls back to `config.output.barrel` when omitted.
43
- *
44
- * @default { type: 'named' }
45
- */
46
- barrel?: PluginBarrelConfig | false
47
- }
48
- }
49
- interface ConfigOptionsRegistry {
50
- output: {
51
- /**
52
- * Barrel configuration for the root barrel file at `config.output.path/index.ts`.
53
- * Set to `false` to disable root barrel generation. Individual plugins can override
54
- * this via their own `output.barrel`.
55
- *
56
- * @default { type: 'named' }
57
- */
58
- barrel?: BarrelConfig | false
59
- }
60
- }
61
- }
62
- }
63
-
64
- /**
65
- * Canonical plugin name for `@kubb/plugin-barrel`. Used for driver lookups
66
- * and to guard the `kubb:plugin:end` handler against reacting to its own lifecycle event.
67
- */
68
- export const pluginBarrelName = 'plugin-barrel' satisfies Plugin['name']
69
-
70
- /**
71
- * Generates an `index.ts` for every plugin output directory and one root
72
- * barrel at `config.output.path/index.ts` after the build completes. Ships
73
- * with Kubb and is registered by default in `defineConfig`.
74
- *
75
- * Each plugin inherits `output.barrel` from `config.output.barrel` (which
76
- * defaults to `{ type: 'named' }`). Set `barrel: false` on a plugin to skip
77
- * its barrel and also exclude its files from the root barrel.
78
- *
79
- * A plugin with `output.mode: 'file'` gets no per-plugin barrel, since its output
80
- * is a single file. The root barrel re-exports that file directly.
81
- *
82
- * @example
83
- * ```ts
84
- * import { defineConfig } from '@kubb/core'
85
- * import { pluginBarrel } from '@kubb/plugin-barrel'
86
- * import { pluginTs } from '@kubb/plugin-ts'
87
- * import { pluginZod } from '@kubb/plugin-zod'
88
- *
89
- * export default defineConfig({
90
- * input: { path: './petStore.yaml' },
91
- * output: { path: 'src/gen', barrel: { type: 'named' } },
92
- * plugins: [
93
- * pluginTs({ output: { path: 'types', barrel: { type: 'all' } } }),
94
- * pluginZod({ output: { path: 'schemas' } }),
95
- * pluginBarrel(),
96
- * ],
97
- * })
98
- * ```
99
- */
100
- export const pluginBarrel = definePlugin(() => {
101
- const excludedPrefixes = new Set<string>()
102
-
103
- return {
104
- name: pluginBarrelName,
105
- enforce: 'post' as const,
106
- hooks: {
107
- 'kubb:plugin:end'({ plugin, config, files, upsertFile }) {
108
- // Skip reactions to the barrel plugin's own lifecycle event
109
- if (plugin.name === pluginBarrelName) return
110
-
111
- const pluginBarrelOpt = plugin.options.output?.barrel
112
- const configBarrel = config.output.barrel
113
- const defaultBarrel = { type: 'named' } as const
114
-
115
- // Root config barrel doesn't have nested, so we add it
116
- const barrelConfig: PluginBarrelConfig | false = (() => {
117
- if (pluginBarrelOpt !== undefined) return pluginBarrelOpt
118
- if (configBarrel !== undefined) return configBarrel === false ? false : { ...configBarrel, nested: false }
119
- return defaultBarrel
120
- })()
121
-
122
- if (barrelConfig === false) {
123
- excludedPrefixes.add(getPluginOutputPrefix(plugin, config))
124
- return
125
- }
126
-
127
- // `mode: 'file'` writes a single file, so there is no directory to barrel. The root barrel
128
- // re-exports that file as a direct leaf of `config.output.path`.
129
- if (plugin.options.output.mode === 'file') {
130
- return
131
- }
132
-
133
- const barrelType = barrelConfig.type
134
- const nested = barrelConfig.nested ?? false
135
-
136
- const base = resolve(config.root, config.output.path)
137
- const target = resolve(base, plugin.options.output.path)
138
- const relative = path.relative(base, target)
139
- if (relative.startsWith('..') || path.isAbsolute(relative)) {
140
- throw new Error('Invalid output path')
141
- }
142
- for (const file of getBarrelFiles({ outputPath: target, files, barrelType, nested, recursive: true })) {
143
- upsertFile(withBarrelBannerFooter({ file, plugin, config }))
144
- }
145
- },
146
- 'kubb:plugins:end'({ files, config, upsertFile }) {
147
- const barrelConfig = config.output.barrel ?? { type: 'named' }
148
-
149
- const filteredFiles = excludedPrefixes.size === 0 ? files : files.filter((f) => !isExcludedPath(f.path, excludedPrefixes))
150
- excludedPrefixes.clear()
151
-
152
- if (barrelConfig === false) return
153
-
154
- const barrelType = barrelConfig.type
155
-
156
- for (const file of getBarrelFiles({ outputPath: resolve(config.root, config.output.path), files: filteredFiles, barrelType })) {
157
- upsertFile(file)
158
- }
159
- },
160
- },
161
- }
162
- })
package/src/types.ts DELETED
@@ -1,52 +0,0 @@
1
- /**
2
- * Barrel export strategy.
3
- *
4
- * - `'all'` generates `export * from '...'` for every file
5
- * - `'named'` generates `export { name1, name2 } from '...'` using each file's named exports
6
- */
7
- export type BarrelType = 'all' | 'named'
8
-
9
- /**
10
- * Barrel configuration at the root config level.
11
- *
12
- * @example
13
- * ```ts
14
- * barrel: { type: 'named' } // default
15
- * barrel: { type: 'all' }
16
- * barrel: false // disable barrel generation
17
- * ```
18
- */
19
- export type BarrelConfig = {
20
- /**
21
- * Export strategy for the root barrel file.
22
- * - `'all'` wildcard exports: `export * from './file'`
23
- * - `'named'` explicit exports: `export { x, y } from './file'`
24
- */
25
- type: BarrelType
26
- }
27
-
28
- /**
29
- * Barrel configuration at the plugin level.
30
- * Supports nested barrel generation in subdirectories.
31
- *
32
- * @example
33
- * ```ts
34
- * barrel: { type: 'named' } // single barrel with named exports
35
- * barrel: { type: 'all', nested: true } // hierarchical barrels with wildcard exports
36
- * barrel: { type: 'named', nested: true } // hierarchical barrels with named exports
37
- * barrel: false // disable barrel generation
38
- * ```
39
- */
40
- export type PluginBarrelConfig = {
41
- /**
42
- * Export strategy for the plugin's barrel files.
43
- * - `'all'` wildcard exports: `export * from './file'`
44
- * - `'named'` explicit exports: `export { x, y } from './file'`
45
- */
46
- type: BarrelType
47
- /**
48
- * Generate an `index.ts` in every sub-directory, each re-exporting only what's directly inside it.
49
- * Creates a hierarchical barrel structure instead of flat exports from the root.
50
- */
51
- nested?: boolean
52
- }
package/src/utils.ts DELETED
@@ -1,254 +0,0 @@
1
- import { extname, resolve } from 'node:path'
2
- import * as factory from '@kubb/ast/factory'
3
- import type { ExportNode, FileNode, SourceNode } from '@kubb/ast'
4
- import type { Config, NormalizedPlugin } from '@kubb/core'
5
- import { type BuildTree, buildTree, toPosixPath } from '@internals/utils'
6
- import type { BarrelType } from './types.ts'
7
-
8
- const SOURCE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx'])
9
- const BARREL_SUFFIX = `/index.ts`
10
-
11
- function toRelativeModulePath(fromDir: string, filePath: string): string {
12
- return `./${filePath.slice(fromDir.length + 1)}`
13
- }
14
-
15
- function isBarrelPath(path: string): boolean {
16
- return path.endsWith(BARREL_SUFFIX)
17
- }
18
-
19
- function makeBarrel(dirPath: string, exports: Array<ExportNode>): FileNode {
20
- return factory.createFile({
21
- baseName: 'index.ts',
22
- path: `${dirPath}${BARREL_SUFFIX}`,
23
- exports,
24
- sources: [],
25
- imports: [],
26
- // Default to no banner/footer. The barrel plugin resolves a configured plugin
27
- // banner/footer (with isBarrel: true) afterwards, so a `banner` function can
28
- // decide per file whether a barrel should carry a directive like "use server".
29
- banner: undefined,
30
- footer: undefined,
31
- })
32
- }
33
-
34
- type LeafContext = {
35
- dirPath: string
36
- leafPath: string
37
- sourceFile: FileNode | null
38
- }
39
-
40
- type LeafStrategy = (ctx: LeafContext) => Array<ExportNode>
41
-
42
- function hasOnlyNonIndexableSources(sources: ReadonlyArray<SourceNode>): boolean {
43
- if (sources.length === 0) return false
44
- for (const source of sources) {
45
- if (source.isIndexable) return false
46
- }
47
- return true
48
- }
49
-
50
- function partitionIndexableNames(sources: ReadonlyArray<SourceNode>): Map<boolean, Set<string>> {
51
- const byTypeOnly = new Map<boolean, Set<string>>([
52
- [false, new Set()],
53
- [true, new Set()],
54
- ])
55
- for (const source of sources) {
56
- if (!source.isIndexable || !source.name) continue
57
- byTypeOnly.get(Boolean(source.isTypeOnly))!.add(source.name)
58
- }
59
- return byTypeOnly
60
- }
61
-
62
- const allStrategy: LeafStrategy = ({ dirPath, leafPath, sourceFile }) => {
63
- if (sourceFile && hasOnlyNonIndexableSources(sourceFile.sources)) return []
64
- return [factory.createExport({ path: toRelativeModulePath(dirPath, leafPath) })]
65
- }
66
-
67
- const namedStrategy: LeafStrategy = ({ dirPath, leafPath, sourceFile }) => {
68
- const modulePath = toRelativeModulePath(dirPath, leafPath)
69
-
70
- if (!sourceFile) return [factory.createExport({ path: modulePath })]
71
-
72
- const namesByTypeOnly = partitionIndexableNames(sourceFile.sources)
73
- const valueNames = namesByTypeOnly.get(false)!
74
- const typeNames = namesByTypeOnly.get(true)!
75
-
76
- if (valueNames.size === 0 && typeNames.size === 0) {
77
- if (sourceFile.sources.length > 0) return []
78
- return [factory.createExport({ path: modulePath })]
79
- }
80
-
81
- const exports: Array<ExportNode> = []
82
- if (valueNames.size > 0) {
83
- exports.push(factory.createExport({ name: [...valueNames].sort(), path: modulePath }))
84
- }
85
- if (typeNames.size > 0) {
86
- exports.push(factory.createExport({ name: [...typeNames].sort(), path: modulePath, isTypeOnly: true }))
87
- }
88
- return exports
89
- }
90
-
91
- const LEAF_STRATEGIES: ReadonlyMap<BarrelType, LeafStrategy> = new Map([
92
- ['all', allStrategy],
93
- ['named', namedStrategy],
94
- ])
95
-
96
- type LeafWalkParams = {
97
- sourceFiles: ReadonlyMap<string, FileNode>
98
- strategy: LeafStrategy
99
- recursive: boolean
100
- }
101
-
102
- /**
103
- * Post-order walk that yields a barrel per visited directory.
104
- * Returns the list of leaf file paths collected in this subtree (used by the parent call).
105
- */
106
- function* walkAllOrNamed(node: BuildTree, params: LeafWalkParams, isRoot: boolean): Generator<FileNode, Array<string>> {
107
- const subtreeLeaves: Array<string> = []
108
-
109
- for (const child of node.children) {
110
- if (child.isFile) {
111
- if (!isBarrelPath(child.path)) subtreeLeaves.push(child.path)
112
- continue
113
- }
114
-
115
- const childLeaves = yield* walkAllOrNamed(child, params, false)
116
- for (const leaf of childLeaves) subtreeLeaves.push(leaf)
117
- }
118
-
119
- if (!isRoot && !params.recursive) return subtreeLeaves
120
-
121
- const exports = subtreeLeaves.flatMap((leafPath) => params.strategy({ dirPath: node.path, leafPath, sourceFile: params.sourceFiles.get(leafPath) ?? null }))
122
-
123
- if (exports.length > 0) {
124
- yield makeBarrel(node.path, exports)
125
- }
126
-
127
- return subtreeLeaves
128
- }
129
-
130
- /**
131
- * Recursive walk that yields one barrel per directory, re-exporting files and sub-barrels.
132
- * Used when nested: true.
133
- */
134
- function* walkNested(node: BuildTree): Generator<FileNode> {
135
- const exports: Array<ExportNode> = []
136
-
137
- for (const child of node.children) {
138
- if (child.isFile) {
139
- if (isBarrelPath(child.path)) continue
140
- exports.push(factory.createExport({ path: toRelativeModulePath(node.path, child.path) }))
141
- continue
142
- }
143
-
144
- yield* walkNested(child)
145
- exports.push(factory.createExport({ path: toRelativeModulePath(node.path, `${child.path}${BARREL_SUFFIX}`) }))
146
- }
147
-
148
- if (exports.length > 0) {
149
- yield makeBarrel(node.path, exports)
150
- }
151
- }
152
-
153
- type IndexedFiles = {
154
- sourceFiles: ReadonlyMap<string, FileNode>
155
- paths: ReadonlyArray<string>
156
- }
157
-
158
- function indexRelevantFiles(files: ReadonlyArray<FileNode>, outputPath: string): IndexedFiles {
159
- const outputPrefix = `${toPosixPath(outputPath)}/`
160
- const sourceFiles = new Map<string, FileNode>()
161
- const paths: Array<string> = []
162
-
163
- for (const file of files) {
164
- const normalized = toPosixPath(file.path)
165
- if (!normalized.startsWith(outputPrefix)) continue
166
- if (isBarrelPath(normalized)) continue
167
- if (!SOURCE_EXTENSIONS.has(extname(normalized))) continue
168
-
169
- sourceFiles.set(normalized, file)
170
- paths.push(normalized)
171
- }
172
-
173
- return { sourceFiles, paths }
174
- }
175
-
176
- type GetBarrelFilesParams = {
177
- /**
178
- * Absolute directory the barrel(s) should be rooted at.
179
- * Only files living under this path are considered.
180
- */
181
- outputPath: string
182
- /**
183
- * Pool of generated files to scan for indexable sources.
184
- */
185
- files: ReadonlyArray<FileNode>
186
- /**
187
- * Export strategy used when emitting each barrel.
188
- * - `'all'` re-exports the whole module (`export * from './x'`)
189
- * - `'named'` re-exports only the indexable named symbols
190
- */
191
- barrelType: BarrelType
192
- /**
193
- * Generate an `index.ts` in every sub-directory, each re-exporting only what's directly inside it (hierarchical).
194
- * When false, uses flat generation strategy with optional recursive subdirectory barrels.
195
- */
196
- nested?: boolean
197
- /**
198
- * Also generate a barrel for each sub-directory when nested is false.
199
- * No effect when nested is true (always generates hierarchical structure).
200
- */
201
- recursive?: boolean
202
- }
203
-
204
- /**
205
- * Yields barrel `FileNode`s for the directory rooted at `outputPath`.
206
- *
207
- * @example
208
- * ```ts
209
- * for (const file of getBarrelFiles({ outputPath, files, barrelType })) {
210
- * upsertFile(file)
211
- * }
212
- * // or collect into an array
213
- * const barrels = [...getBarrelFiles({ outputPath, files, barrelType })]
214
- * ```
215
- */
216
- export function* getBarrelFiles({ outputPath, files, barrelType, nested = false, recursive = false }: GetBarrelFilesParams): Generator<FileNode> {
217
- const { sourceFiles, paths } = indexRelevantFiles(files, outputPath)
218
- if (paths.length === 0) return
219
-
220
- const tree = buildTree(outputPath, paths)
221
-
222
- if (nested) {
223
- yield* walkNested(tree)
224
- return
225
- }
226
-
227
- const strategy = LEAF_STRATEGIES.get(barrelType)
228
- if (!strategy) return
229
-
230
- yield* walkAllOrNamed(tree, { sourceFiles, strategy, recursive }, true)
231
- }
232
-
233
- /**
234
- * Builds a POSIX-normalized prefix for a plugin's output. A directory output gets a trailing `/`,
235
- * while a `mode: 'file'` output (the path is the file itself) gets the exact path with no trailing `/`.
236
- *
237
- * Used to detect (and later exclude) files generated by plugins that opted out of the root barrel.
238
- */
239
- export function getPluginOutputPrefix(plugin: NormalizedPlugin, config: Config): string {
240
- const resolved = toPosixPath(resolve(config.root, config.output.path, plugin.options.output.path))
241
- return plugin.options.output.mode === 'file' ? resolved : `${resolved}/`
242
- }
243
-
244
- /**
245
- * Returns `true` when `filePath` lives under any of the given excluded prefixes. A prefix with a
246
- * trailing `/` matches a directory subtree, and a prefix without one matches that exact file
247
- * (used for `mode: 'file'` outputs).
248
- *
249
- * Both sides are POSIX-normalized so Windows backslash paths match correctly.
250
- */
251
- export function isExcludedPath(filePath: string, prefixes: ReadonlySet<string>): boolean {
252
- const normalized = toPosixPath(filePath)
253
- return prefixes.values().some((prefix) => (prefix.endsWith('/') ? normalized.startsWith(prefix) : normalized === prefix))
254
- }