@kubb/core 5.0.0-alpha.42 → 5.0.0-alpha.44

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.
@@ -1,7 +1,6 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
2
- import path, { basename, extname, resolve } from "node:path";
1
+ import "./chunk--u3MIqq1.js";
2
+ import path, { extname, resolve } from "node:path";
3
3
  import { createFile, isOperationNode, isSchemaNode } from "@kubb/ast";
4
- import { performance } from "node:perf_hooks";
5
4
  import { deflateSync } from "fflate";
6
5
  import { x } from "tinyexec";
7
6
  //#region ../../internals/utils/src/casing.ts
@@ -62,141 +61,6 @@ function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
62
61
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
63
62
  }
64
63
  //#endregion
65
- //#region ../../internals/utils/src/promise.ts
66
- /** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.
67
- *
68
- * @example
69
- * ```ts
70
- * const results = await Promise.allSettled([p1, p2])
71
- * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)
72
- * ```
73
- */
74
- function isPromiseRejectedResult(result) {
75
- return result.status === "rejected";
76
- }
77
- //#endregion
78
- //#region ../../internals/utils/src/reserved.ts
79
- /**
80
- * JavaScript and Java reserved words.
81
- * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
82
- */
83
- const reservedWords = new Set([
84
- "abstract",
85
- "arguments",
86
- "boolean",
87
- "break",
88
- "byte",
89
- "case",
90
- "catch",
91
- "char",
92
- "class",
93
- "const",
94
- "continue",
95
- "debugger",
96
- "default",
97
- "delete",
98
- "do",
99
- "double",
100
- "else",
101
- "enum",
102
- "eval",
103
- "export",
104
- "extends",
105
- "false",
106
- "final",
107
- "finally",
108
- "float",
109
- "for",
110
- "function",
111
- "goto",
112
- "if",
113
- "implements",
114
- "import",
115
- "in",
116
- "instanceof",
117
- "int",
118
- "interface",
119
- "let",
120
- "long",
121
- "native",
122
- "new",
123
- "null",
124
- "package",
125
- "private",
126
- "protected",
127
- "public",
128
- "return",
129
- "short",
130
- "static",
131
- "super",
132
- "switch",
133
- "synchronized",
134
- "this",
135
- "throw",
136
- "throws",
137
- "transient",
138
- "true",
139
- "try",
140
- "typeof",
141
- "var",
142
- "void",
143
- "volatile",
144
- "while",
145
- "with",
146
- "yield",
147
- "Array",
148
- "Date",
149
- "hasOwnProperty",
150
- "Infinity",
151
- "isFinite",
152
- "isNaN",
153
- "isPrototypeOf",
154
- "length",
155
- "Math",
156
- "name",
157
- "NaN",
158
- "Number",
159
- "Object",
160
- "prototype",
161
- "String",
162
- "toString",
163
- "undefined",
164
- "valueOf"
165
- ]);
166
- /**
167
- * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.
168
- *
169
- * @example
170
- * ```ts
171
- * transformReservedWord('class') // '_class'
172
- * transformReservedWord('42foo') // '_42foo'
173
- * transformReservedWord('status') // 'status'
174
- * ```
175
- */
176
- function transformReservedWord(word) {
177
- const firstChar = word.charCodeAt(0);
178
- if (word && (reservedWords.has(word) || firstChar >= 48 && firstChar <= 57)) return `_${word}`;
179
- return word;
180
- }
181
- /**
182
- * Returns `true` when `name` is a syntactically valid JavaScript variable name.
183
- *
184
- * @example
185
- * ```ts
186
- * isValidVarName('status') // true
187
- * isValidVarName('class') // false (reserved word)
188
- * isValidVarName('42foo') // false (starts with digit)
189
- * ```
190
- */
191
- function isValidVarName(name) {
192
- try {
193
- new Function(`var ${name}`);
194
- } catch {
195
- return false;
196
- }
197
- return true;
198
- }
199
- //#endregion
200
64
  //#region ../../internals/utils/src/string.ts
201
65
  /**
202
66
  * Strips the file extension from a path or file name.
@@ -220,9 +84,16 @@ function trimExtName(text) {
220
84
  */
221
85
  const DEFAULT_STUDIO_URL = "https://studio.kubb.dev";
222
86
  /**
87
+ * Basename (without extension) of generated barrel files.
88
+ *
89
+ * Used to detect whether a path already points at a barrel so the generator
90
+ * avoids re-creating one on top of it.
91
+ */
92
+ const BARREL_BASENAME = "index";
93
+ /**
223
94
  * File name used for generated barrel (index) files.
224
95
  */
