@kubb/core 5.8.0 → 5.8.1

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,11 +1,14 @@
1
- import "./rolldown-runtime-C0LytTxp.js";
1
+ import "./rolldown-runtime-CRm0XQPb.js";
2
+ import { satisfies } from "verkit";
2
3
  import { AsyncLocalStorage } from "node:async_hooks";
3
4
  import { styleText } from "node:util";
4
5
  import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
5
6
  import path, { dirname, isAbsolute, relative, resolve } from "node:path";
6
7
  import { ast, collectImportedRefNames, collectUsedSchemaNames, composeMacros, extractStringsFromNodes, operationDef, schemaDef, transform } from "@kubb/ast";
7
- import { satisfies } from "verkit";
8
8
  import { EventEmitter } from "node:events";
9
+ //#region package.json
10
+ var version = "5.8.1";
11
+ //#endregion
9
12
  //#region ../../internals/utils/src/casing.ts
10
13
  /**
11
14
  * Shared implementation for camelCase and PascalCase conversion.
@@ -329,9 +332,6 @@ function formatMs(ms) {
329
332
  return `${Math.round(ms)}ms`;
330
333
  }
331
334
  //#endregion
332
- //#region package.json
333
- var version = "5.8.0";
334
- //#endregion
335
335
  //#region src/constants.ts
336
336
  /**
337
337
  * Stable codes Kubb attaches to a `Diagnostic`. Each maps to a known failure mode
@@ -443,10 +443,6 @@ const diagnosticCode = {
443
443
  */
444
444
  invalidPluginOptions: "KUBB_INVALID_PLUGIN_OPTIONS",
445
445
  /**
446
- * An extension's options fail its options `schema`.
447
- */
448
- invalidOptions: "KUBB_INVALID_OPTIONS",
449
- /**
450
446
  * A post-generate command (`output.postGenerate`) exited with a failure.
451
447
  */
452
448
  postGenerateFailed: "KUBB_POST_GENERATE_FAILED",
@@ -654,11 +650,6 @@ const diagnosticCatalog = {
654
650
  cause: "A plugin was configured with options that cannot be honored, for example `output.mode: 'file'` paired with a `group` option.",
655
651
  fix: "Fix the plugin options. A single-file output has nothing to group, so remove the `group` option or use `output.mode: 'directory'`."
656
652
  },
657
- [diagnosticCode.invalidOptions]: {
658
- title: "Invalid options",
659
- cause: "A plugin declares an options `schema`, and the options passed to it in kubb.config.ts do not match it.",
660
- fix: "Fix each option the message lists by its path, such as `enum.type`. The plugin documentation lists the accepted values."
661
- },
662
653
  [diagnosticCode.postGenerateFailed]: {
663
654
  title: "Post-generate command failed",
664
655
  cause: "A post-generate command (`output.postGenerate`) exited with a non-zero status.",
@@ -932,6 +923,86 @@ var Diagnostics = class Diagnostics {
932
923
  }
933
924
  };
934
925
  //#endregion
