@kubb/middleware-barrel 5.0.0-beta.75 → 5.0.0-beta.8

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/src/types.ts CHANGED
@@ -1,14 +1,54 @@
1
1
  /**
2
- * Barrel re-export style for generated `index.ts` files.
2
+ * Barrel export strategy.
3
3
  *
4
4
  * - `'all'` — generates `export * from '...'` for every file
5
5
  * - `'named'` — generates `export { name1, name2 } from '...'` using each file's named exports
6
- * - `'propagate'` — generates intermediate barrel files for every sub-directory so consumers can import from any depth
7
6
  */
8
- export type BarrelType = 'all' | 'named' | 'propagate'
7
+ export type BarrelType = 'all' | 'named'
9
8
 
10
9
  /**
11
- * Barrel re-export style for the root barrel at `config.output.path/index.ts`.
12
- * `'propagate'` is only available at the per-plugin level.
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
+ * ```
13
39
  */
14
- export type RootBarrelType = Exclude<BarrelType, 'propagate'>
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
+ * @default false
52
+ */
53
+ nested?: boolean
54
+ }
@@ -1,12 +1,12 @@
1
- import { extname } from 'node:path'
1
+ import { extname, resolve } from 'node:path'
2
2
  import { createExport, createFile } from '@kubb/ast'
3
3
  import type { ExportNode, FileNode, SourceNode } from '@kubb/ast'
4
- import { BARREL_FILENAME } from '../constants.ts'
5
- import type { BarrelType } from '../types.ts'
4
+ import type { Config, NormalizedPlugin } from '@kubb/core'
6
5
  import { type BuildTree, buildTree, toPosixPath } from '@internals/utils'
6
+ import type { BarrelType } from './types.ts'
7
7
 
8
8
  const SOURCE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx'])
9
- const BARREL_SUFFIX = `/${BARREL_FILENAME}`
9
+ const BARREL_SUFFIX = `/index.ts`
10
10
 
