@loydjs/vite 0.0.0 → 1.1.0

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 ADDED
@@ -0,0 +1,200 @@
1
+ <div align="center">
2
+
3
+ <h1>@loydjs/vite</h1>
4
+
5
+ <p><strong>AOT compilation plugin for Vite and Rollup.</strong><br/>
6
+ Replaces compile() calls with flat inline validators at build time · Zero runtime overhead.</p>
7
+
8
+ [![CI](https://github.com/b3nito404/loyd/actions/workflows/ci.yml/badge.svg)](https://github.com/b3nito404/loyd/actions/workflows/ci.yml)
9
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
10
+ [![Bundle](https://img.shields.io/badge/bundle-~2kb-brightgreen.svg)](https://bundlephobia.com/package/@loydjs/vite)
11
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.4%2B-blue.svg)](https://www.typescriptlang.org)
12
+ [![npm downloads](https://img.shields.io/npm/dm/@loydjs/vite?color=6366f1&label=downloads)](https://www.npmjs.com/package/@loydjs/vite)
13
+
14
+ </div>
15
+
16
+ ---
17
+
18
+ ## Overview
19
+
20
+ `@loydjs/vite` is a Vite and Rollup plugin that performs Ahead-of-Time (AOT) compilation of Loyd schemas. When enabled, it replaces `compile(schema)` calls in your source code with the flat inline validator functions at build time — so your production bundle contains zero compilation overhead and no dependency on `@loydjs/compiler` at runtime.
21
+
22
+ In development, the plugin is a no-op — JIT compilation runs as normal for fast HMR.
23
+
24
+ ---
25
+
26
+ ## Installation
27
+
28
+ ```sh
29
+ npm install @loydjs/vite
30
+ ```
31
+
32
+ > **Requires** `@loydjs/core` · `@loydjs/compiler` · Vite ≥ 5.0.0 · Node.js ≥ 20 · TypeScript ≥ 5.4
33
+
34
+ ---
35
+
36
+ ## Setup
37
+
38
+ ### Vite
39
+
40
+ ```ts
41
+ // vite.config.ts
42
+ import { defineConfig } from "vite";
43
+ import { loydPlugin } from "@loydjs/vite";
44
+
45
+ export default defineConfig({
46
+ plugins: [
47
+ loydPlugin({
48
+ // Schemas to resolve statically at build time.
49
+ // Key = variable name as it appears in your source code.
50
+ schemas: {
51
+ UserSchema,
52
+ PostSchema,
53
+ CommentSchema,
54
+ },
55
+ }),
56
+ ],
57
+ });
58
+ ```
59
+
60
+ ### Rollup
61
+
62
+ ```js
63
+ // rollup.config.js
64
+ import { loydPlugin } from "@loydjs/vite";
65
+
66
+ export default {
67
+ input: "src/index.ts",
68
+ plugins: [
69
+ loydPlugin({ schemas: { UserSchema } }),
70
+ ],
71
+ };
72
+ ```
73
+
74
+ ---
75
+
76
+ ## How it works
77
+
78
+ Your source code, untouched:
79
+
80
+ ```ts
81
+ import { compile } from "@loydjs/compiler";
82
+ import { UserSchema } from "./schemas";
83
+
84
+ const validate = compile(UserSchema);
85
+ const result = validate(req.body);
86
+ ```
87
+
88
+ After AOT transform — what ships in your production bundle:
89
+
90
+ ```js
91
+ // @loydjs/compiler: AOT-inlined validator for UserSchema
92
+ function __loyd_UserSchema__(input) {
93
+ "use strict";
94
+ let __input__ = input;
95
+ const __issues__ = [];
96
+ if (typeof __input__ !== "object" || __input__ === null || Array.isArray(__input__)) {
97
+ __issues__.push({ code: "ERR_OBJECT_INVALID_TYPE", path: [] });
98
+ } else {
99
+ const __fname__ = __input__["name"];
100
+ if (typeof __fname__ !== "string") {
101
+ __issues__.push({ code: "ERR_STRING_INVALID_TYPE", path: ["name"] });
102
+ } else {
103
+ if (__fname__.length < 2) { __issues__.push({ code: "ERR_STRING_TOO_SHORT", path: ["name"], meta: { min: 2, actual: __fname__.length } }); }
104
+ if (__fname__.length > 100) { __issues__.push({ code: "ERR_STRING_TOO_LONG", path: ["name"], meta: { max: 100, actual: __fname__.length } }); }
105
+ }
106
+ // ...
107
+ }
108
+ if (__issues__.length > 0) return { success: false, data: undefined, issues: __issues__ };
109
+ return { success: true, data: __input__, issues: [] };
110
+ }
111
+ const validate = __loyd_UserSchema__;
112
+ const result = validate(req.body);
113
+ ```
114
+
115
+ ---
116
+
117
+ ## Plugin options
118
+
119
+ ```ts
120
+ interface LoydVitePluginOptions {
121
+ /**
122
+ * Schemas to resolve statically at build time.
123
+ * Key = variable name as it appears in source code.
124
+ */
125
+ schemas?: Record<string, LoydSchema<unknown>>;
126
+
127
+ /**
128
+ * Enable/disable the plugin entirely.
129
+ * @default true
130
+ */
131
+ enabled?: boolean;
132
+
133
+ /**
134
+ * Log transformed files.
135
+ * @default false
136
+ */
137
+ verbose?: boolean;
138
+
139
+ /**
140
+ * Force AOT even in development mode.
141
+ * By default, AOT is only active during production builds.
142
+ * @default false
143
+ */
144
+ forceAot?: boolean;
145
+
146
+ /**
147
+ * Generate sourcemaps for transformed files.
148
+ * @default true
149
+ */
150
+ sourcemap?: boolean;
151
+ }
152
+ ```
153
+
154
+ ---
155
+
156
+ ## Emit standalone validators
157
+
158
+ Use the `emit()` function from `@loydjs/compiler` to generate standalone `.js` + `.d.ts` validator files, independent of the Vite plugin.
159
+
160
+ ```ts
161
+ import { emit } from "@loydjs/compiler";
162
+ import { UserSchema } from "./schemas";
163
+
164
+ await emit(UserSchema, {
165
+ outFile: "./dist/validators/user.js",
166
+ exportName: "validateUser",
167
+ format: "esm",
168
+ dts: true,
169
+ });
170
+
171
+ // dist/validators/user.js — flat inline validator, no imports
172
+ // dist/validators/user.d.ts — TypeScript declarations
173
+ ```
174
+
175
+ ---
176
+
177
+ ## Dependencies
178
+
179
+ | Package | Role |
180
+ |:---|:---|
181
+ | `@loydjs/core` | `LoydSchema` type |
182
+ | `@loydjs/compiler` | `generateCode`, `optimize` for AOT codegen |
183
+
184
+ ## Peer dependencies
185
+
186
+ | Package | Version |
187
+ |:---|:---|
188
+ | `vite` | ≥ 5.0.0 |
189
+
190
+ ---
191
+
192
+ ## Documentation
193
+
194
+ **[loyddev-psi.vercel.app](https://loyddev-psi.vercel.app)**
195
+
196
+ ---
197
+
198
+ ## License
199
+
200
+ MIT © [b3nito404](https://github.com/b3nito404)