@loydjs/vite
v1.1.0
Published
Loyd vite - Vite/Rollup AOT plugin
Maintainers
Readme
Overview
@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.
In development, the plugin is a no-op — JIT compilation runs as normal for fast HMR.
Installation
npm install @loydjs/viteRequires
@loydjs/core·@loydjs/compiler· Vite ≥ 5.0.0 · Node.js ≥ 20 · TypeScript ≥ 5.4
Setup
Vite
// vite.config.ts
import { defineConfig } from "vite";
import { loydPlugin } from "@loydjs/vite";
export default defineConfig({
plugins: [
loydPlugin({
// Schemas to resolve statically at build time.
// Key = variable name as it appears in your source code.
schemas: {
UserSchema,
PostSchema,
CommentSchema,
},
}),
],
});Rollup
// rollup.config.js
import { loydPlugin } from "@loydjs/vite";
export default {
input: "src/index.ts",
plugins: [
loydPlugin({ schemas: { UserSchema } }),
],
};How it works
Your source code, untouched:
import { compile } from "@loydjs/compiler";
import { UserSchema } from "./schemas";
const validate = compile(UserSchema);
const result = validate(req.body);After AOT transform — what ships in your production bundle:
// @loydjs/compiler: AOT-inlined validator for UserSchema
function __loyd_UserSchema__(input) {
"use strict";
let __input__ = input;
const __issues__ = [];
if (typeof __input__ !== "object" || __input__ === null || Array.isArray(__input__)) {
__issues__.push({ code: "ERR_OBJECT_INVALID_TYPE", path: [] });
} else {
const __fname__ = __input__["name"];
if (typeof __fname__ !== "string") {
__issues__.push({ code: "ERR_STRING_INVALID_TYPE", path: ["name"] });
} else {
if (__fname__.length < 2) { __issues__.push({ code: "ERR_STRING_TOO_SHORT", path: ["name"], meta: { min: 2, actual: __fname__.length } }); }
if (__fname__.length > 100) { __issues__.push({ code: "ERR_STRING_TOO_LONG", path: ["name"], meta: { max: 100, actual: __fname__.length } }); }
}
// ...
}
if (__issues__.length > 0) return { success: false, data: undefined, issues: __issues__ };
return { success: true, data: __input__, issues: [] };
}
const validate = __loyd_UserSchema__;
const result = validate(req.body);Plugin options
interface LoydVitePluginOptions {
/**
* Schemas to resolve statically at build time.
* Key = variable name as it appears in source code.
*/
schemas?: Record<string, LoydSchema<unknown>>;
/**
* Enable/disable the plugin entirely.
* @default true
*/
enabled?: boolean;
/**
* Log transformed files.
* @default false
*/
verbose?: boolean;
/**
* Force AOT even in development mode.
* By default, AOT is only active during production builds.
* @default false
*/
forceAot?: boolean;
/**
* Generate sourcemaps for transformed files.
* @default true
*/
sourcemap?: boolean;
}Emit standalone validators
Use the emit() function from @loydjs/compiler to generate standalone .js + .d.ts validator files, independent of the Vite plugin.
import { emit } from "@loydjs/compiler";
import { UserSchema } from "./schemas";
await emit(UserSchema, {
outFile: "./dist/validators/user.js",
exportName: "validateUser",
format: "esm",
dts: true,
});
// dist/validators/user.js — flat inline validator, no imports
// dist/validators/user.d.ts — TypeScript declarationsDependencies
| Package | Role |
|:---|:---|
| @loydjs/core | LoydSchema type |
| @loydjs/compiler | generateCode, optimize for AOT codegen |
Peer dependencies
| Package | Version |
|:---|:---|
| vite | ≥ 5.0.0 |
Documentation
License
MIT © b3nito404