926
+ //#region src/defineExtension.ts
927
+ /**
928
+ * Returns `meta`, or `{ name }` for an extension that only sets the deprecated `name`. Throws `KUBB_INVALID_PLUGIN_OPTIONS` when it has neither.
929
+ */
930
+ function resolveMeta({ name, meta }) {
931
+ if (meta) return meta;
932
+ if (name) return { name };
933
+ throw new Diagnostics.Error({
934
+ code: Diagnostics.code.invalidPluginOptions,
935
+ severity: "error",
936
+ message: "An extension has neither `meta.name` nor `name`.",
937
+ help: "Set `meta: { name }` on the plugin or parser.",
938
+ location: { kind: "config" }
939
+ });
940
+ }
941
+ /**
942
+ * Throws `KUBB_INCOMPATIBLE_EXTENSION` when the running Kubb or Node.js version is outside `meta.compatibility`.
943
+ */
944
+ function checkCompatibility(meta) {
945
+ const checks = [{
946
+ target: "Kubb",
947
+ range: meta.compatibility?.kubb,
948
+ version
949
+ }, {
950
+ target: "Node.js",
951
+ range: meta.compatibility?.node,
952
+ version: process.versions.node
953
+ }];
954
+ for (const { target, range, version } of checks) {
955
+ if (!range || satisfies(version, range, { includePrerelease: true })) continue;
956
+ throw new Diagnostics.Error({
957
+ code: Diagnostics.code.incompatibleExtension,
958
+ severity: "error",
959
+ message: `"${meta.name}" requires ${target} ${range}, but ${version} is running.`,
960
+ help: `Use a ${target} version that satisfies ${range}, or a version of "${meta.name}" that supports ${target} ${version}.`,
961
+ location: { kind: "config" },
962
+ plugin: meta.name
963
+ });
964
+ }
965
+ }
966
+ /**
967
+ * Throws `KUBB_MISSING_DEPENDENCY` for a missing required entry in a `dependencies` record, and
968
+ * `KUBB_INCOMPATIBLE_DEPENDENCY` when an entry's `meta.version` is out of range. Arrays are not checked.
969
+ */
970
+ function checkDependencies({ name, dependencies, plugins }) {
971
+ if (!dependencies || Array.isArray(dependencies)) return;
972
+ for (const [dependencyName, dependency] of Object.entries(dependencies)) {
973
+ const target = plugins.get(dependencyName);
974
+ if (!target) {
975
+ if (dependency?.optional) continue;
976
+ throw new Diagnostics.Error({
977
+ code: Diagnostics.code.missingDependency,
978
+ severity: "error",
979
+ message: `Plugin "${dependencyName}" is required by "${name}" but not found.`,
980
+ help: `Add "${dependencyName}" to the \`plugins\` array in kubb.config.ts.`,
981
+ location: { kind: "config" },
982
+ plugin: name
983
+ });
984
+ }
985
+ const range = dependency?.version;
986
+ const version = target.meta?.version;
987
+ if (!range || !version || satisfies(version, range, { includePrerelease: true })) continue;
988
+ throw new Diagnostics.Error({
989
+ code: Diagnostics.code.incompatibleDependency,
990
+ severity: "error",
991
+ message: `"${name}" requires "${dependencyName}" ${range}, but ${version} is configured.`,
992
+ help: `Install a version of "${dependencyName}" that satisfies ${range}.`,
993
+ location: { kind: "config" },
994
+ plugin: name
995
+ });
996
+ }
997
+ }
998
+ function defineExtension({ kind, meta, build }) {
999
+ const factory = (options) => build(options ?? {});
1000
+ return Object.assign(factory, meta ? {
1001
+ kind,
1002
+ meta
1003
+ } : { kind });
1004
+ }
1005
+ //#endregion
935
1006
  //#region src/composables.ts
936
1007
  const kubbContextKey = Symbol.for("kubb.context");
937
1008
  const host = globalThis;
@@ -999,7 +1070,7 @@ function injectFile(file) {
999
1070
  requireContext("injectFile").injectFile(file);
1000
1071
  }
1001
1072
  /**
1002
- * Returns the options of the plugin being set up, parsed by its options `schema` when it has one, like `ctx.options`.
1073
+ * Returns the options the user passed to the plugin being set up, like `ctx.options`.
1003
1074
  */
