unplugin-kubb 5.12.5 → 5.12.7
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/README.md +12 -2
- package/dist/esbuild.js +1 -1
- package/dist/farm.js +1 -1
- package/dist/generateModule.d.ts +29 -0
- package/dist/generateModule.js +60 -0
- package/dist/generateModule.js.map +1 -0
- package/dist/index.d.ts +27 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/rolldown.js +1 -1
- package/dist/rollup.js +1 -1
- package/dist/rspack.js +1 -1
- package/dist/unpluginFactory-CLbstatM.js +241 -0
- package/dist/unpluginFactory-CLbstatM.js.map +1 -0
- package/dist/unpluginFactory.d.ts +6 -8
- package/dist/unpluginFactory.js +1 -102
- package/dist/vite.js +1 -1
- package/dist/webpack.js +1 -1
- package/package.json +11 -8
- package/dist/unpluginFactory.js.map +0 -1
package/README.md
CHANGED
|
@@ -115,15 +115,25 @@ export default defineConfig({
|
|
|
115
115
|
})
|
|
116
116
|
```
|
|
117
117
|
|
|
118
|
+
## Import a spec
|
|
119
|
+
|
|
120
|
+
Import a `*.openapi.yaml` or `*.openapi.yml` file to get the output of every plugin in your config as one module. This also works in the Vite dev server.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { getPetById, petSchema } from './petStore.openapi.yaml'
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
For types in `tsc` and your editor, add [`openapi-import`](https://github.com/kubb-labs/openapi-import).
|
|
127
|
+
|
|
118
128
|
## Options
|
|
119
129
|
|
|
120
130
|
### config
|
|
121
131
|
|
|
122
|
-
|
|
132
|
+
Your Kubb config. Without it, the plugin loads `kubb.config.ts` like the CLI does.
|
|
123
133
|
|
|
124
134
|
```typescript [Options]
|
|
125
135
|
type Options = {
|
|
126
|
-
config
|
|
136
|
+
config?: UserConfig
|
|
127
137
|
}
|
|
128
138
|
```
|
|
129
139
|
|
package/dist/esbuild.js
CHANGED
package/dist/farm.js
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Hookable, KubbHooks, ProblemDiagnostic, UserConfig } from "@kubb/core";
|
|
2
|
+
//#region src/generateModule.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The config whose `input` is `fileName`, else the first one without an `input`. A config for
|
|
5
|
+
* another spec does not apply, so it returns `undefined` when no config fits. A relative `root`
|
|
6
|
+
* resolves from `cwd`, and `input` from `root`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function selectConfig({ configs, fileName, cwd }: {
|
|
9
|
+
configs: Array<UserConfig>;
|
|
10
|
+
fileName: string;
|
|
11
|
+
cwd: string;
|
|
12
|
+
}): UserConfig | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Generates the output of every plugin in `config` for one spec as one TypeScript module, without
|
|
15
|
+
* writing files. Only `config`'s adapter and plugins are used. `text` is empty when `errors` is not.
|
|
16
|
+
* The `*.openapi.yaml` loader and the `openapi-import` content mapper share it.
|
|
17
|
+
*/
|
|
18
|
+
export declare function generateModule({ root, input, config, hooks }: {
|
|
19
|
+
/** Folder of the spec; relative imports in the module resolve from here. */
|
|
20
|
+
root: string;
|
|
21
|
+
input: UserConfig['input'];
|
|
22
|
+
config: UserConfig;
|
|
23
|
+
hooks?: Hookable<KubbHooks>;
|
|
24
|
+
}): Promise<{
|
|
25
|
+
text: string;
|
|
26
|
+
errors: Array<ProblemDiagnostic>;
|
|
27
|
+
}>;
|
|
28
|
+
//#endregion
|
|
29
|
+
//# sourceMappingURL=generateModule.d.ts.map
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { adapterOas } from "@kubb/adapter-oas";
|
|
4
|
+
import { Diagnostics, createKubb, memoryStorage } from "@kubb/core";
|
|
5
|
+
import { parserTs } from "@kubb/parser-ts";
|
|
6
|
+
import { pluginBarrelName } from "@kubb/plugin-barrel";
|
|
7
|
+
//#region src/generateModule.ts
|
|
8
|
+
const moduleFile = "index.ts";
|
|
9
|
+
const toModulePlugin = (plugin) => ({
|
|
10
|
+
...plugin,
|
|
11
|
+
options: {
|
|
12
|
+
...plugin.options,
|
|
13
|
+
output: {
|
|
14
|
+
...plugin.options?.output,
|
|
15
|
+
path: moduleFile,
|
|
16
|
+
mode: "file"
|
|
17
|
+
},
|
|
18
|
+
group: void 0
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* The config whose `input` is `fileName`, else the first one without an `input`. A config for
|
|
23
|
+
* another spec does not apply, so it returns `undefined` when no config fits. A relative `root`
|
|
24
|
+
* resolves from `cwd`, and `input` from `root`.
|
|
25
|
+
*/
|
|
26
|
+
function selectConfig({ configs, fileName, cwd }) {
|
|
27
|
+
const matches = (config) => typeof config.input === "string" && path.resolve(cwd, config.root || ".", config.input) === path.resolve(fileName);
|
|
28
|
+
return configs.find(matches) ?? configs.find((config) => config.input === void 0);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Generates the output of every plugin in `config` for one spec as one TypeScript module, without
|
|
32
|
+
* writing files. Only `config`'s adapter and plugins are used. `text` is empty when `errors` is not.
|
|
33
|
+
* The `*.openapi.yaml` loader and the `openapi-import` content mapper share it.
|
|
34
|
+
*/
|
|
35
|
+
async function generateModule({ root, input, config, hooks }) {
|
|
36
|
+
const storage = memoryStorage();
|
|
37
|
+
const { diagnostics } = await createKubb({
|
|
38
|
+
root,
|
|
39
|
+
input,
|
|
40
|
+
output: {
|
|
41
|
+
path: ".",
|
|
42
|
+
barrel: false,
|
|
43
|
+
format: false,
|
|
44
|
+
lint: false
|
|
45
|
+
},
|
|
46
|
+
adapter: config.adapter ?? adapterOas(),
|
|
47
|
+
parsers: [parserTs()],
|
|
48
|
+
plugins: (config.plugins ?? []).filter((plugin) => plugin.name !== pluginBarrelName).map(toModulePlugin),
|
|
49
|
+
storage
|
|
50
|
+
}, { hooks }).safeBuild();
|
|
51
|
+
const errors = diagnostics.filter(Diagnostics.isProblem).filter((diagnostic) => diagnostic.severity === "error");
|
|
52
|
+
return {
|
|
53
|
+
text: errors.length ? "" : await storage.readItem(path.resolve(root, moduleFile)) ?? "",
|
|
54
|
+
errors
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { generateModule, selectConfig };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=generateModule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateModule.js","names":[],"sources":["../src/generateModule.ts"],"sourcesContent":["import path from 'node:path'\nimport { adapterOas } from '@kubb/adapter-oas'\nimport {\n createKubb,\n Diagnostics,\n type Hookable,\n type KubbHooks,\n memoryStorage,\n type Output,\n type Plugin,\n type ProblemDiagnostic,\n type UserConfig,\n} from '@kubb/core'\nimport { parserTs } from '@kubb/parser-ts'\nimport { pluginBarrelName } from '@kubb/plugin-barrel'\n\n// Every plugin writes to this one file, so Kubb merges their output and drops the imports between them.\nconst moduleFile = 'index.ts'\n\nconst toModulePlugin = (plugin: Plugin): Plugin => ({\n ...plugin,\n options: {\n ...plugin.options,\n // An explicit `mode: 'directory'` would otherwise survive and write under `index.ts/`.\n output: { ...(plugin.options as { output?: Output } | undefined)?.output, path: moduleFile, mode: 'file' },\n group: undefined,\n },\n})\n\n/**\n * The config whose `input` is `fileName`, else the first one without an `input`. A config for\n * another spec does not apply, so it returns `undefined` when no config fits. A relative `root`\n * resolves from `cwd`, and `input` from `root`.\n */\nexport function selectConfig({ configs, fileName, cwd }: { configs: Array<UserConfig>; fileName: string; cwd: string }): UserConfig | undefined {\n const matches = (config: UserConfig) => typeof config.input === 'string' && path.resolve(cwd, config.root || '.', config.input) === path.resolve(fileName)\n\n return configs.find(matches) ?? configs.find((config) => config.input === undefined)\n}\n\n/**\n * Generates the output of every plugin in `config` for one spec as one TypeScript module, without\n * writing files. Only `config`'s adapter and plugins are used. `text` is empty when `errors` is not.\n * The `*.openapi.yaml` loader and the `openapi-import` content mapper share it.\n */\nexport async function generateModule({\n root,\n input,\n config,\n hooks,\n}: {\n /** Folder of the spec; relative imports in the module resolve from here. */\n root: string\n input: UserConfig['input']\n config: UserConfig\n hooks?: Hookable<KubbHooks>\n}): Promise<{ text: string; errors: Array<ProblemDiagnostic> }> {\n const storage = memoryStorage()\n const { diagnostics } = await createKubb(\n {\n root,\n input,\n output: { path: '.', barrel: false, format: false, lint: false },\n adapter: config.adapter ?? adapterOas(),\n parsers: [parserTs()],\n plugins: (config.plugins ?? []).filter((plugin) => plugin.name !== pluginBarrelName).map(toModulePlugin),\n storage,\n },\n { hooks },\n ).safeBuild()\n const errors = diagnostics.filter(Diagnostics.isProblem).filter((diagnostic) => diagnostic.severity === 'error')\n // Files written elsewhere (a bundled client runtime) are left out; no output is an empty module.\n const text = errors.length ? '' : ((await storage.readItem(path.resolve(root, moduleFile))) ?? '')\n\n return { text, errors }\n}\n"],"mappings":";;;;;;;AAiBA,MAAM,aAAa;AAEnB,MAAM,kBAAkB,YAA4B;CAClD,GAAG;CACH,SAAS;EACP,GAAG,OAAO;EAEV,QAAQ;GAAE,GAAI,OAAO,SAA6C;GAAQ,MAAM;GAAY,MAAM;EAAO;EACzG,OAAO,KAAA;CACT;AACF;;;;;;AAOA,SAAgB,aAAa,EAAE,SAAS,UAAU,OAA8F;CAC9I,MAAM,WAAW,WAAuB,OAAO,OAAO,UAAU,YAAY,KAAK,QAAQ,KAAK,OAAO,QAAQ,KAAK,OAAO,KAAK,MAAM,KAAK,QAAQ,QAAQ;CAEzJ,OAAO,QAAQ,KAAK,OAAO,KAAK,QAAQ,MAAM,WAAW,OAAO,UAAU,KAAA,CAAS;AACrF;;;;;;AAOA,eAAsB,eAAe,EACnC,MACA,OACA,QACA,SAO8D;CAC9D,MAAM,UAAU,cAAc;CAC9B,MAAM,EAAE,gBAAgB,MAAM,WAC5B;EACE;EACA;EACA,QAAQ;GAAE,MAAM;GAAK,QAAQ;GAAO,QAAQ;GAAO,MAAM;EAAM;EAC/D,SAAS,OAAO,WAAW,WAAW;EACtC,SAAS,CAAC,SAAS,CAAC;EACpB,UAAU,OAAO,WAAW,CAAC,EAAA,CAAG,QAAQ,WAAW,OAAO,SAAS,gBAAgB,CAAC,CAAC,IAAI,cAAc;EACvG;CACF,GACA,EAAE,MAAM,CACV,CAAC,CAAC,UAAU;CACZ,MAAM,SAAS,YAAY,OAAO,YAAY,SAAS,CAAC,CAAC,QAAQ,eAAe,WAAW,aAAa,OAAO;CAI/G,OAAO;EAAE,MAFI,OAAO,SAAS,KAAO,MAAM,QAAQ,SAAS,KAAK,QAAQ,MAAM,UAAU,CAAC,KAAM;EAEhF;CAAO;AACxB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
import { Options, applyDefaults, unpluginFactory } from "./unpluginFactory.js";
|
|
2
|
+
import { generateModule, selectConfig } from "./generateModule.js";
|
|
3
|
+
//#region ../../internals/shared/src/config.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Every config file path the Kubb CLI checks in `dir`, in the order it checks them.
|
|
6
|
+
*/
|
|
7
|
+
export declare function configFileCandidates(dir: string): Array<string>;
|
|
8
|
+
/**
|
|
9
|
+
* The first Kubb config file in `cwd` or the nearest parent folder that has one: `.kubbrc` before
|
|
10
|
+
* `kubb.config`, the folder itself before its `.config/` and `configs/`, and {@link CONFIG_EXTENSIONS} in order.
|
|
11
|
+
*/
|
|
12
|
+
export declare function findConfigFile(cwd?: string): string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Loads a Kubb config module, calls it with `cli` when it exports a function, and returns its
|
|
15
|
+
* configs as a list, so a single config and a `defineConfig([...])` array read the same. Each
|
|
16
|
+
* `root` resolves from the folder the config belongs to: the file's folder, or the folder above
|
|
17
|
+
* `.config/` and `configs/`. A missing `root` becomes that folder, so the CLI, the bundler, and
|
|
18
|
+
* an editor read the same paths wherever they run.
|
|
19
|
+
*/
|
|
20
|
+
export declare function loadConfigFile<T extends {
|
|
21
|
+
root?: string;
|
|
22
|
+
}>({ file, cli }: {
|
|
23
|
+
file: string;
|
|
24
|
+
cli: unknown;
|
|
25
|
+
}): Promise<Array<T>>;
|
|
26
|
+
//#endregion
|
|
2
27
|
//#region src/index.d.ts
|
|
3
|
-
declare const plugin: import("unplugin").UnpluginInstance<Options | undefined,
|
|
28
|
+
declare const plugin: import("unplugin").UnpluginInstance<Options | undefined, true>;
|
|
4
29
|
//#endregion
|
|
5
|
-
export { type Options, applyDefaults, plugin as default, plugin as unplugin, unpluginFactory };
|
|
30
|
+
export { type Options, applyDefaults, plugin as default, plugin as unplugin, generateModule, selectConfig, unpluginFactory };
|
|
6
31
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as loadConfigFile, i as findConfigFile, n as unpluginFactory, r as configFileCandidates, t as applyDefaults } from "./unpluginFactory-CLbstatM.js";
|
|
2
|
+
import { generateModule, selectConfig } from "./generateModule.js";
|
|
2
3
|
import { createUnplugin } from "unplugin";
|
|
3
4
|
//#region src/index.ts
|
|
4
5
|
const plugin = /* #__PURE__ */ createUnplugin(unpluginFactory);
|
|
5
6
|
//#endregion
|
|
6
|
-
export { applyDefaults, plugin as default, plugin as unplugin, unpluginFactory };
|
|
7
|
+
export { applyDefaults, configFileCandidates, plugin as default, plugin as unplugin, findConfigFile, generateModule, loadConfigFile, selectConfig, unpluginFactory };
|
|
7
8
|
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { createUnplugin } from 'unplugin'\nimport { unpluginFactory } from './unpluginFactory.ts'\n\nexport { applyDefaults, type Options, unpluginFactory } from './unpluginFactory.ts'\n\nconst plugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport { plugin as unplugin, plugin as default }\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { createUnplugin } from 'unplugin'\nimport { unpluginFactory } from './unpluginFactory.ts'\n\nexport { configFileCandidates, findConfigFile, loadConfigFile } from '@internals/shared'\nexport { generateModule, selectConfig } from './generateModule.ts'\nexport { applyDefaults, type Options, unpluginFactory } from './unpluginFactory.ts'\n\nconst plugin = /* #__PURE__ */ createUnplugin(unpluginFactory)\n\nexport { plugin as unplugin, plugin as default }\n"],"mappings":";;;;AAOA,MAAM,SAAyB,+BAAe,eAAe"}
|
package/dist/rolldown.js
CHANGED
package/dist/rollup.js
CHANGED
package/dist/rspack.js
CHANGED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
+
import { generateModule, selectConfig } from "./generateModule.js";
|
|
3
|
+
import path, { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { adapterOas } from "@kubb/adapter-oas";
|
|
5
|
+
import { Diagnostics, Hookable, cliReporter, createCliReporter, createKubb, fileReporter, htmlReporter, jsonReporter, logLevel } from "@kubb/core";
|
|
6
|
+
import { parserMd } from "@kubb/parser-md";
|
|
7
|
+
import { parserTs, parserTsx } from "@kubb/parser-ts";
|
|
8
|
+
import { pluginBarrel, pluginBarrelName } from "@kubb/plugin-barrel";
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import process$1 from "node:process";
|
|
11
|
+
import { readFile } from "node:fs/promises";
|
|
12
|
+
import { createJiti } from "jiti";
|
|
13
|
+
import ts from "@typescript/typescript6";
|
|
14
|
+
//#region ../../internals/shared/src/constants.ts
|
|
15
|
+
/** File extensions a Kubb config file may use. */
|
|
16
|
+
const CONFIG_EXTENSIONS = [
|
|
17
|
+
".ts",
|
|
18
|
+
".mts",
|
|
19
|
+
".cts",
|
|
20
|
+
".js",
|
|
21
|
+
".mjs",
|
|
22
|
+
".cjs"
|
|
23
|
+
];
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region ../../internals/shared/src/loader.ts
|
|
26
|
+
/**
|
|
27
|
+
* jiti options for loading Kubb config modules: the automatic JSX runtime pointed at
|
|
28
|
+
* `@kubb/renderer-jsx`, and `moduleCache` off so a re-load re-evaluates the file.
|
|
29
|
+
*/
|
|
30
|
+
const JITI_OPTIONS = {
|
|
31
|
+
jsx: {
|
|
32
|
+
runtime: "automatic",
|
|
33
|
+
importSource: "@kubb/renderer-jsx"
|
|
34
|
+
},
|
|
35
|
+
moduleCache: false
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Plain JavaScript files. jiti hands these to Node's native `import()`, whose module cache never
|
|
39
|
+
* lets go of a file, so `moduleCache: false` alone would keep returning the first version of it.
|
|
40
|
+
*/
|
|
41
|
+
const JAVASCRIPT_EXTENSION = /\.[cm]?jsx?$/;
|
|
42
|
+
const jiti = createJiti(import.meta.url, JITI_OPTIONS);
|
|
43
|
+
/**
|
|
44
|
+
* Loads a Kubb TypeScript or JavaScript config module via jiti.
|
|
45
|
+
*
|
|
46
|
+
* jiti transpiles TypeScript and the `@kubb/renderer-jsx` JSX runtime on the fly. Every load
|
|
47
|
+
* re-evaluates the file, so a config edited on disk is picked up by the next load.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const config = await loadModule('/abs/kubb.config.ts', { default: true })
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
async function loadModule(filePath, options) {
|
|
55
|
+
if (!JAVASCRIPT_EXTENSION.test(filePath)) return options?.default ? jiti.import(filePath, { default: true }) : jiti.import(filePath);
|
|
56
|
+
const mod = await jiti.evalModule(await readFile(filePath, "utf8"), {
|
|
57
|
+
filename: filePath,
|
|
58
|
+
async: true,
|
|
59
|
+
forceTranspile: true
|
|
60
|
+
});
|
|
61
|
+
return options?.default ? mod?.default ?? mod : mod;
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region ../../internals/shared/src/config.ts
|
|
65
|
+
const CONFIG_FOLDERS = [".config", "configs"];
|
|
66
|
+
const SEARCH_FILES = ["", ...CONFIG_FOLDERS.map((folder) => `${folder}/`)].flatMap((prefix) => [`${prefix}.kubbrc`, `${prefix}kubb.config`]);
|
|
67
|
+
/**
|
|
68
|
+
* Every config file path the Kubb CLI checks in `dir`, in the order it checks them.
|
|
69
|
+
*/
|
|
70
|
+
function configFileCandidates(dir) {
|
|
71
|
+
return SEARCH_FILES.flatMap((file) => CONFIG_EXTENSIONS.map((extension) => join(dir, `${file}${extension}`)));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The first Kubb config file in `cwd` or the nearest parent folder that has one: `.kubbrc` before
|
|
75
|
+
* `kubb.config`, the folder itself before its `.config/` and `configs/`, and {@link CONFIG_EXTENSIONS} in order.
|
|
76
|
+
*/
|
|
77
|
+
function findConfigFile(cwd = process$1.cwd()) {
|
|
78
|
+
for (let dir = resolve(cwd);; dir = dirname(dir)) {
|
|
79
|
+
const found = configFileCandidates(dir).find((file) => existsSync(file));
|
|
80
|
+
if (found || dirname(dir) === dir) return found;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Loads a Kubb config module, calls it with `cli` when it exports a function, and returns its
|
|
85
|
+
* configs as a list, so a single config and a `defineConfig([...])` array read the same. Each
|
|
86
|
+
* `root` resolves from the folder the config belongs to: the file's folder, or the folder above
|
|
87
|
+
* `.config/` and `configs/`. A missing `root` becomes that folder, so the CLI, the bundler, and
|
|
88
|
+
* an editor read the same paths wherever they run.
|
|
89
|
+
*/
|
|
90
|
+
async function loadConfigFile({ file, cli }) {
|
|
91
|
+
const loaded = await loadModule(file, { default: true });
|
|
92
|
+
const resolved = await (typeof loaded === "function" ? loaded(cli) : loaded);
|
|
93
|
+
const dir = CONFIG_FOLDERS.includes(basename(dirname(file))) ? dirname(dirname(file)) : dirname(file);
|
|
94
|
+
return (Array.isArray(resolved) ? resolved : [resolved]).map((config) => ({
|
|
95
|
+
...config,
|
|
96
|
+
root: resolve(dir, config.root ?? ".")
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/unpluginFactory.ts
|
|
101
|
+
/**
|
|
102
|
+
* Applies the defaults `defineConfig` and the unplugin share to a single user config when not set.
|
|
103
|
+
*
|
|
104
|
+
* - `adapter` defaults to `adapterOas()`
|
|
105
|
+
* - `parsers` defaults to `[parserTs(), parserTsx(), parserMd()]`
|
|
106
|
+
* - `reporters` defaults to `[cliReporter, jsonReporter, fileReporter, htmlReporter]`
|
|
107
|
+
* - `plugins` gets `pluginBarrel()` appended when none is already present
|
|
108
|
+
* - `output.barrel` defaults to `false` when not set (`pluginBarrel` is always present after the step above, but generates nothing until configured)
|
|
109
|
+
* - `output.format` and `output.lint` default to `false`
|
|
110
|
+
*/
|
|
111
|
+
function applyDefaults(config) {
|
|
112
|
+
const plugins = config.plugins ?? [];
|
|
113
|
+
return {
|
|
114
|
+
...config,
|
|
115
|
+
adapter: config.adapter ?? adapterOas(),
|
|
116
|
+
parsers: config.parsers?.length ? config.parsers : [
|
|
117
|
+
parserTs(),
|
|
118
|
+
parserTsx(),
|
|
119
|
+
parserMd()
|
|
120
|
+
],
|
|
121
|
+
reporters: config.reporters?.length ? config.reporters : [
|
|
122
|
+
cliReporter,
|
|
123
|
+
jsonReporter,
|
|
124
|
+
fileReporter,
|
|
125
|
+
htmlReporter
|
|
126
|
+
],
|
|
127
|
+
plugins: plugins.some((plugin) => plugin.name === pluginBarrelName) ? plugins : [...plugins, pluginBarrel()],
|
|
128
|
+
output: {
|
|
129
|
+
barrel: false,
|
|
130
|
+
format: false,
|
|
131
|
+
lint: false,
|
|
132
|
+
...config.output
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/** Spec files an import loads as a module, the extensions `openapi-import` registers too. */
|
|
137
|
+
const specFilePattern = /\.openapi\.ya?ml$/;
|
|
138
|
+
/**
|
|
139
|
+
* Builds the Kubb unplugin for every unplugin bundler. `unplugin-kubb` runs the Kubb build once for
|
|
140
|
+
* each config with an `input` (only during `build` under Vite), and `unplugin-kubb:import` loads an
|
|
141
|
+
* imported {@link specFilePattern} file as one module. Without `config`, it loads `kubb.config.*`.
|
|
142
|
+
*/
|
|
143
|
+
const unpluginFactory = (options, meta) => {
|
|
144
|
+
const name = "unplugin-kubb";
|
|
145
|
+
const hooks = new Hookable();
|
|
146
|
+
const reporter = createCliReporter();
|
|
147
|
+
hooks.hook("kubb:generation:end", ({ config, diagnostics = [], filesCreated = 0, status = "success", hrStart = process.hrtime() }) => reporter.report({
|
|
148
|
+
config,
|
|
149
|
+
diagnostics,
|
|
150
|
+
filesCreated,
|
|
151
|
+
status,
|
|
152
|
+
hrStart
|
|
153
|
+
}, { logLevel: logLevel.info }));
|
|
154
|
+
const fail = (problem, message) => new Diagnostics.Error({
|
|
155
|
+
...problem ?? {
|
|
156
|
+
code: Diagnostics.code.unknown,
|
|
157
|
+
severity: "error"
|
|
158
|
+
},
|
|
159
|
+
message: `[${name}] ${message}`
|
|
160
|
+
});
|
|
161
|
+
async function readConfigs() {
|
|
162
|
+
if (options?.config) return [options.config];
|
|
163
|
+
const file = findConfigFile();
|
|
164
|
+
if (!file) throw new Diagnostics.Error({
|
|
165
|
+
code: Diagnostics.code.inputRequired,
|
|
166
|
+
severity: "error",
|
|
167
|
+
message: `[${name}] Config is not set`,
|
|
168
|
+
help: "Pass your Kubb config as the `config` option of the plugin, or add a `kubb.config.ts`.",
|
|
169
|
+
location: { kind: "config" }
|
|
170
|
+
});
|
|
171
|
+
return loadConfigFile({
|
|
172
|
+
file,
|
|
173
|
+
cli: {}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
let loaded;
|
|
177
|
+
const loadConfigs = () => loaded ??= readConfigs();
|
|
178
|
+
async function runBuild() {
|
|
179
|
+
for (const config of (await loadConfigs()).filter((config) => config.input)) {
|
|
180
|
+
const result = await createKubb(applyDefaults(config), { hooks }).generate();
|
|
181
|
+
if (result.success) continue;
|
|
182
|
+
const failedCount = Diagnostics.failedPlugins(result.diagnostics).length;
|
|
183
|
+
const firstError = result.diagnostics.filter(Diagnostics.isProblem).find((diagnostic) => diagnostic.severity === "error");
|
|
184
|
+
throw fail(firstError, failedCount > 0 ? `Build Error with ${failedCount} failed plugins` : firstError?.message ?? "Build failed");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
let generation;
|
|
188
|
+
return [{
|
|
189
|
+
name,
|
|
190
|
+
enforce: "pre",
|
|
191
|
+
apply: meta.framework === "vite" ? "build" : void 0,
|
|
192
|
+
async buildStart() {
|
|
193
|
+
generation ??= runBuild().catch((error) => {
|
|
194
|
+
generation = void 0;
|
|
195
|
+
throw error;
|
|
196
|
+
});
|
|
197
|
+
await generation;
|
|
198
|
+
}
|
|
199
|
+
}, {
|
|
200
|
+
name: `${name}:import`,
|
|
201
|
+
enforce: "pre",
|
|
202
|
+
loadInclude: (id) => specFilePattern.test(id),
|
|
203
|
+
async load(id) {
|
|
204
|
+
try {
|
|
205
|
+
const config = selectConfig({
|
|
206
|
+
configs: await loadConfigs(),
|
|
207
|
+
fileName: id,
|
|
208
|
+
cwd: process.cwd()
|
|
209
|
+
});
|
|
210
|
+
if (!config) throw new Diagnostics.Error({
|
|
211
|
+
code: Diagnostics.code.unknown,
|
|
212
|
+
severity: "error",
|
|
213
|
+
message: `[${name}] No Kubb config applies to ${id}`,
|
|
214
|
+
help: "Set `input` of a config to this spec, or add a config without `input` to serve every imported spec.",
|
|
215
|
+
location: { kind: "config" }
|
|
216
|
+
});
|
|
217
|
+
const { text, errors } = await generateModule({
|
|
218
|
+
root: path.dirname(id),
|
|
219
|
+
input: id,
|
|
220
|
+
config
|
|
221
|
+
});
|
|
222
|
+
if (errors.length) throw fail(errors[0], `${id}: ${errors[0].message}`);
|
|
223
|
+
return ts.transpileModule(text, {
|
|
224
|
+
fileName: `${id}.ts`,
|
|
225
|
+
compilerOptions: {
|
|
226
|
+
module: ts.ModuleKind.ESNext,
|
|
227
|
+
target: ts.ScriptTarget.ESNext
|
|
228
|
+
}
|
|
229
|
+
}).outputText;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (meta.framework !== "webpack" && meta.framework !== "rspack") throw error;
|
|
232
|
+
this.error(error);
|
|
233
|
+
return "export {}";
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}];
|
|
237
|
+
};
|
|
238
|
+
//#endregion
|
|
239
|
+
export { loadConfigFile as a, findConfigFile as i, unpluginFactory as n, configFileCandidates as r, applyDefaults as t };
|
|
240
|
+
|
|
241
|
+
//# sourceMappingURL=unpluginFactory-CLbstatM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unpluginFactory-CLbstatM.js","names":["process"],"sources":["../../../internals/shared/src/constants.ts","../../../internals/shared/src/loader.ts","../../../internals/shared/src/config.ts","../src/unpluginFactory.ts"],"sourcesContent":["export const KUBB_CONFIG_FILENAME = 'kubb.config.ts' as const\n\n/** File extensions a Kubb config file may use. */\nexport const CONFIG_EXTENSIONS = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs'] as const\n\nexport const KUBB_PACKAGE_NAME = 'kubb' as const\n\nexport const initDefaults = {\n inputPath: './openapi.yaml',\n outputPath: './src/gen',\n plugins: ['plugin-ts'],\n} as const\n","import { readFile } from 'node:fs/promises'\nimport { createJiti } from 'jiti'\n\n/**\n * jiti options for loading Kubb config modules: the automatic JSX runtime pointed at\n * `@kubb/renderer-jsx`, and `moduleCache` off so a re-load re-evaluates the file.\n */\nconst JITI_OPTIONS = {\n jsx: { runtime: 'automatic', importSource: '@kubb/renderer-jsx' },\n moduleCache: false,\n} satisfies Parameters<typeof createJiti>[1]\n\n/**\n * Plain JavaScript files. jiti hands these to Node's native `import()`, whose module cache never\n * lets go of a file, so `moduleCache: false` alone would keep returning the first version of it.\n */\nconst JAVASCRIPT_EXTENSION = /\\.[cm]?jsx?$/\n\n/**\n * Per-call options for {@link loadModule}.\n */\ntype LoadModuleOptions = {\n /**\n * Return the module's default export instead of the full namespace.\n */\n default?: boolean\n}\n\nconst jiti = createJiti(import.meta.url, JITI_OPTIONS)\n\n/**\n * Loads a Kubb TypeScript or JavaScript config module via jiti.\n *\n * jiti transpiles TypeScript and the `@kubb/renderer-jsx` JSX runtime on the fly. Every load\n * re-evaluates the file, so a config edited on disk is picked up by the next load.\n *\n * @example\n * ```ts\n * const config = await loadModule('/abs/kubb.config.ts', { default: true })\n * ```\n */\nexport async function loadModule<T = unknown>(filePath: string, options?: LoadModuleOptions): Promise<T> {\n if (!JAVASCRIPT_EXTENSION.test(filePath)) {\n return (options?.default ? jiti.import(filePath, { default: true }) : jiti.import(filePath)) as Promise<T>\n }\n\n // Transpiled through jiti instead, so every load re-evaluates the file as it is on disk now.\n // A TypeScript file it imports reloads too. A plain JavaScript file it imports still goes\n // through Node's `import()` and stays cached until the process restarts.\n const mod = (await jiti.evalModule(await readFile(filePath, 'utf8'), { filename: filePath, async: true, forceTranspile: true })) as\n | { default?: unknown }\n | undefined\n return (options?.default ? (mod?.default ?? mod) : mod) as T\n}\n","import { existsSync } from 'node:fs'\nimport { basename, dirname, join, resolve } from 'node:path'\nimport process from 'node:process'\nimport { CONFIG_EXTENSIONS } from './constants.ts'\nimport { loadModule } from './loader.ts'\n\n// A config in one of these folders belongs to the folder above it.\nconst CONFIG_FOLDERS = ['.config', 'configs']\nconst SEARCH_FILES = ['', ...CONFIG_FOLDERS.map((folder) => `${folder}/`)].flatMap((prefix) => [`${prefix}.kubbrc`, `${prefix}kubb.config`])\n\n/**\n * Every config file path the Kubb CLI checks in `dir`, in the order it checks them.\n */\nexport function configFileCandidates(dir: string): Array<string> {\n return SEARCH_FILES.flatMap((file) => CONFIG_EXTENSIONS.map((extension) => join(dir, `${file}${extension}`)))\n}\n\n/**\n * The first Kubb config file in `cwd` or the nearest parent folder that has one: `.kubbrc` before\n * `kubb.config`, the folder itself before its `.config/` and `configs/`, and {@link CONFIG_EXTENSIONS} in order.\n */\nexport function findConfigFile(cwd: string = process.cwd()): string | undefined {\n for (let dir = resolve(cwd); ; dir = dirname(dir)) {\n const found = configFileCandidates(dir).find((file) => existsSync(file))\n if (found || dirname(dir) === dir) return found\n }\n}\n\n/**\n * Loads a Kubb config module, calls it with `cli` when it exports a function, and returns its\n * configs as a list, so a single config and a `defineConfig([...])` array read the same. Each\n * `root` resolves from the folder the config belongs to: the file's folder, or the folder above\n * `.config/` and `configs/`. A missing `root` becomes that folder, so the CLI, the bundler, and\n * an editor read the same paths wherever they run.\n */\nexport async function loadConfigFile<T extends { root?: string }>({ file, cli }: { file: string; cli: unknown }): Promise<Array<T>> {\n const loaded = await loadModule<unknown>(file, { default: true })\n const resolved = (await (typeof loaded === 'function' ? loaded(cli) : loaded)) as T | Array<T>\n const dir = CONFIG_FOLDERS.includes(basename(dirname(file))) ? dirname(dirname(file)) : dirname(file)\n return (Array.isArray(resolved) ? resolved : [resolved]).map((config) => ({ ...config, root: resolve(dir, config.root ?? '.') }))\n}\n","import path from 'node:path'\nimport { adapterOas } from '@kubb/adapter-oas'\nimport {\n cliReporter,\n type Config,\n createCliReporter,\n createKubb,\n Diagnostics,\n fileReporter,\n Hookable,\n htmlReporter,\n jsonReporter,\n type KubbHooks,\n logLevel,\n type ProblemDiagnostic,\n type UserConfig,\n} from '@kubb/core'\nimport { parserMd } from '@kubb/parser-md'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\nimport { pluginBarrel, pluginBarrelName } from '@kubb/plugin-barrel'\nimport { findConfigFile, loadConfigFile } from '@internals/shared'\nimport ts from '@typescript/typescript6'\nimport type { UnpluginFactory } from 'unplugin'\nimport { generateModule, selectConfig } from './generateModule.ts'\n\nexport type Options = {\n /**\n * Kubb config passed to the unplugin entry point. When not set, the plugin loads\n * `kubb.config.*` the way the Kubb CLI finds it, from the current working directory up.\n */\n config?: UserConfig\n}\n\n/**\n * Applies the defaults `defineConfig` and the unplugin share to a single user config when not set.\n *\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs(), parserTsx(), parserMd()]`\n * - `reporters` defaults to `[cliReporter, jsonReporter, fileReporter, htmlReporter]`\n * - `plugins` gets `pluginBarrel()` appended when none is already present\n * - `output.barrel` defaults to `false` when not set (`pluginBarrel` is always present after the step above, but generates nothing until configured)\n * - `output.format` and `output.lint` default to `false`\n */\nexport function applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n const plugins = config.plugins ?? []\n\n return {\n ...config,\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs(), parserTsx(), parserMd()],\n reporters: config.reporters?.length ? config.reporters : [cliReporter, jsonReporter, fileReporter, htmlReporter],\n plugins: plugins.some((plugin) => plugin.name === pluginBarrelName) ? plugins : [...plugins, pluginBarrel()],\n output: { barrel: false, format: false, lint: false, ...config.output },\n }\n}\n\n/** Spec files an import loads as a module, the extensions `openapi-import` registers too. */\nconst specFilePattern = /\\.openapi\\.ya?ml$/\n\n/**\n * Builds the Kubb unplugin for every unplugin bundler. `unplugin-kubb` runs the Kubb build once for\n * each config with an `input` (only during `build` under Vite), and `unplugin-kubb:import` loads an\n * imported {@link specFilePattern} file as one module. Without `config`, it loads `kubb.config.*`.\n */\nexport const unpluginFactory: UnpluginFactory<Options | undefined, true> = (options, meta) => {\n const name = 'unplugin-kubb' as const\n const hooks = new Hookable<KubbHooks>()\n const reporter = createCliReporter()\n\n hooks.hook('kubb:generation:end', ({ config, diagnostics = [], filesCreated = 0, status = 'success', hrStart = process.hrtime() }) =>\n reporter.report({ config, diagnostics, filesCreated, status, hrStart }, { logLevel: logLevel.info }),\n )\n\n // Throw: webpack, Rspack, esbuild, and Farm contexts have no `this.error`. Keep the problem's code,\n // help and location, so the bundler shows the same diagnostic.\n const fail = (problem: ProblemDiagnostic | undefined, message: string) =>\n new Diagnostics.Error({ ...(problem ?? { code: Diagnostics.code.unknown, severity: 'error' }), message: `[${name}] ${message}` })\n\n async function readConfigs(): Promise<Array<UserConfig>> {\n if (options?.config) return [options.config]\n\n const file = findConfigFile()\n if (!file) {\n throw new Diagnostics.Error({\n code: Diagnostics.code.inputRequired,\n severity: 'error',\n message: `[${name}] Config is not set`,\n help: 'Pass your Kubb config as the `config` option of the plugin, or add a `kubb.config.ts`.',\n location: { kind: 'config' },\n })\n }\n return loadConfigFile<UserConfig>({ file, cli: {} })\n }\n\n // Loaded once per instance, like the generation below: restart the build after editing the config file.\n let loaded: ReturnType<typeof readConfigs> | undefined\n const loadConfigs = () => (loaded ??= readConfigs())\n\n async function runBuild() {\n for (const config of (await loadConfigs()).filter((config) => config.input)) {\n const result = await createKubb(applyDefaults(config) as Config, { hooks }).generate()\n if (result.success) continue\n\n const failedCount = Diagnostics.failedPlugins(result.diagnostics).length\n const firstError = result.diagnostics.filter(Diagnostics.isProblem).find((diagnostic) => diagnostic.severity === 'error')\n throw fail(firstError, failedCount > 0 ? `Build Error with ${failedCount} failed plugins` : (firstError?.message ?? 'Build failed'))\n }\n }\n\n // A rerun with `output.clean` loops webpack and Rspack watchers, so generate once per instance.\n let generation: Promise<void> | undefined\n\n return [\n {\n name,\n enforce: 'pre',\n apply: meta.framework === 'vite' ? 'build' : undefined,\n async buildStart() {\n generation ??= runBuild().catch((error: unknown) => {\n generation = undefined\n throw error\n })\n await generation\n },\n },\n {\n name: `${name}:import`,\n enforce: 'pre',\n loadInclude: (id) => specFilePattern.test(id),\n async load(id) {\n try {\n const config = selectConfig({ configs: await loadConfigs(), fileName: id, cwd: process.cwd() })\n if (!config) {\n throw new Diagnostics.Error({\n code: Diagnostics.code.unknown,\n severity: 'error',\n message: `[${name}] No Kubb config applies to ${id}`,\n help: 'Set `input` of a config to this spec, or add a config without `input` to serve every imported spec.',\n location: { kind: 'config' },\n })\n }\n const { text, errors } = await generateModule({ root: path.dirname(id), input: id, config })\n if (errors.length) throw fail(errors[0], `${id}: ${errors[0]!.message}`)\n\n // Bundlers do not strip types from a `.yaml` module, so hand them JavaScript.\n return ts.transpileModule(text, { fileName: `${id}.ts`, compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ESNext } })\n .outputText\n } catch (error) {\n // unplugin's webpack and Rspack loader never settles when `load` throws, so report the error instead.\n if (meta.framework !== 'webpack' && meta.framework !== 'rspack') throw error\n this.error(error as Error)\n return 'export {}'\n }\n },\n },\n ]\n}\n"],"mappings":"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"}
|
|
@@ -3,14 +3,14 @@ import { UnpluginFactory } from "unplugin";
|
|
|
3
3
|
//#region src/unpluginFactory.d.ts
|
|
4
4
|
export type Options = {
|
|
5
5
|
/**
|
|
6
|
-
* Kubb config passed to the unplugin entry point.
|
|
6
|
+
* Kubb config passed to the unplugin entry point. When not set, the plugin loads
|
|
7
|
+
* `kubb.config.*` the way the Kubb CLI finds it, from the current working directory up.
|
|
7
8
|
*/
|
|
8
9
|
config?: UserConfig;
|
|
9
10
|
};
|
|
10
11
|
/**
|
|
11
12
|
* Applies the defaults `defineConfig` and the unplugin share to a single user config when not set.
|
|
12
13
|
*
|
|
13
|
-
* - `root` defaults to `process.cwd()`
|
|
14
14
|
* - `adapter` defaults to `adapterOas()`
|
|
15
15
|
* - `parsers` defaults to `[parserTs(), parserTsx(), parserMd()]`
|
|
16
16
|
* - `reporters` defaults to `[cliReporter, jsonReporter, fileReporter, htmlReporter]`
|
|
@@ -20,12 +20,10 @@ export type Options = {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare function applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput>;
|
|
22
22
|
/**
|
|
23
|
-
* Builds the Kubb unplugin for
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* config and prints the same summary `kubb generate` does. Under Vite it only applies during
|
|
27
|
-
* `build`. A missing config or a failed generation fails the host build.
|
|
23
|
+
* Builds the Kubb unplugin for every unplugin bundler. `unplugin-kubb` runs the Kubb build once for
|
|
24
|
+
* each config with an `input` (only during `build` under Vite), and `unplugin-kubb:import` loads an
|
|
25
|
+
* imported {@link specFilePattern} file as one module. Without `config`, it loads `kubb.config.*`.
|
|
28
26
|
*/
|
|
29
|
-
export declare const unpluginFactory: UnpluginFactory<Options | undefined>;
|
|
27
|
+
export declare const unpluginFactory: UnpluginFactory<Options | undefined, true>;
|
|
30
28
|
//#endregion
|
|
31
29
|
//# sourceMappingURL=unpluginFactory.d.ts.map
|
package/dist/unpluginFactory.js
CHANGED
|
@@ -1,102 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { adapterOas } from "@kubb/adapter-oas";
|
|
3
|
-
import { Diagnostics, Hookable, cliReporter, createCliReporter, createKubb, fileReporter, htmlReporter, jsonReporter, logLevel } from "@kubb/core";
|
|
4
|
-
import { parserMd } from "@kubb/parser-md";
|
|
5
|
-
import { parserTs, parserTsx } from "@kubb/parser-ts";
|
|
6
|
-
import { pluginBarrel, pluginBarrelName } from "@kubb/plugin-barrel";
|
|
7
|
-
//#region src/unpluginFactory.ts
|
|
8
|
-
/**
|
|
9
|
-
* Applies the defaults `defineConfig` and the unplugin share to a single user config when not set.
|
|
10
|
-
*
|
|
11
|
-
* - `root` defaults to `process.cwd()`
|
|
12
|
-
* - `adapter` defaults to `adapterOas()`
|
|
13
|
-
* - `parsers` defaults to `[parserTs(), parserTsx(), parserMd()]`
|
|
14
|
-
* - `reporters` defaults to `[cliReporter, jsonReporter, fileReporter, htmlReporter]`
|
|
15
|
-
* - `plugins` gets `pluginBarrel()` appended when none is already present
|
|
16
|
-
* - `output.barrel` defaults to `false` when not set (`pluginBarrel` is always present after the step above, but generates nothing until configured)
|
|
17
|
-
* - `output.format` and `output.lint` default to `false`
|
|
18
|
-
*/
|
|
19
|
-
function applyDefaults(config) {
|
|
20
|
-
const plugins = config.plugins ?? [];
|
|
21
|
-
return {
|
|
22
|
-
...config,
|
|
23
|
-
root: config.root || process.cwd(),
|
|
24
|
-
adapter: config.adapter ?? adapterOas(),
|
|
25
|
-
parsers: config.parsers?.length ? config.parsers : [
|
|
26
|
-
parserTs(),
|
|
27
|
-
parserTsx(),
|
|
28
|
-
parserMd()
|
|
29
|
-
],
|
|
30
|
-
reporters: config.reporters?.length ? config.reporters : [
|
|
31
|
-
cliReporter,
|
|
32
|
-
jsonReporter,
|
|
33
|
-
fileReporter,
|
|
34
|
-
htmlReporter
|
|
35
|
-
],
|
|
36
|
-
plugins: plugins.some((plugin) => plugin.name === pluginBarrelName) ? plugins : [...plugins, pluginBarrel()],
|
|
37
|
-
output: {
|
|
38
|
-
barrel: false,
|
|
39
|
-
format: false,
|
|
40
|
-
lint: false,
|
|
41
|
-
...config.output
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Builds the Kubb unplugin for any unplugin-supported bundler (Vite, Rollup, Webpack, esbuild).
|
|
47
|
-
*
|
|
48
|
-
* Runs the Kubb build during the first `buildStart` with {@link applyDefaults} applied to the
|
|
49
|
-
* config and prints the same summary `kubb generate` does. Under Vite it only applies during
|
|
50
|
-
* `build`. A missing config or a failed generation fails the host build.
|
|
51
|
-
*/
|
|
52
|
-
const unpluginFactory = (options, meta) => {
|
|
53
|
-
const name = "unplugin-kubb";
|
|
54
|
-
const hooks = new Hookable();
|
|
55
|
-
const reporter = createCliReporter();
|
|
56
|
-
hooks.hook("kubb:generation:end", ({ config, diagnostics = [], filesCreated = 0, status = "success", hrStart = process.hrtime() }) => reporter.report({
|
|
57
|
-
config,
|
|
58
|
-
diagnostics,
|
|
59
|
-
filesCreated,
|
|
60
|
-
status,
|
|
61
|
-
hrStart
|
|
62
|
-
}, { logLevel: logLevel.info }));
|
|
63
|
-
async function runBuild() {
|
|
64
|
-
if (!options?.config) throw new Diagnostics.Error({
|
|
65
|
-
code: Diagnostics.code.inputRequired,
|
|
66
|
-
severity: "error",
|
|
67
|
-
message: `[${name}] Config is not set`,
|
|
68
|
-
help: "Pass your Kubb config as the `config` option of the plugin.",
|
|
69
|
-
location: { kind: "config" }
|
|
70
|
-
});
|
|
71
|
-
const result = await createKubb(applyDefaults(options.config), { hooks }).generate();
|
|
72
|
-
if (!result.success) {
|
|
73
|
-
const failedCount = Diagnostics.failedPlugins(result.diagnostics).length;
|
|
74
|
-
const firstError = result.diagnostics.filter(Diagnostics.isProblem).find((diagnostic) => diagnostic.severity === "error");
|
|
75
|
-
const message = failedCount > 0 ? `Build Error with ${failedCount} failed plugins` : firstError?.message ?? "Build failed";
|
|
76
|
-
throw new Diagnostics.Error({
|
|
77
|
-
...firstError ?? {
|
|
78
|
-
code: Diagnostics.code.unknown,
|
|
79
|
-
severity: "error"
|
|
80
|
-
},
|
|
81
|
-
message: `[${name}] ${message}`
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
let generation;
|
|
86
|
-
return {
|
|
87
|
-
name,
|
|
88
|
-
enforce: "pre",
|
|
89
|
-
apply: meta.framework === "vite" ? "build" : void 0,
|
|
90
|
-
async buildStart() {
|
|
91
|
-
generation ??= runBuild().catch((error) => {
|
|
92
|
-
generation = void 0;
|
|
93
|
-
throw error;
|
|
94
|
-
});
|
|
95
|
-
await generation;
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
//#endregion
|
|
100
|
-
export { applyDefaults, unpluginFactory };
|
|
101
|
-
|
|
102
|
-
//# sourceMappingURL=unpluginFactory.js.map
|
|
1
|
+
export { t as applyDefaults, n as unpluginFactory } from "./unpluginFactory-CLbstatM.js";
|
package/dist/vite.js
CHANGED
package/dist/webpack.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unplugin-kubb",
|
|
3
|
-
"version": "5.12.
|
|
3
|
+
"version": "5.12.7",
|
|
4
4
|
"description": "Integration of Kubb for Vite, webpack, Rollup, Rolldown, Rspack, esbuild, Farm, Nuxt, and Astro.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -105,22 +105,25 @@
|
|
|
105
105
|
"registry": "https://registry.npmjs.org/"
|
|
106
106
|
},
|
|
107
107
|
"dependencies": {
|
|
108
|
-
"@kubb/adapter-oas": "5.12.
|
|
109
|
-
"@kubb/core": "5.12.
|
|
110
|
-
"@kubb/parser-md": "5.12.
|
|
111
|
-
"@kubb/parser-ts": "5.12.
|
|
112
|
-
"@kubb/plugin-barrel": "5.12.
|
|
108
|
+
"@kubb/adapter-oas": "5.12.7",
|
|
109
|
+
"@kubb/core": "5.12.7",
|
|
110
|
+
"@kubb/parser-md": "5.12.7",
|
|
111
|
+
"@kubb/parser-ts": "5.12.7",
|
|
112
|
+
"@kubb/plugin-barrel": "5.12.7",
|
|
113
|
+
"@typescript/typescript6": "^6.0.2",
|
|
114
|
+
"jiti": "^2.7.0",
|
|
113
115
|
"unplugin": "^3.4.0"
|
|
114
116
|
},
|
|
115
117
|
"devDependencies": {
|
|
116
118
|
"@farmfe/core": "^1.7.11",
|
|
117
|
-
"@
|
|
119
|
+
"@internals/shared": "0.0.1",
|
|
120
|
+
"@kubb/ast": "5.12.7",
|
|
118
121
|
"@nuxt/kit": "^4.6.2",
|
|
119
122
|
"@nuxt/schema": "^4.6.2",
|
|
120
123
|
"@rspack/core": "^2.2.9",
|
|
121
124
|
"esbuild": "^0.28.2",
|
|
122
125
|
"rolldown": "^1.2.14",
|
|
123
|
-
"rollup": "^4.64.
|
|
126
|
+
"rollup": "^4.64.12",
|
|
124
127
|
"vite": "^8.3.4",
|
|
125
128
|
"webpack": "^5.112.0"
|
|
126
129
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unpluginFactory.js","names":[],"sources":["../src/unpluginFactory.ts"],"sourcesContent":["import { adapterOas } from '@kubb/adapter-oas'\nimport {\n cliReporter,\n type Config,\n createCliReporter,\n createKubb,\n Diagnostics,\n fileReporter,\n Hookable,\n htmlReporter,\n jsonReporter,\n type KubbHooks,\n logLevel,\n type UserConfig,\n} from '@kubb/core'\nimport { parserMd } from '@kubb/parser-md'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\nimport { pluginBarrel, pluginBarrelName } from '@kubb/plugin-barrel'\nimport type { UnpluginFactory } from 'unplugin'\n\nexport type Options = {\n /**\n * Kubb config passed to the unplugin entry point.\n */\n config?: UserConfig\n}\n\n/**\n * Applies the defaults `defineConfig` and the unplugin share to a single user config when not set.\n *\n * - `root` defaults to `process.cwd()`\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs(), parserTsx(), parserMd()]`\n * - `reporters` defaults to `[cliReporter, jsonReporter, fileReporter, htmlReporter]`\n * - `plugins` gets `pluginBarrel()` appended when none is already present\n * - `output.barrel` defaults to `false` when not set (`pluginBarrel` is always present after the step above, but generates nothing until configured)\n * - `output.format` and `output.lint` default to `false`\n */\nexport function applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n const plugins = config.plugins ?? []\n\n return {\n ...config,\n root: config.root || process.cwd(),\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs(), parserTsx(), parserMd()],\n reporters: config.reporters?.length ? config.reporters : [cliReporter, jsonReporter, fileReporter, htmlReporter],\n plugins: plugins.some((plugin) => plugin.name === pluginBarrelName) ? plugins : [...plugins, pluginBarrel()],\n output: { barrel: false, format: false, lint: false, ...config.output },\n }\n}\n\n/**\n * Builds the Kubb unplugin for any unplugin-supported bundler (Vite, Rollup, Webpack, esbuild).\n *\n * Runs the Kubb build during the first `buildStart` with {@link applyDefaults} applied to the\n * config and prints the same summary `kubb generate` does. Under Vite it only applies during\n * `build`. A missing config or a failed generation fails the host build.\n */\nexport const unpluginFactory: UnpluginFactory<Options | undefined> = (options, meta) => {\n const name = 'unplugin-kubb' as const\n const hooks = new Hookable<KubbHooks>()\n const reporter = createCliReporter()\n\n hooks.hook('kubb:generation:end', ({ config, diagnostics = [], filesCreated = 0, status = 'success', hrStart = process.hrtime() }) =>\n reporter.report({ config, diagnostics, filesCreated, status, hrStart }, { logLevel: logLevel.info }),\n )\n\n // Throw: webpack, Rspack, esbuild, and Farm contexts have no `this.error`.\n async function runBuild() {\n if (!options?.config) {\n throw new Diagnostics.Error({\n code: Diagnostics.code.inputRequired,\n severity: 'error',\n message: `[${name}] Config is not set`,\n help: 'Pass your Kubb config as the `config` option of the plugin.',\n location: { kind: 'config' },\n })\n }\n\n const kubb = createKubb(applyDefaults(options.config) as Config, { hooks })\n const result = await kubb.generate()\n\n if (!result.success) {\n const failedCount = Diagnostics.failedPlugins(result.diagnostics).length\n const firstError = result.diagnostics.filter(Diagnostics.isProblem).find((diagnostic) => diagnostic.severity === 'error')\n const message = failedCount > 0 ? `Build Error with ${failedCount} failed plugins` : (firstError?.message ?? 'Build failed')\n\n // Keep the first error's code, help, and location, so the bundler shows the same diagnostic.\n throw new Diagnostics.Error({\n ...(firstError ?? { code: Diagnostics.code.unknown, severity: 'error' }),\n message: `[${name}] ${message}`,\n })\n }\n }\n\n // A rerun with `output.clean` loops webpack and Rspack watchers, so generate once per instance.\n let generation: Promise<void> | undefined\n\n return {\n name,\n enforce: 'pre',\n apply: meta.framework === 'vite' ? 'build' : undefined,\n async buildStart() {\n generation ??= runBuild().catch((error: unknown) => {\n generation = undefined\n throw error\n })\n await generation\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsCA,SAAgB,cAAsB,QAAgD;CACpF,MAAM,UAAU,OAAO,WAAW,CAAC;CAEnC,OAAO;EACL,GAAG;EACH,MAAM,OAAO,QAAQ,QAAQ,IAAI;EACjC,SAAS,OAAO,WAAW,WAAW;EACtC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU;GAAC,SAAS;GAAG,UAAU;GAAG,SAAS;EAAC;EACvF,WAAW,OAAO,WAAW,SAAS,OAAO,YAAY;GAAC;GAAa;GAAc;GAAc;EAAY;EAC/G,SAAS,QAAQ,MAAM,WAAW,OAAO,SAAS,gBAAgB,IAAI,UAAU,CAAC,GAAG,SAAS,aAAa,CAAC;EAC3G,QAAQ;GAAE,QAAQ;GAAO,QAAQ;GAAO,MAAM;GAAO,GAAG,OAAO;EAAO;CACxE;AACF;;;;;;;;AASA,MAAa,mBAAyD,SAAS,SAAS;CACtF,MAAM,OAAO;CACb,MAAM,QAAQ,IAAI,SAAoB;CACtC,MAAM,WAAW,kBAAkB;CAEnC,MAAM,KAAK,wBAAwB,EAAE,QAAQ,cAAc,CAAC,GAAG,eAAe,GAAG,SAAS,WAAW,UAAU,QAAQ,OAAO,QAC5H,SAAS,OAAO;EAAE;EAAQ;EAAa;EAAc;EAAQ;CAAQ,GAAG,EAAE,UAAU,SAAS,KAAK,CAAC,CACrG;CAGA,eAAe,WAAW;EACxB,IAAI,CAAC,SAAS,QACZ,MAAM,IAAI,YAAY,MAAM;GAC1B,MAAM,YAAY,KAAK;GACvB,UAAU;GACV,SAAS,IAAI,KAAK;GAClB,MAAM;GACN,UAAU,EAAE,MAAM,SAAS;EAC7B,CAAC;EAIH,MAAM,SAAS,MADF,WAAW,cAAc,QAAQ,MAAM,GAAa,EAAE,MAAM,CACjD,CAAC,CAAC,SAAS;EAEnC,IAAI,CAAC,OAAO,SAAS;GACnB,MAAM,cAAc,YAAY,cAAc,OAAO,WAAW,CAAC,CAAC;GAClE,MAAM,aAAa,OAAO,YAAY,OAAO,YAAY,SAAS,CAAC,CAAC,MAAM,eAAe,WAAW,aAAa,OAAO;GACxH,MAAM,UAAU,cAAc,IAAI,oBAAoB,YAAY,mBAAoB,YAAY,WAAW;GAG7G,MAAM,IAAI,YAAY,MAAM;IAC1B,GAAI,cAAc;KAAE,MAAM,YAAY,KAAK;KAAS,UAAU;IAAQ;IACtE,SAAS,IAAI,KAAK,IAAI;GACxB,CAAC;EACH;CACF;CAGA,IAAI;CAEJ,OAAO;EACL;EACA,SAAS;EACT,OAAO,KAAK,cAAc,SAAS,UAAU,KAAA;EAC7C,MAAM,aAAa;GACjB,eAAe,SAAS,CAAC,CAAC,OAAO,UAAmB;IAClD,aAAa,KAAA;IACb,MAAM;GACR,CAAC;GACD,MAAM;EACR;CACF;AACF"}
|