definePackage
Mark luna types as coming from an npm package, and the luna field of package.json that makes a deploy carry the package.
definePackage(name, lunas) marks luna types as coming from a package, so an app that installs the package runs them and its bill shows their usage on a line of their own.
import { defineLuna, definePackage, t, table } from "luna";
export const { Session } = definePackage("@acme/luna-sessions", {
Session: defineLuna({
name: "Session",
key: { id: t.string() },
schema: { values: table({ k: t.string(), v: t.string() }, { primaryKey: ["k"] }) },
handlers: (h) => ({
put: h({ args: { k: t.string(), v: t.string() } }, (ctx, { k, v }) => {
ctx.db.values.upsert({ k, v });
}),
}),
}),
});
Reference
definePackage(name, lunas)
import { definePackage } from "luna";
namestring
The package's name, 1 to 128 letters, digits and the characters @ / . _ -, such as its npm name.
stringlunasRecord<string, Luna>
The package's luna types, by the name it exports them under.
Record<string, Luna>Returns lunas as given, with each luna type and the types it adds marked with name. A name that breaks the rule throws.
The luna field of package.json
lunaany JSON value
Marks the package as holding luna types. Its value is not read, so {} is enough.
any JSON value{
"name": "@acme/luna-sessions",
"version": "1.0.0",
"type": "module",
"main": "dist/index.js",
"luna": {}
}
Good to know
luna deploycarries every installed package whosepackage.jsonhas alunafield, with its production dependencies, whether your app imports it or not.- Only the luna types your app’s lunas module exports are deployed. Re-export the package’s types there.
- Code from a package runs with your app, as trusted as your own code.
- From
node_modules, a deploy leaves out.ts,.mts,.cts,.mapand.mdfiles. Publish the package as JavaScript. - A package with a native
.nodeaddon fails the deploy, unless it is an optional dependency, which is left out. - The
lunapackage is never deployed fromnode_modules. Luna provides it. - pnpm’s linked layout cannot be deployed. Set
node-linker=hoistedin.npmrcand install again.
Examples
Use a package’s lunas in your app
After npm install @acme/luna-sessions, export its types beside your own.
export { Session } from "@acme/luna-sessions";
export { List } from "./list.ts";