1004
1075
  function useOptions() {
1005
1076
  return requireContext("useOptions").options;
@@ -1023,123 +1094,6 @@ function requirePlugin(name) {
1023
1094
  return requireContext("requirePlugin").requirePlugin(name);
1024
1095
  }
1025
1096
  //#endregion
1026
- //#region src/defineExtension.ts
1027
- /**
1028
- * Returns `meta`, or `{ name }` for an extension that only sets the deprecated `name`. Throws `KUBB_INVALID_PLUGIN_OPTIONS` when it has neither.
1029
- */
1030
- function resolveMeta({ name, meta }) {
1031
- if (meta) return meta;
1032
- if (name) return { name };
1033
- throw new Diagnostics.Error({
1034
- code: Diagnostics.code.invalidPluginOptions,
1035
- severity: "error",
1036
- message: "An extension has neither `meta.name` nor `name`.",
1037
- help: "Set `meta: { name }` on the plugin or parser.",
1038
- location: { kind: "config" }
1039
- });
1040
- }
1041
- /**
1042
- * Throws `KUBB_INCOMPATIBLE_EXTENSION` when the running Kubb or Node.js version is outside `meta.compatibility`.
1043
- */
1044
- function checkCompatibility(meta) {
1045
- const checks = [{
1046
- target: "Kubb",
1047
- range: meta.compatibility?.kubb,
1048
- version
1049
- }, {
1050
- target: "Node.js",
1051
- range: meta.compatibility?.node,
1052
- version: process.versions.node
1053
- }];
1054
- for (const { target, range, version } of checks) {
1055
- if (!range || satisfies(version, range, { includePrerelease: true })) continue;
1056
- throw new Diagnostics.Error({
1057
- code: Diagnostics.code.incompatibleExtension,
1058
- severity: "error",
1059
- message: `"${meta.name}" requires ${target} ${range}, but ${version} is running.`,
1060
- help: `Use a ${target} version that satisfies ${range}, or a version of "${meta.name}" that supports ${target} ${version}.`,
1061
- location: { kind: "config" },
1062
- plugin: meta.name
1063
- });
1064
- }
1065
- }
1066
- /**
1067
- * Throws `KUBB_MISSING_DEPENDENCY` for a missing required entry in a `dependencies` record, and
1068
- * `KUBB_INCOMPATIBLE_DEPENDENCY` when an entry's `meta.version` is out of range. Arrays are not checked.
1069
- */
1070
- function checkDependencies({ name, dependencies, plugins }) {
1071
- if (!dependencies || Array.isArray(dependencies)) return;
1072
- for (const [dependencyName, dependency] of Object.entries(dependencies)) {
1073
- const target = plugins.get(dependencyName);
1074
- if (!target) {
1075
- if (dependency?.optional) continue;
1076
- throw new Diagnostics.Error({
1077
- code: Diagnostics.code.missingDependency,
1078
- severity: "error",
1079
- message: `Plugin "${dependencyName}" is required by "${name}" but not found.`,
1080
- help: `Add "${dependencyName}" to the \`plugins\` array in kubb.config.ts.`,
1081
- location: { kind: "config" },
1082
- plugin: name
1083
- });
1084
- }
1085
- const range = dependency?.version;
1086
- const version = target.meta?.version;
1087
- if (!range || !version || satisfies(version, range, { includePrerelease: true })) continue;
1088
- throw new Diagnostics.Error({
1089
- code: Diagnostics.code.incompatibleDependency,
1090
- severity: "error",
1091
- message: `"${name}" requires "${dependencyName}" ${range}, but ${version} is configured.`,
1092
- help: `Install a version of "${dependencyName}" that satisfies ${range}.`,
1093
- location: { kind: "config" },
1094
- plugin: name
1095
- });
1096
- }
1097
- }
1098
- function formatIssuePath(path) {
1099
- return (path ?? []).map((segment) => String(typeof segment === "object" ? segment.key : segment)).join(".");
1100
- }
1101
- /**
1102
- * Validates an extension's raw options against its `schema` and returns the parsed value, with the schema's defaults.
1103
- * Throws `KUBB_INVALID_OPTIONS`, naming the extension and each issue path, when the options are invalid.
1104
- * Refuses an async validator, because extension factories run synchronously while the config loads.
1105
- */
1106
- function parseExtensionOptions({ kind, meta, schema, input }) {
1107
- const validated = schema["~standard"].validate(input);
1108
- if (typeof validated.then === "function") {
1109
- Promise.resolve(validated).catch(() => void 0);
1110
- throw new Error(`The options schema of ${kind} "${meta.name}" validates asynchronously. Use a synchronous schema, because Kubb validates options while the config loads.`);
1111
- }
1112
- const result = validated;
1113
- if (!result.issues) return result.value;
1114
- const issues = result.issues.map((issue) => {
1115
- const path = formatIssuePath(issue.path);
1116
- return path ? `\`${path}\`: ${issue.message}` : issue.message;
1117
- });
1118
- throw new Diagnostics.Error({
1119
- code: Diagnostics.code.invalidOptions,
1120
- severity: "error",
1121
- message: `Invalid options for ${kind} "${meta.name}". ${issues.join("; ")}`,
1122
- help: `Fix the listed options of "${meta.name}" in kubb.config.ts. Its documentation lists the accepted values.`,
1123
- location: { kind: "config" },
1124
- ...kind === "plugin" && { plugin: meta.name }
1125
- });
1126
- }
1127
- function defineExtension({ kind, meta, schema, build }) {
1128
- const factory = (raw) => {
1129
- const options = raw ?? {};
1130
- return build({
1131
- options,
1132
- parsedOptions: schema && meta ? parseExtensionOptions({
1133
- kind,
1134
- meta,
1135
- schema,
1136
- input: options
1137
- }) : options
1138
- });
1139
- };
1140
- return Object.assign(factory, { kind }, meta && { meta }, schema && { schema });
1141
- }
1142
- //#endregion
1143
1097
  //#region src/definePlugin.ts
1144
1098
  /**
1145
1099
  * Merges the `output.mode` default into the output config and validates the combination.
@@ -1182,7 +1136,7 @@ function assertPluginOption({ plugin, option, value, allowed }) {
1182
1136
  function definePlugin(input) {
1183
1137
  if (typeof input === "function") return defineExtension({
1184
1138
  kind: "plugin",
1185
- build: ({ options }) => {
1139
+ build: (options) => {
1186
1140
  const plugin = input(options);
1187
1141
  return {
1188
1142
  ...plugin,
@@ -1190,16 +1144,14 @@ function definePlugin(input) {
1190
1144
  };
1191
1145
  }
1192
1146
  });
1193
- const { meta, schema, dependencies, enforce, hooks = {}, setup } = input;
1147
+ const { meta, dependencies, enforce, hooks = {}, setup } = input;
1194
1148
  return defineExtension({
1195
1149
  kind: "plugin",
1196
1150
  meta,
1197
- schema,
1198
- build: ({ options, parsedOptions }) => ({
1151
+ build: (options) => ({
1199
1152
  name: meta.name,
1200
1153
  meta,
1201
1154
  options,
1202
- ...schema && { parsedOptions },
1203
1155
  dependencies,
1204
1156
  enforce,
1205
1157
  hooks,
@@ -2242,26 +2194,25 @@ var KubbDriver = class {
2242
2194
  this.adapter = config.adapter ?? null;
2243
2195
  }
2244
2196
  /**
2245
- * Normalizes and checks every configured plugin, orders them, and registers their lifecycle handlers.
2197
+ * Checks the adapter's and parsers' `meta.compatibility`, then normalizes and checks every configured plugin, orders them, and registers their lifecycle handlers.
2246
2198
  * A plugin that another lists as a dependency runs first, then `enforce: 'pre'` before
2247
2199
  * `'post'`. When the config has an adapter, the adapter source is resolved from the input
2248
2200
  * so `run` can parse it later.
2249
2201
  */
2250
2202
  async setup() {
2251
2203
  this.options.signal?.throwIfAborted();
2252
- for (const parser of this.config.parsers) if (parser.meta) checkCompatibility(parser.meta);
2204
+ for (const extension of [this.config.adapter, ...this.config.parsers]) if (extension?.meta) checkCompatibility(extension.meta);
2253
2205
  const normalized = this.#sortPlugins(this.config.plugins.map((rawPlugin) => {
2254
2206
  if (rawPlugin.meta) checkCompatibility(rawPlugin.meta);
2255
2207
  const meta = resolveMeta(rawPlugin);
2256
- const options = rawPlugin.parsedOptions ?? rawPlugin.options;
2257
2208
  return {
2258
2209
  name: meta.name,
2259
2210
  meta,
2260
- dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2211
+ dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(rawPlugin.options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2261
2212
  enforce: rawPlugin.enforce,
2262
2213
  hooks: rawPlugin.hooks,
2263
2214
  setup: rawPlugin.setup,
2264
- options: options ?? defaultPluginOptions(),
2215
+ options: rawPlugin.options ?? defaultPluginOptions(),
2265
2216
  resolver: createResolver({ pluginName: meta.name })
2266
2217
  };
2267
2218
  }));