225
- const BARREL_FILENAME = "index.ts";
96
+ const BARREL_FILENAME = `${BARREL_BASENAME}.ts`;
226
97
  /**
227
98
  * Default banner style written at the top of every generated file.
228
99
  */
@@ -245,184 +116,29 @@ const logLevel = {
245
116
  debug: 5
246
117
  };
247
118
  //#endregion
248
- //#region ../../node_modules/.pnpm/yocto-queue@1.2.2/node_modules/yocto-queue/index.js
249
- var Node$1 = class {
250
- static {
251
- __name(this, "Node");
252
- }
253
- value;
254
- next;
255
- constructor(value) {
256
- this.value = value;
257
- }
258
- };
259
- var Queue = class {
260
- #head;
261
- #tail;
262
- #size;
263
- constructor() {
264
- this.clear();
265
- }
266
- enqueue(value) {
267
- const node = new Node$1(value);
268
- if (this.#head) {
269
- this.#tail.next = node;
270
- this.#tail = node;
271
- } else {
272
- this.#head = node;
273
- this.#tail = node;
274
- }
275
- this.#size++;
276
- }
277
- dequeue() {
278
- const current = this.#head;
279
- if (!current) return;
280
- this.#head = this.#head.next;
281
- this.#size--;
282
- if (!this.#head) this.#tail = void 0;
283
- return current.value;
284
- }
285
- peek() {
286
- if (!this.#head) return;
287
- return this.#head.value;
288
- }
289
- clear() {
290
- this.#head = void 0;
291
- this.#tail = void 0;
292
- this.#size = 0;
293
- }
294
- get size() {
295
- return this.#size;
296
- }
297
- *[Symbol.iterator]() {
298
- let current = this.#head;
299
- while (current) {
300
- yield current.value;
301
- current = current.next;
119
+ //#region src/defineResolver.ts
120
+ const stringPatternCache = /* @__PURE__ */ new Map();
121
+ function testPattern(value, pattern) {
122
+ if (typeof pattern === "string") {
123
+ let regex = stringPatternCache.get(pattern);
124
+ if (!regex) {
125
+ regex = new RegExp(pattern);
126
+ stringPatternCache.set(pattern, regex);
302
127
  }
128
+ return regex.test(value);
303
129
  }
304
- *drain() {
305
- while (this.#head) yield this.dequeue();
306
- }
307
- };
308
- //#endregion
309
- //#region ../../node_modules/.pnpm/p-limit@7.3.0/node_modules/p-limit/index.js
310
- function pLimit(concurrency) {
311
- let rejectOnClear = false;
312
- if (typeof concurrency === "object") ({concurrency, rejectOnClear = false} = concurrency);
313
- validateConcurrency(concurrency);
314
- if (typeof rejectOnClear !== "boolean") throw new TypeError("Expected `rejectOnClear` to be a boolean");
315
- const queue = new Queue();
316
- let activeCount = 0;
317
- const resumeNext = () => {
318
- if (activeCount < concurrency && queue.size > 0) {
319
- activeCount++;
320
- queue.dequeue().run();
321
- }
322
- };
323
- const next = () => {
324
- activeCount--;
325
- resumeNext();
326
- };
327
- const run = async (function_, resolve, arguments_) => {
328
- const result = (async () => function_(...arguments_))();
329
- resolve(result);
330
- try {
331
- await result;
332
- } catch {}
333
- next();
334
- };
335
- const enqueue = (function_, resolve, reject, arguments_) => {
336
- const queueItem = { reject };
337
- new Promise((internalResolve) => {
338
- queueItem.run = internalResolve;
339
- queue.enqueue(queueItem);
340
- }).then(run.bind(void 0, function_, resolve, arguments_));
341
- if (activeCount < concurrency) resumeNext();
342
- };
343
- const generator = (function_, ...arguments_) => new Promise((resolve, reject) => {
344
- enqueue(function_, resolve, reject, arguments_);
345
- });
346
- Object.defineProperties(generator, {
347
- activeCount: { get: () => activeCount },
348
- pendingCount: { get: () => queue.size },
349
- clearQueue: { value() {
350
- if (!rejectOnClear) {
351
- queue.clear();
352
- return;
353
- }
354
- const abortError = AbortSignal.abort().reason;
355
- while (queue.size > 0) queue.dequeue().reject(abortError);
356
- } },
357
- concurrency: {
358
- get: () => concurrency,
359
- set(newConcurrency) {
360
- validateConcurrency(newConcurrency);
361
- concurrency = newConcurrency;
362
- queueMicrotask(() => {
363
- while (activeCount < concurrency && queue.size > 0) resumeNext();
364
- });
365
- }
366
- },
367
- map: { async value(iterable, function_) {
368
- const promises = Array.from(iterable, (value, index) => this(function_, value, index));
369
- return Promise.all(promises);
370
- } }
371
- });
372
- return generator;
373
- }
374
- function validateConcurrency(concurrency) {
375
- if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) throw new TypeError("Expected `concurrency` to be a number from 1 and up");
376
- }
377
- //#endregion
378
- //#region src/definePlugin.ts
379
- /**
380
- * Returns `true` when `plugin` is a hook-style plugin created with `definePlugin`.
381
- *
382
- * Used by `PluginDriver` to distinguish hook-style plugins from legacy `createPlugin` plugins
383
- * so it can normalize them and register their handlers on the `AsyncEventEmitter`.
384
- */
385
- function isHookStylePlugin(plugin) {
386
- return typeof plugin === "object" && plugin !== null && "hooks" in plugin;
130
+ return value.match(pattern) !== null;
387
131
  }
388
132
  /**
389
- * Creates a plugin factory using the new hook-style (`hooks:`) API.
390
- *
391
- * The returned factory is called with optional options and produces a `HookStylePlugin`
392
- * that coexists with plugins created via the legacy `createPlugin` API in the same
393
- * `kubb.config.ts`.
394
- *
395
- * Lifecycle handlers are registered on the `PluginDriver`'s `AsyncEventEmitter`, enabling
396
- * both the plugin's own handlers and external tooling (CLI, devtools) to observe every event.
397
- *
398
- * @example
399
- * ```ts
400
- * // With PluginFactoryOptions (recommended for real plugins)
401
- * export const pluginTs = definePlugin<PluginTs>((options) => ({
402
- * name: 'plugin-ts',
403
- * hooks: {
404
- * 'kubb:plugin:setup'(ctx) {
405
- * ctx.setResolver(resolverTs) // typed as Partial<ResolverTs>
406
- * },
407
- * },
408
- * }))
409
- * ```
410
- */
411
- function definePlugin(factory) {
412
- return (options) => factory(options ?? {});
413
- }
414
- //#endregion
415
- //#region src/defineResolver.ts
416
- /**
417
133
  * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
418
134
  */
419
135
  function matchesOperationPattern(node, type, pattern) {
420
136
  switch (type) {
421
- case "tag": return node.tags.some((tag) => !!tag.match(pattern));
422
- case "operationId": return !!node.operationId.match(pattern);
423
- case "path": return !!node.path.match(pattern);
424
- case "method": return !!node.method.toLowerCase().match(pattern);
425
- case "contentType": return !!node.requestBody?.contentType?.match(pattern);
137
+ case "tag": return node.tags.some((tag) => testPattern(tag, pattern));
138
+ case "operationId": return testPattern(node.operationId, pattern);
139
+ case "path": return testPattern(node.path, pattern);
140
+ case "method": return testPattern(node.method.toLowerCase(), pattern);
141
+ case "contentType": return node.requestBody?.contentType ? testPattern(node.requestBody.contentType, pattern) : false;
426
142
  default: return false;
427
143
  }
428
144
  }
@@ -433,7 +149,7 @@ function matchesOperationPattern(node, type, pattern) {
433
149
  */
434
150
  function matchesSchemaPattern(node, type, pattern) {
435
151
  switch (type) {
436
- case "schemaName": return node.name ? !!node.name.match(pattern) : false;
152
+ case "schemaName": return node.name ? testPattern(node.name, pattern) : false;
437
153
  default: return null;
438
154
  }
439
155
  }
@@ -827,17 +543,16 @@ var FileManager = class {
827
543
  add(...files) {
828
544
  const resolvedFiles = [];
829
545
  const mergedFiles = /* @__PURE__ */ new Map();
830
- files.forEach((file) => {
546
+ for (const file of files) {
831
547
  const existing = mergedFiles.get(file.path);
832
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
833
- else mergedFiles.set(file.path, file);
834
- });
548
+ mergedFiles.set(file.path, existing ? mergeFile(existing, file) : file);
549
+ }
835
550
  for (const file of mergedFiles.values()) {
836
551
  const resolvedFile = createFile(file);
837
552
  this.#cache.set(resolvedFile.path, resolvedFile);
838
- this.#filesCache = null;
839
553
  resolvedFiles.push(resolvedFile);
840
554
  }
555
+ this.#filesCache = null;
841
556
  return resolvedFiles;
842
557
  }
843
558
  /**
@@ -847,18 +562,17 @@ var FileManager = class {
847
562
  upsert(...files) {
848
563
  const resolvedFiles = [];
849
564
  const mergedFiles = /* @__PURE__ */ new Map();
850
- files.forEach((file) => {
565
+ for (const file of files) {
851
566
  const existing = mergedFiles.get(file.path);
852
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
853
- else mergedFiles.set(file.path, file);
854
- });
567
+ mergedFiles.set(file.path, existing ? mergeFile(existing, file) : file);
568
+ }
855
569
  for (const file of mergedFiles.values()) {
856
570
  const existing = this.#cache.get(file.path);
857
571
  const resolvedFile = createFile(existing ? mergeFile(existing, file) : file);
858
572
  this.#cache.set(resolvedFile.path, resolvedFile);
859
- this.#filesCache = null;
860
573
  resolvedFiles.push(resolvedFile);
861
574
  }
575
+ this.#filesCache = null;
862
576
  return resolvedFiles;
863
577
  }
864
578
  getByPath(path) {
@@ -878,10 +592,17 @@ var FileManager = class {
878
592
  */
879
593
  get files() {
880
594
  if (this.#filesCache) return this.#filesCache;
881
- const keys = [...this.#cache.keys()].sort((a, b) => {
882
- if (a.length !== b.length) return a.length - b.length;
883
- const aIsIndex = trimExtName(a).endsWith("index");
884
- if (aIsIndex !== trimExtName(b).endsWith("index")) return aIsIndex ? 1 : -1;
595
+ const keys = [...this.#cache.keys()];
596
+ const meta = /* @__PURE__ */ new Map();
597
+ for (const key of keys) meta.set(key, {
598
+ length: key.length,
599
+ isIndex: trimExtName(key).endsWith(BARREL_BASENAME)
600
+ });
601
+ keys.sort((a, b) => {
602
+ const ma = meta.get(a);
603
+ const mb = meta.get(b);
604
+ if (ma.length !== mb.length) return ma.length - mb.length;
605
+ if (ma.isIndex !== mb.isIndex) return ma.isIndex ? 1 : -1;
885
606
  return 0;
886
607
  });
887
608
  const files = [];
@@ -918,55 +639,7 @@ async function applyHookResult(result, driver, rendererFactory) {
918
639
  renderer.unmount();
919
640
  }
920
641
  //#endregion
921
- //#region src/utils/executeStrategies.ts
922
- /**
923
- * Runs promise functions in sequence, threading each result into the next call.
924
- *
925
- * - Each function receives the accumulated state from the previous call.
926
- * - Skips functions that return a falsy value (acts as a no-op for that step).
927
- * - Returns an array of all individual results.
928
- * @deprecated
929
- */
930
- function hookSeq(promises) {
931
- return promises.filter(Boolean).reduce((promise, func) => {
932
- if (typeof func !== "function") throw new Error("HookSeq needs a function that returns a promise `() => Promise<unknown>`");
933
- return promise.then((state) => {
934
- const calledFunc = func(state);
935
- if (calledFunc) return calledFunc.then(Array.prototype.concat.bind(state));
936
- return state;
937
- });
938
- }, Promise.resolve([]));
939
- }
940
- /**
941
- * Runs promise functions in sequence and returns the first non-null result.
942
- *
943
- * - Stops as soon as `nullCheck` passes for a result (default: `!== null`).
944
- * - Subsequent functions are skipped once a match is found.
945
- * @deprecated
946
- */
947
- function hookFirst(promises, nullCheck = (state) => state !== null) {
948
- let promise = Promise.resolve(null);
949
- for (const func of promises.filter(Boolean)) promise = promise.then((state) => {
950
- if (nullCheck(state)) return state;
951
- return func(state);
952
- });
953
- return promise;
954
- }
955
- /**
956
- * Runs promise functions concurrently and returns all settled results.
957
- *
958
- * - Limits simultaneous executions to `concurrency` (default: unlimited).
959
- * - Uses `Promise.allSettled` so individual failures do not cancel other tasks.
960
- * @deprecated
961
- */
962
- function hookParallel(promises, concurrency = Number.POSITIVE_INFINITY) {
963
- const limit = pLimit(concurrency);
964
- const tasks = promises.filter(Boolean).map((promise) => limit(() => promise()));
965
- return Promise.allSettled(tasks);
966
- }
967
- //#endregion
968
642
  //#region src/PluginDriver.ts
969
- const hookFirstNullCheck = (state) => !!state?.result;
970
643
  var PluginDriver = class PluginDriver {
971
644
  config;
972
645
  options;
@@ -1007,18 +680,8 @@ var PluginDriver = class PluginDriver {
1007
680
  #hookListeners = /* @__PURE__ */ new Map();
1008
681
  constructor(config, options) {
1009
682
  this.config = config;
1010
- this.options = {
1011
- ...options,
1012
- hooks: options.hooks
1013
- };
1014
- config.plugins.map((rawPlugin) => {
1015
- if (isHookStylePlugin(rawPlugin)) return this.#normalizeHookStylePlugin(rawPlugin);
1016
- return {
1017
- ...rawPlugin,
1018
- buildStart: rawPlugin.buildStart ?? (() => {}),
1019
- buildEnd: rawPlugin.buildEnd ?? (() => {})
1020
- };
1021
- }).filter((plugin) => {
683
+ this.options = options;
684
+ config.plugins.map((rawPlugin) => this.#normalizePlugin(rawPlugin)).filter((plugin) => {
1022
685
  if (typeof plugin.apply === "function") return plugin.apply(config);
1023
686
  return true;
1024
687
  }).sort((a, b) => {
@@ -1030,20 +693,13 @@ var PluginDriver = class PluginDriver {
1030
693
  });
1031
694
  }
1032
695
  get hooks() {
1033
- if (!this.options.hooks) throw new Error("hooks are not defined");
1034
696
  return this.options.hooks;
1035
697
  }
1036
698
  /**
1037
- * Creates a `Plugin`-compatible object from a hook-style plugin and registers
699
+ * Creates an `NormalizedPlugin` from a hook-style plugin and registers
1038
700
  * its lifecycle handlers on the `AsyncEventEmitter`.
1039
- *
1040
- * The normalized plugin has an empty `buildStart` — generators registered via
1041
- * `addGenerator()` in `kubb:plugin:setup` are stored on `normalizedPlugin.generators`
1042
- * and used by `runPluginAstHooks` during the build.
1043
701
  */
1044
- #normalizeHookStylePlugin(hookPlugin) {
1045
- const generators = [];
1046
- const driver = this;
702
+ #normalizePlugin(hookPlugin) {
1047
703
  const normalizedPlugin = {
1048
704
  name: hookPlugin.name,
1049
705
  dependencies: hookPlugin.dependencies,
@@ -1051,29 +707,7 @@ var PluginDriver = class PluginDriver {
1051
707
  output: { path: "." },
1052
708
  exclude: [],
1053
709
  override: []
1054
- },
1055
- generators,
1056
- inject: () => void 0,
1057
- resolveName(name, type) {
1058
- return driver.getResolver(hookPlugin.name).default(name, type);
1059
- },
1060
- resolvePath(baseName, pathMode, resolveOptions) {
1061
- const resolver = driver.getResolver(hookPlugin.name);
1062
- const opts = normalizedPlugin.options;
1063
- const group = resolveOptions?.group;
1064
- return resolver.resolvePath({
1065
- baseName,
1066
- pathMode,
1067
- tag: group?.tag,
1068
- path: group?.path
1069
- }, {
1070
- root: resolve(driver.config.root, driver.config.output.path),
1071
- output: opts.output,
1072
- group: opts.group
1073
- });
1074
- },
1075
- buildStart() {},
1076
- buildEnd() {}
710
+ }
1077
711
  };
1078
712
  this.registerPluginHooks(hookPlugin, normalizedPlugin);
1079
713
  return normalizedPlugin;
@@ -1090,6 +724,8 @@ var PluginDriver = class PluginDriver {
1090
724
  *
1091
725
  * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
1092
726
  * the plugin lifecycle without modifying plugin behavior.
727
+ *
728
+ * @internal
1093
729
  */
1094
730
  registerPluginHooks(hookPlugin, normalizedPlugin) {
1095
731
  const { hooks } = hookPlugin;
@@ -1116,15 +752,13 @@ var PluginDriver = class PluginDriver {
1116
752
  ...opts
1117
753
  };
1118
754
  },
1119
- injectFile: (file) => {
1120
- const fileNode = createFile({
1121
- baseName: file.baseName,
1122
- path: file.path,
1123
- sources: file.sources ?? [],
755
+ injectFile: ({ sources = [], ...rest }) => {
756
+ this.fileManager.add(createFile({
1124
757
  imports: [],
1125
- exports: []
1126
- });
1127
- this.fileManager.add(fileNode);
758
+ exports: [],
759
+ sources,
760
+ ...rest
761
+ }));
1128
762
  }
1129
763
  };
1130
764
  return hooks["kubb:plugin:setup"](pluginCtx);
@@ -1145,16 +779,17 @@ var PluginDriver = class PluginDriver {
1145
779
  * Call this once from `safeBuild` before the plugin execution loop begins.
1146
780
  */
1147
781
  async emitSetupHooks() {
782
+ const noop = () => {};
1148
783
  await this.hooks.emit("kubb:plugin:setup", {
1149
784
  config: this.config,
1150
- addGenerator: () => {},
1151
- setResolver: () => {},
1152
- setTransformer: () => {},
1153
- setRenderer: () => {},
1154
- setOptions: () => {},
1155
- injectFile: () => {},
1156
- updateConfig: () => {},
1157
- options: {}
785
+ options: {},
786
+ addGenerator: noop,
787
+ setResolver: noop,
788
+ setTransformer: noop,
789
+ setRenderer: noop,
790
+ setOptions: noop,
791
+ injectFile: noop,
792
+ updateConfig: noop
1158
793
  });
1159
794
  }
1160
795
  /**
@@ -1212,6 +847,12 @@ var PluginDriver = class PluginDriver {
1212
847
  hasRegisteredGenerators(pluginName) {
1213
848
  return this.#pluginsWithEventGenerators.has(pluginName);
1214
849
  }
850
+ /**
851
+ * Unregisters all plugin lifecycle listeners from the shared event emitter.
852
+ * Called at the end of a build to prevent listener leaks across repeated builds.
853
+ *
854
+ * @internal
855
+ */
1215
856
  dispose() {
1216
857
  for (const [event, handlers] of this.#hookListeners) for (const handler of handlers) this.hooks.off(event, handler);
1217
858
  this.#hookListeners.clear();
@@ -1235,6 +876,11 @@ var PluginDriver = class PluginDriver {
1235
876
  this.#defaultResolvers.set(pluginName, resolver);
1236
877
  return resolver;
1237
878
  }
879
+ /**
880
+ * Merges `partial` with the plugin's default resolver and stores the result.
881
+ * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
882
+ * get the up-to-date resolver without going through `getResolver()`.
883
+ */
1238
884
  setPluginResolver(pluginName, partial) {
1239
885
  const merged = {
1240
886
  ...this.#createDefaultResolver(pluginName),
@@ -1244,16 +890,18 @@ var PluginDriver = class PluginDriver {
1244
890
  const plugin = this.plugins.get(pluginName);
1245
891
  if (plugin) plugin.resolver = merged;
1246
892
  }
893
+ /**
894
+ * Returns the resolver for the given plugin.
895
+ *
896
+ * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
897
+ * plugin → lazily created default resolver (identity name, no path transforms).
898
+ */
1247
899
  getResolver(pluginName) {
1248
- const dynamicResolver = this.#resolvers.get(pluginName);
1249
- if (dynamicResolver) return dynamicResolver;
1250
- const pluginResolver = this.plugins.get(pluginName)?.resolver;
1251
- if (pluginResolver) return pluginResolver;
1252
- return this.#createDefaultResolver(pluginName);
900
+ return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#createDefaultResolver(pluginName);
1253
901
  }
1254
902
  getContext(plugin) {
1255
903
  const driver = this;
1256
- const baseContext = {
904
+ return {
1257
905
  config: driver.config,
1258
906
  get root() {
1259
907
  return resolve(driver.config.root, driver.config.output.path);
@@ -1302,225 +950,6 @@ var PluginDriver = class PluginDriver {
1302
950
  return openInStudio(driver.inputNode, studioUrl, options);
1303
951
  }
1304
952
  };
1305
- let mergedExtras = {};
1306
- for (const p of this.plugins.values()) if (typeof p.inject === "function") {
1307
- const result = p.inject.call(baseContext);
1308
- if (result !== null && typeof result === "object") mergedExtras = {
1309
- ...mergedExtras,
1310
- ...result
1311
- };
1312
- }
1313
- return {
1314
- ...baseContext,
1315
- ...mergedExtras
1316
- };
1317
- }
1318
- /**
1319
- * @deprecated use resolvers context instead
1320
- */
1321
- getFile({ name, mode, extname, pluginName, options }) {
1322
- const resolvedName = mode ? mode === "single" ? "" : this.resolveName({
1323
- name,
1324
- pluginName,
1325
- type: "file"
1326
- }) : name;
1327
- const path = this.resolvePath({
1328
- baseName: `${resolvedName}${extname}`,
1329
- mode,
1330
- pluginName,
1331
- options
1332
- });
1333
- if (!path) throw new Error(`Filepath should be defined for resolvedName "${resolvedName}" and pluginName "${pluginName}"`);
1334
- return createFile({
1335
- path,
1336
- baseName: basename(path),
1337
- meta: { pluginName },
1338
- sources: [],
1339
- imports: [],
1340
- exports: []
1341
- });
1342
- }
1343
- /**
1344
- * @deprecated use resolvers context instead
1345
- */
1346
- resolvePath = (params) => {
1347
- const defaultPath = resolve(resolve(this.config.root, this.config.output.path), params.baseName);
1348
- if (params.pluginName) return this.hookForPluginSync({
1349
- pluginName: params.pluginName,
1350
- hookName: "resolvePath",
1351
- parameters: [
1352
- params.baseName,
1353
- params.mode,
1354
- params.options
1355
- ]
1356
- })?.at(0) || defaultPath;
1357
- return this.hookFirstSync({
1358
- hookName: "resolvePath",
1359
- parameters: [
1360
- params.baseName,
1361
- params.mode,
1362
- params.options
1363
- ]
1364
- })?.result || defaultPath;
1365
- };
1366
- /**
1367
- * @deprecated use resolvers context instead
1368
- */
1369
- resolveName = (params) => {
1370
- if (params.pluginName) return transformReservedWord(this.hookForPluginSync({
1371
- pluginName: params.pluginName,
1372
- hookName: "resolveName",
1373
- parameters: [params.name.trim(), params.type]
1374
- })?.at(0) ?? params.name);
1375
- const name = this.hookFirstSync({
1376
- hookName: "resolveName",
1377
- parameters: [params.name.trim(), params.type]
1378
- })?.result;
1379
- return transformReservedWord(name ?? params.name);
1380
- };
1381
- /**
1382
- * Run a specific hookName for plugin x.
1383
- */
1384
- async hookForPlugin({ pluginName, hookName, parameters }) {
1385
- const plugin = this.plugins.get(pluginName);
1386
- if (!plugin) return [null];
1387
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1388
- hookName,
1389
- plugins: [plugin]
1390
- });
1391
- const result = await this.#execute({
1392
- strategy: "hookFirst",
1393
- hookName,
1394
- parameters,
1395
- plugin
1396
- });
1397
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
1398
- return [result];
1399
- }
1400
- /**
1401
- * Run a specific hookName for plugin x.
1402
- */
1403
- hookForPluginSync({ pluginName, hookName, parameters }) {
1404
- const plugin = this.plugins.get(pluginName);
1405
- if (!plugin) return null;
1406
- const result = this.#executeSync({
1407
- strategy: "hookFirst",
1408
- hookName,
1409
- parameters,
1410
- plugin
1411
- });
1412
- return result !== null ? [result] : [];
1413
- }
1414
- /**
1415
- * Returns the first non-null result.
1416
- */
1417
- async hookFirst({ hookName, parameters, skipped }) {
1418
- const plugins = [];
1419
- for (const plugin of this.plugins.values()) if (hookName in plugin && (skipped ? !skipped.has(plugin) : true)) plugins.push(plugin);
1420
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1421
- hookName,
1422
- plugins
1423
- });
1424
- const result = await hookFirst(plugins.map((plugin) => {
1425
- return async () => {
1426
- const value = await this.#execute({
1427
- strategy: "hookFirst",
1428
- hookName,
1429
- parameters,
1430
- plugin
1431
- });
1432
- return Promise.resolve({
1433
- plugin,
1434
- result: value
1435
- });
1436
- };
1437
- }), hookFirstNullCheck);
1438
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
1439
- return result;
1440
- }
1441
- /**
1442
- * Returns the first non-null result.
1443
- */
1444
- hookFirstSync({ hookName, parameters, skipped }) {
1445
- let parseResult = null;
1446
- for (const plugin of this.plugins.values()) {
1447
- if (!(hookName in plugin)) continue;
1448
- if (skipped?.has(plugin)) continue;
1449
- parseResult = {
1450
- result: this.#executeSync({
1451
- strategy: "hookFirst",
1452
- hookName,
1453
- parameters,
1454
- plugin
1455
- }),
1456
- plugin
1457
- };
1458
- if (parseResult.result != null) break;
1459
- }
1460
- return parseResult;
1461
- }
1462
- /**
1463
- * Runs all plugins in parallel based on `this.plugin` order and `dependencies` settings.
1464
- */
1465
- async hookParallel({ hookName, parameters }) {
1466
- const plugins = [];
1467
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
1468
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1469
- hookName,
1470
- plugins
1471
- });
1472
- const pluginStartTimes = /* @__PURE__ */ new Map();
1473
- const results = await hookParallel(plugins.map((plugin) => {
1474
- return () => {
1475
- pluginStartTimes.set(plugin, performance.now());
1476
- return this.#execute({
1477
- strategy: "hookParallel",
1478
- hookName,
1479
- parameters,
1480
- plugin
1481
- });
1482
- };
1483
- }), this.options.concurrency);
1484
- results.forEach((result, index) => {
1485
- if (isPromiseRejectedResult(result)) {
1486
- const plugin = plugins[index];
1487
- if (plugin) {
1488
- const startTime = pluginStartTimes.get(plugin) ?? performance.now();
1489
- this.hooks.emit("kubb:error", result.reason, {
1490
- plugin,
1491
- hookName,
1492
- strategy: "hookParallel",
1493
- duration: Math.round(performance.now() - startTime),
1494
- parameters
1495
- });
1496
- }
1497
- }
1498
- });
1499
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
1500
- return results.reduce((acc, result) => {
1501
- if (result.status === "fulfilled") acc.push(result.value);
1502
- return acc;
1503
- }, []);
1504
- }
1505
- /**
1506
- * Execute a lifecycle hook sequentially for all plugins that implement it.
1507
- */
1508
- async hookSeq({ hookName, parameters }) {
1509
- const plugins = [];
1510
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
1511
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1512
- hookName,
1513
- plugins
1514
- });
1515
- await hookSeq(plugins.map((plugin) => {
1516
- return () => this.#execute({
1517
- strategy: "hookSeq",
1518
- hookName,
1519
- parameters,
1520
- plugin
1521
- });
1522
- }));
1523
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
1524
953
  }
1525
954
  getPlugin(pluginName) {
1526
955
  return this.plugins.get(pluginName);
@@ -1530,88 +959,8 @@ var PluginDriver = class PluginDriver {
1530
959
  if (!plugin) throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`);
1531
960
  return plugin;
1532
961
  }
1533
- /**
1534
- * Emit hook-processing completion metadata after a plugin hook resolves.
1535
- */
1536
- #emitProcessingEnd({ startTime, output, strategy, hookName, plugin, parameters }) {
1537
- this.hooks.emit("kubb:plugins:hook:processing:end", {
1538
- duration: Math.round(performance.now() - startTime),
1539
- parameters,
1540
- output,
1541
- strategy,
1542
- hookName,
1543
- plugin
1544
- });
1545
- }
1546
- #execute({ strategy, hookName, parameters, plugin }) {
1547
- const hook = plugin[hookName];
1548
- if (!hook) return null;
1549
- this.hooks.emit("kubb:plugins:hook:processing:start", {
1550
- strategy,
1551
- hookName,
1552
- parameters,
1553
- plugin
1554
- });
1555
- const startTime = performance.now();
1556
- return (async () => {
1557
- try {
1558
- const output = typeof hook === "function" ? await Promise.resolve(hook.apply(this.getContext(plugin), parameters ?? [])) : hook;
1559
- this.#emitProcessingEnd({
1560
- startTime,
1561
- output,
1562
- strategy,
1563
- hookName,
1564
- plugin,
1565
- parameters
1566
- });
1567
- return output;
1568
- } catch (error) {
1569
- this.hooks.emit("kubb:error", error, {
1570
- plugin,
1571
- hookName,
1572
- strategy,
1573
- duration: Math.round(performance.now() - startTime)
1574
- });
1575
- return null;
1576
- }
1577
- })();
1578
- }
1579
- /**
1580
- * Execute a plugin lifecycle hook synchronously and return its output.
1581
- */
1582
- #executeSync({ strategy, hookName, parameters, plugin }) {
1583
- const hook = plugin[hookName];
1584
- if (!hook) return null;
1585
- this.hooks.emit("kubb:plugins:hook:processing:start", {
1586
- strategy,
1587
- hookName,
1588
- parameters,
1589
- plugin
1590
- });
1591
- const startTime = performance.now();
1592
- try {
1593
- const output = typeof hook === "function" ? hook.apply(this.getContext(plugin), parameters) : hook;
1594
- this.#emitProcessingEnd({
1595
- startTime,
1596
- output,
1597
- strategy,
1598
- hookName,
1599
- plugin,
1600
- parameters
1601
- });
1602
- return output;
1603
- } catch (error) {
1604
- this.hooks.emit("kubb:error", error, {
1605
- plugin,
1606
- hookName,
1607
- strategy,
1608
- duration: Math.round(performance.now() - startTime)
1609
- });
1610
- return null;
1611
- }
1612
- }
1613
962
  };
1614
963
  //#endregion
1615
- export { definePlugin as a, DEFAULT_BANNER as c, logLevel as d, isValidVarName as f, defineResolver as i, DEFAULT_EXTENSION as l, applyHookResult as n, pLimit as o, camelCase as p, FileManager as r, BARREL_FILENAME as s, PluginDriver as t, DEFAULT_STUDIO_URL as u };
964
+ export { BARREL_BASENAME as a, DEFAULT_EXTENSION as c, camelCase as d, defineResolver as i, DEFAULT_STUDIO_URL as l, applyHookResult as n, BARREL_FILENAME as o, FileManager as r, DEFAULT_BANNER as s, PluginDriver as t, logLevel as u };
1616
965
 
1617
- //# sourceMappingURL=PluginDriver-CgXFtmNP.js.map
966
+ //# sourceMappingURL=PluginDriver-Bt_UCn7-.js.map