11
11
  function toRelativeModulePath(fromDir: string, filePath: string): string {
12
12
  return `./${filePath.slice(fromDir.length + 1)}`
@@ -18,7 +18,7 @@ function isBarrelPath(path: string): boolean {
18
18
 
19
19
  function makeBarrel(dirPath: string, exports: Array<ExportNode>): FileNode {
20
20
  return createFile({
21
- baseName: BARREL_FILENAME,
21
+ baseName: 'index.ts',
22
22
  path: `${dirPath}${BARREL_SUFFIX}`,
23
23
  exports,
24
24
  sources: [],
@@ -87,7 +87,7 @@ const namedStrategy: LeafStrategy = ({ dirPath, leafPath, sourceFile }) => {
87
87
  return exports
88
88
  }
89
89
 
90
- const LEAF_STRATEGIES: ReadonlyMap<Exclude<BarrelType, 'propagate'>, LeafStrategy> = new Map([
90
+ const LEAF_STRATEGIES: ReadonlyMap<BarrelType, LeafStrategy> = new Map([
91
91
  ['all', allStrategy],
92
92
  ['named', namedStrategy],
93
93
  ])
@@ -127,8 +127,9 @@ function walkAllOrNamed(node: BuildTree, params: LeafWalkParams, isRoot: boolean
127
127
 
128
128
  /**
129
129
  * Recursive walk that emits one barrel per directory, re-exporting files and sub-barrels.
130
+ * Used when nested: true.
130
131
  */
131
- function walkPropagate(node: BuildTree, out: Array<FileNode>): void {
132
+ function walkNested(node: BuildTree, out: Array<FileNode>): void {
132
133
  const exports: Array<ExportNode> = []
133
134
 
134
135
  for (const child of node.children) {
@@ -138,7 +139,7 @@ function walkPropagate(node: BuildTree, out: Array<FileNode>): void {
138
139
  continue
139
140
  }
140
141
 
141
- walkPropagate(child, out)
142
+ walkNested(child, out)
142
143
  exports.push(createExport({ path: toRelativeModulePath(node.path, `${child.path}${BARREL_SUFFIX}`) }))
143
144
  }
144
145
 
@@ -181,15 +182,21 @@ type GetBarrelFilesParams = {
181
182
  */
182
183
  files: ReadonlyArray<FileNode>
183
184
  /**
184
- * Re-export style used when emitting each barrel.
185
+ * Export strategy used when emitting each barrel.
185
186
  * - `'all'` re-exports the whole module (`export * from './x'`)
186
187
  * - `'named'` re-exports only the indexable named symbols
187
- * - `'propagate'` emits one barrel per directory and chains sub-barrels
188
188
  */
189
189
  barrelType: BarrelType
190
190
  /**
191
- * Also generate a barrel for each sub-directory of `outputPath`.
192
- * No effect for `barrelType: 'propagate'`, which always recurses.
191
+ * Generate an `index.ts` in every sub-directory, each re-exporting only what's directly inside it (hierarchical).
192
+ * When false, uses flat generation strategy with optional recursive subdirectory barrels.
193
+ *
194
+ * @default false
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).
193
200
  *
194
201
  * @default false
195
202
  */
@@ -199,15 +206,16 @@ type GetBarrelFilesParams = {
199
206
  /**
200
207
  * Generates barrel `FileNode`s for the directory rooted at `outputPath`.
201
208
  */
202
- export function getBarrelFiles({ outputPath, files, barrelType, recursive = false }: GetBarrelFilesParams): Array<FileNode> {
209
+ export function getBarrelFiles({ outputPath, files, barrelType, nested = false, recursive = false }: GetBarrelFilesParams): Array<FileNode> {
203
210
  const { sourceFiles, paths } = indexRelevantFiles(files, outputPath)
204
211
  if (paths.length === 0) return []
205
212
 
206
213
  const tree = buildTree(outputPath, paths)
207
214
  const result: Array<FileNode> = []
208
215
 
209
- if (barrelType === 'propagate') {
210
- walkPropagate(tree, result)
216
+ // Use nested walk for hierarchical barrel structure
217
+ if (nested) {
218
+ walkNested(tree, result)
211
219
  return result
212
220
  }
213
221
 
@@ -217,3 +225,22 @@ export function getBarrelFiles({ outputPath, files, barrelType, recursive = fals
217
225
  walkAllOrNamed(tree, { sourceFiles, strategy, recursive }, true, result)
218
226
  return result
219
227
  }
228
+
229
+ /**
230
+ * Builds a POSIX-normalized prefix for a plugin's output directory, with a trailing `/`.
231
+ *
232
+ * Used to detect (and later exclude) files generated by plugins that opted out of the root barrel.
233
+ */
234
+ export function getPluginOutputPrefix(plugin: NormalizedPlugin, config: Config): string {
235
+ return `${toPosixPath(resolve(config.root, config.output.path, plugin.options.output.path))}/`
236
+ }
237
+
238
+ /**
239
+ * Returns `true` when `filePath` lives under any of the given excluded directory prefixes.
240
+ *
241
+ * Both sides are POSIX-normalized so Windows backslash paths match correctly.
242
+ */
243
+ export function isExcludedPath(filePath: string, prefixes: ReadonlySet<string>): boolean {
244
+ const normalized = toPosixPath(filePath)
245
+ return prefixes.values().some((prefix) => normalized.startsWith(prefix))
246
+ }
package/src/constants.ts DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * Full file name for barrel files (with extension).
3
- */
4
- export const BARREL_FILENAME = 'index.ts' as const
@@ -1,22 +0,0 @@
1
- import { resolve } from 'node:path'
2
- import type { Config, NormalizedPlugin } from '@kubb/core'
3
- import { toPosixPath } from '@internals/utils'
4
-
5
- /**
6
- * Builds a POSIX-normalized prefix for a plugin's output directory, with a trailing `/`.
7
- *
8
- * Used to detect (and later exclude) files generated by plugins that opted out of the root barrel.
9
- */
10
- export function getPluginOutputPrefix(plugin: NormalizedPlugin, config: Config): string {
11
- return `${toPosixPath(resolve(config.root, config.output.path, plugin.options.output.path))}/`
12
- }
13
-
14
- /**
15
- * Returns `true` when `filePath` lives under any of the given excluded directory prefixes.
16
- *
17
- * Both sides are POSIX-normalized so Windows backslash paths match correctly.
18
- */
19
- export function isExcludedPath(filePath: string, prefixes: ReadonlySet<string>): boolean {
20
- const normalized = toPosixPath(filePath)
21
- return prefixes.values().some((prefix) => normalized.startsWith(prefix))
22
- }