@@ -2833,6 +2784,6 @@ function pluginNotFoundError({ pluginName, requiredBy }) {
2833
2784
  });
2834
2785
  }
2835
2786
  //#endregion
2836
- export { clean as A, useConfig as C, formatMs as D, Diagnostics as E, write as F, toError as I, camelCase as L, isPathInside as M, read as N, getElapsedMs as O, toPosixPath as P, tryUseKubb as S, useOptions as T, kubbContext as _, _usingCtx as a, setOptions as b, Resolver as c, definePlugin as d, defineExtension as f, injectFile as g, addMacro as h, FileManager as i, exists as j, inParallel as k, getInputKind as l, addGenerator as m, pluginNotFoundError as n, Hookable as o, resolveMeta as p, createNodeCache as r, createResolver as s, KubbDriver as t, assertPluginOption as u, requirePlugin as v, useKubb as w, setResolver as x, setMacros as y };
2787
+ export { inParallel as A, useOptions as C, Diagnostics as D, resolveMeta as E, toPosixPath as F, write as I, toError as L, exists as M, isPathInside as N, formatMs as O, read as P, camelCase as R, useKubb as S, defineExtension as T, setMacros as _, _usingCtx as a, tryUseKubb as b, Resolver as c, definePlugin as d, addGenerator as f, requirePlugin as g, kubbContext as h, FileManager as i, clean as j, getElapsedMs as k, getInputKind as l, injectFile as m, pluginNotFoundError as n, Hookable as o, addMacro as p, createNodeCache as r, createResolver as s, KubbDriver as t, assertPluginOption as u, setOptions as v, checkCompatibility as w, useConfig as x, setResolver as y };
2837
2788
 
2838
- //# sourceMappingURL=KubbDriver-BrltQ0nL.js.map
2789
+ //# sourceMappingURL=KubbDriver-Dz36Klt9.js.map