@varavel/vdl-plugin-sdk
v0.5.10
Published
SDK to build plugins for VDL using TypeScript
Downloads
235
Maintainers
Readme
What You Get
- A typed plugin authoring API (
definePlugin) forsrc/index.ts. - Built-in error primitives (
PluginError,fail,assert) for clear diagnostics. - Utility subpath imports (
utils/*) that tree-shake cleanly. - A dedicated testing entry point with IR builders.
- A small CLI (
vdl-plugin) forcheckandbuild. - Shared TypeScript presets for plugin and test projects.
If you want the full API surface while reading, see vdl-plugin-sdk.varavel.com.
Install
Most projects should start from the official template:
vdl-plugin-template
If you are setting up from scratch:
npm install --save-dev --save-exact @varavel/vdl-plugin-sdk@latestQuick Start
Create src/index.ts:
import { definePlugin } from "@varavel/vdl-plugin-sdk";
export const generate = definePlugin((input) => {
// Useful input fields:
console.log(input.version); // VDL version (without the "v" prefix)
console.log(input.options); // Plugin options from vdl.config.vdl
console.log(input.ir); // Typed VDL intermediate representation
return {
files: [
{
path: "hello.txt",
content: "Hello from VDL Plugin SDK",
},
],
};
});Run:
npx vdl-plugin check
npx vdl-plugin buildcheck: type-checks plugin code.build: bundlessrc/index.tsintodist/index.js.
Entry Points
| Import | Use for |
| ------------------------------------------ | ------------------------------------------------------------------------------ |
| @varavel/vdl-plugin-sdk | Runtime plugin authoring (definePlugin), typed input, generated files output |
| @varavel/vdl-plugin-sdk/utils/<category> | Tree-shakeable helper imports by category |
| @varavel/vdl-plugin-sdk/testing | Test-only builders for realistic plugin input and IR fixtures |
@varavel/vdl-plugin-sdk
Use this in runtime plugin code (usually src/index.ts).
@varavel/vdl-plugin-sdk/utils/<category>
Use this for reusable helpers in plugin logic:
import { words, pascalCase } from "@varavel/vdl-plugin-sdk/utils/strings";
import { chunk } from "@varavel/vdl-plugin-sdk/utils/arrays";@varavel/vdl-plugin-sdk/testing
Use this only in tests to build IR and plugin input fixtures without manually writing large object graphs.
CLI
Use via npx or package scripts:
npx vdl-plugin check
npx vdl-plugin build
# Or with custom entry/out paths:
npx vdl-plugin build --entry packages/my-plugin/src/main.ts --out ../../dist/index.jscheckruns TypeScript with no emit.buildproduces the release artifact atdist/index.jsfromsrc/index.tsby default. You can override these defaults using the--entry <path>and--out <path>options.- Note: Remember that the final built plugin must always be located at
dist/index.jsrelative to your project's root directory so that VDL can automatically discover and load it.
- Note: Remember that the final built plugin must always be located at
Importing Raw Files
The SDK's builder includes out-of-the-box support for importing files as raw plaintext strings. This is extremely useful for bundling templates, HTML, or SQL queries directly into your plugin without needing the fs module at runtime.
Simply append ?raw to any relative import path:
import { definePlugin } from "@varavel/vdl-plugin-sdk";
import query from "./query.sql?raw";
export const generate = definePlugin((input) => {
return {
files: [
{
path: "output.sql",
content: query,
},
],
};
});Error Handling
definePlugin wraps your handler in a safe global boundary.
- Throw
PluginError(or usefail) when you want to report a structured diagnostic. - Use
assertto fail fast and keep TypeScript narrowing for validated values. - Any unexpected thrown value is converted into a safe
errorsresponse.
import { assert, definePlugin, fail } from "@varavel/vdl-plugin-sdk";
export const generate = definePlugin((input) => {
const serviceType = input.ir.types.find(
(typeDef) => typeDef.name === "Service",
);
assert(serviceType, 'Missing required type "Service".', input.ir.position);
if (serviceType.type.kind !== "object") {
fail('Type "Service" must be an object.', serviceType.position);
}
return {
files: [{ path: "service.txt", content: "ok" }],
};
});For RPC plugins, prefer a single fail-fast validation call before generation:
import { definePlugin } from "@varavel/vdl-plugin-sdk";
import { assertValidIrForRpc } from "@varavel/vdl-plugin-sdk/utils/rpc";
export const generate = definePlugin((input) => {
assertValidIrForRpc(input.ir);
return {
files: [{ path: "rpc.ts", content: "// generated" }],
};
});Typical Plugin Workflow
- Implement your plugin in
src/index.tswith@varavel/vdl-plugin-sdk. - Use helpers from
@varavel/vdl-plugin-sdk/utils/<category>as needed. - Add unit tests using
@varavel/vdl-plugin-sdk/testing. - Run
vdl-plugin checkduring development to ensure type safety. - Run
vdl-plugin buildto producedist/index.js. - Commit
dist/index.jsand publish a new release tag.
VDL consumes the built dist/index.js artifact, not your TypeScript source.
Example package.json scripts:
{
"scripts": {
"check": "vdl-plugin check",
"build": "vdl-plugin build"
}
}TypeScript Setup
The SDK uses a composite TypeScript setup to strictly separate your production code from your test environment. You will need three configuration files in your project root:
tsconfig.json(The Router) This file tells your editor and thevdl-plugin checkwhere to find the two separate configurations for production and tests.
{
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.test.json" }
]
}tsconfig.app.json(For Production Code) This configuration extends the SDK's base app config and defines which files are part of your plugin, excluding tests.
{
"extends": "@varavel/vdl-plugin-sdk/tsconfig.app.base.json",
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts", "src/**/*.spec.ts"]
}tsconfig.test.json(For Tests) This configuration extends the SDK's test base config and includes everything your production code ignores. It ensures that test-specific types do not leak into your main plugin compilation.
{
"extends": "@varavel/vdl-plugin-sdk/tsconfig.test.base.json",
"include": [
"src/**/*.test.ts",
"src/**/*.spec.ts",
"tests/**/*.ts",
"e2e/**/*.ts",
"vitest.config.ts"
]
}Testing
The testing entry point exposes independent builders, so each test imports only what it needs.
import {
field,
objectType,
pluginInput,
primitiveType,
schema,
typeDef,
} from "@varavel/vdl-plugin-sdk/testing";
const input = pluginInput({
options: { prefix: "Api" },
ir: schema({
types: [
typeDef("User", objectType([field("id", primitiveType("string"))])),
],
}),
});Pass input to your plugin handler in unit tests and assert generated files or errors.
To add tests, install Vitest:
npm install --save-dev vitestLicense
This project is released under the MIT License. See the LICENSE.
