Packaging and local development
Publishing a plugin to npm, what a site does to install it, developing a plugin in a site, and the checklist before publishing.
Packaging for npm
A plugin on npm ships compiled JavaScript. The site does not compile packages installed from npm.
Compile file by file with tsc
Use tsc, not a bundler. tsc writes one .js file for each source file, so a file that starts with 'use client' or 'use server' still starts with it after the build. Next.js needs that line first in the file. A bundler merges files, and the lines are lost.
tsconfig.build.json, beside the plugin's tsconfig.json:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"outDir": "dist",
"rootDir": "src",
"declaration": true,
"sourceMap": false
}
}A plugin with tables includes schema and migrations.ts as well, with "rootDir": "." and "include": ["src", "schema", "migrations.ts"]. Its files then land in dist/src/. The form builder and the Instagram feed are built this way (see the form builder's tsconfig.build.json).
package.json
{
"name": "pet-adopt-plugin-hello",
"version": "0.1.0",
"type": "module",
"exports": {
".": "./src/index.tsx",
"./block": "./src/block.tsx"
},
"files": ["dist"],
"publishConfig": {
"access": "public",
"exports": {
".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
"./block": { "types": "./dist/block.d.ts", "default": "./dist/block.js" }
}
},
"scripts": {
"build:package": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
"prepack": "pnpm run build:package"
},
"peerDependencies": {
"@pet-adopt/plugin-sdk": "^0.1.0",
"@pet-adopt/ui": ">=0.1.0",
"react": "^19.2.0",
"react-icons": "^5.7.0"
}
}exportsandpublishConfig.exports: in a workspace,exportspoints at the TypeScript source, so the site compiles it and changes show at once.publishConfig.exportspoints atdist, and pnpm writes it into the package when it packs or publishes. So pack and publish with pnpm. A plugin that is never used from a workspace can pointexportsatdistdirectly.- A block has its own entry point (
./blockabove), because the browser loads it. Keep it out of the main entry's exports. See The two halves. - Clear
distfirst.build:packagedeletesdistbeforetscruns, as the packages in the repository do, so a file removed from the source is not shipped from an older build. - Peer dependencies: the SDK and
@pet-adopt/uimust be peer dependencies, so the plugin uses the site's copies. Adddrizzle-ormwhen the plugin has tables, andnextwhen it imports fromnext/.... List the same packages underdevDependenciesto build against. Inside the repository the SDK peer is writtenworkspace:^, which pnpm turns into^<version>when it packs. @pet-adopt/uiis only needed by a plugin whose screens use it. It is not on npm: it is a workspace package inside every site (packages/ui). So a plugin that uses it must be built inside a workspace that has it, which means a site or the Pet Adopt repository (see Local development). Write its version as a wide range (>=0.1.0), not the current release: each site keeps the copy it started with, so a narrow range would warn on every older site. Also mark it optional ("peerDependenciesMeta": { "@pet-adopt/ui": { "optional": true } }): it is never on npm, so a required peer would warn on every install.- README and LICENSE: npm adds them to the package whatever
filessays. Say in the README how a site installs the plugin (next section). - Check the result:
pnpm pack --pack-destination <folder>writes the tarball without publishing it. Look inside fordist/, for every address inexports, and for'use client'and'use server'on the first line of the files that had them.
What the site adds
A site that installs an npm plugin does up to five things. Your README should list the ones your plugin needs:
-
Install it into the web app, from the site's root folder:
pnpm --filter @pet-adopt/web add <package>. -
List the plugin in
apps/web/pet-adopt.config.ts. -
List its blocks, if it has any, in
apps/web/pet-adopt.blocks.ts, imported from the block entry point. -
Tell Tailwind to read it, if it has admin screens. Tailwind does not look in
node_modulesby itself.packages/ui/src/styles/globals.csslists what it reads, as@sourcelines.-
A package named
@pet-adopt/plugin-*is covered already, by this line:@source "../../../../apps/web/node_modules/@pet-adopt/plugin-*/dist"; -
A plugin with any other name needs a line of its own beside it:
@source "../../../../apps/web/node_modules/<package>/dist";
Without it, the plugin's screens lose every class that nothing else on the site uses.
-
-
Create its tables, if it has any:
pnpm migrateon PostgreSQL or hosted libSQL (pnpm buildruns it too). A local SQLite file needs nothing. See When migrations run.
Anything else the plugin needs belongs in its README as well. The Instagram feed, for example, needs the site to allow Instagram's picture hosts with CSP_IMG_SRC, because the site's content security policy allows pictures only from itself.
Local development
A plugin kept as a folder works the same as one from npm, and needs no build. The site's Next.js compiles any dependency installed with workspace:, link: or file:.
In the repository, or in a site
A site starts as a clone of the Pet Adopt repository, so both have a plugins/ folder that is already in pnpm-workspace.yaml and in the Tailwind @source lines.
-
Make a folder under
plugins/, with apackage.jsonwhoseexportspoints at the source ("./src/index.tsx", plus"./block": "./src/block.tsx"for a block). Copyplugins/adoption-tipsto start. -
Run
pnpm installat the root, so the new package gets its dependencies. -
Add it to the web app, from the root:
pnpm --filter @pet-adopt/web add <package name>@workspace:*Never run
pnpm addat the root. -
List it in
apps/web/pet-adopt.config.ts, and its blocks inapps/web/pet-adopt.blocks.ts. -
Run
pnpm dev.
pnpm typecheck and pnpm lint cover a plugin that has those two scripts ("typecheck": "tsc --noEmit" and "lint": "eslint .", as in the example plugin); copy eslint.config.mjs from the example plugin too.
After changing the plugin's tables, run pnpm plugin:generate plugins/<folder> <change_name>, then restart pnpm dev. A local SQLite file applies the new migration when it opens. See Generating migrations.
Trying a packed plugin
To try a plugin as a site would get it from npm, before publishing it:
pnpm pack --pack-destination <a scratch folder>in the plugin's folder.- In a site,
pnpm --filter @pet-adopt/web add <path to the .tgz>.
A plugin installed from a tarball is compiled JavaScript, so it also needs an @source line for its dist, unless its name starts with @pet-adopt/plugin-.
Checklist before publishing a plugin
- It imports
@pet-adopt/plugin-sdkand, for screens,@pet-adopt/ui. Nothing from the site. - Every server action is wrapped in
host().runand checks a permission first. - Every change is recorded with
host().recordActivity, naming fields and never values. - Errors are logged with
host().logErroronly. - Table and index names start with the plugin's prefix, and both schema files match.
drizzle/andmigrations.tsare committed, and no shipped migration was edited.- Each block lives in a module the browser can load, exported from its own entry point, with a name that starts with something of your own.
- Each loader returns only public data, as plain JSON, and copes with options that name nothing.
- Block CSS uses the site's theme variables, and every selector starts with a class of your own. The block was checked on every preset, in light and dark (see Checking your block).
- The packed tarball holds
dist/, and client and server files keep their first line. - The README tells a site how to install it: the config, the blocks file, the
@sourceline when its name needs one, andpnpm migrate.