Enhanced Sanity.io plugin development experience.

Overview

@sanity/plugin-kit

NOTE

This is a developer preview package meant for Sanity Studio v3 plugin development.

For a v2 alternative, consider using Sanipack.

What is it?

@sanity/plugin-kit is an opinionated, enhanced Sanity.io v3 plugin development experience.

It provides a set of CLI commands for initializing, verifying and testing a Sanity plugin for Sanity Studio v3. The verify-package command can be used when upgrading V2 plugins to Studio V3 versions.

@sanity/plugin-kit also comes with a verify-studio command that can be used to recommend upgrade steps in existing Sanity Studio v2 studio.

This package assumes and recommends Parcel for building, and Yalc with watch for testing the plugin in Sanity Studio. Check the FAQ fro more on these.

Table of contents

Installation

npm install --save-dev @sanity/plugin-kit

Install build tool

@sanity/plugin-kit assumes the plugin will use Parcel for build and watch:

npm install --save-dev parcel

Parcel uses a build cache, you probably want to put .parcel-cache into .gitignore.

Initialize a new plugin

Quickstart

First, run the init command:

# Initialize a new plugin (outside of your Sanity studio folder)
npx @sanity/plugin-kit@latest init sanity-plugin-testing-it-out

# Make your plugin linkable, and compile an initial version
cd sanity-plugin-testing-it-out
npm run link-watch

# In another shell, link the plugin to your Sanity studio
cd /path/to/my-studio
yalc add --link sanity-plugin-testing-it-out && yarn install

Now, configure the plugin in sanity.config.ts (or .js) in Sanity Studio:

  import {createConfig} from 'sanity'
  import {myPlugin} from 'sanity-plugin-testing-it-out'
 
  export const createConfig({
      //...
      plugins: [
          myPlugin({})
      ]
  })

Start the studio:

sanity start

Check browser console: the plugin should have logged "hello from my-sanity-plugin". Since the plugin is running in watch mode, any changes you make to the plugin code will be reloaded in the studio.

Verify plugin package

Verify that the plugin package is configured correctly by running:

npx @sanity/plugin-kit@latest verify-package

What does it do?

  • Check package.json for:
    • recommended script commands
    • recommended cjs and esm configuration
    • sanity dependency compatibility
    • parcel devDependency
    • recommended usage of devDependencies/peerDependencies/dependencies for certain packages
  • Check for redundant v2 config:
    • babel
    • rollup
    • sanity.json
  • Check for sanity imports that has changed in v3, using eslint
  • Check tsconfig.json settings
  • Check for SPDX compatible license definition
  • If the package uses TypeScript, this will also run tsc --noEmit when all other checks have passed

Each check will explain why it is needed, steps to fix it and how it can be individually disabled.

What it is not

verify-package is not a codemod tool (yet). It will only check files and recommended settings: it will not change any files.

Upgrading a v2 plugin

Simply use the verify-package command in a v2 plugin package, and it will notify you about steps you need to take to upgrade the plugin to v3.

npx @sanity/plugin-kit@latest verify-package

Upgrade help in V2 Studio

You can use the verify-studio command in a v2 Sanity Studio to get some of the same validation there, to help in the upgrade from v2 to v3.

npx @sanity/plugin-kit@latest verify-studio

This will:

  • Check for sanity.json, sanity.config.(ts|js) and sanity.cli.(ts|js) and advice on how to convert the former to the latter two.
  • Check for sanity dependencies that has changed in v3
  • Check for sanity imports that has changed in v3, using ESlint

Fail fast mode

## for plugins
npx @sanity/plugin-kit@latest verify-package --single

## for studio
npx @sanity/plugin-kit@latest verify-package --studio --single

This will only output the first validation that fails. Useful when working through the list of issues by fixing and rerunning the command.

Testing a plugin in Sanity Studio

Ensure you have the following script setup in package.json:

{
  "scripts": {
    "link-watch": "plugin-kit link-watch"
  }
}

Then, in a shell, run:

npm run link-watch

This will publish the plugin to a local yalc registry.

In another shell, in your test Sanity Studio directory, run:

npx yalc add <your-plugin-package> && npx yalc add <your-plugin-package> --link && npm install

You can now change your plugin code, which will:

  1. Trigger a rebuild using your watch task
  2. Update the files in the plugin output directory
  3. Trigger a yalc publish --push
  4. Update the files in your Sanity Studio
  5. Trigger hot-reload; you should see changes in the Studio

Note: Yalc will modify your studio package.json when linking; remember to revert it when you are done testing. You should also put .yalc and yalc.lock into .gitignore.

When you are done testing, you can run

npx yalc remove <your-plugin-package> && yarn install

to restore the version in package.json.

Link-watch configuration

This command can be configured using sanityPlugin.linkWatch in package.json:

{
  "sanityPlugin": {
    "linkWatch": {
      // directory to watch
      "folder": "lib",
      // command to run when content in linkWatch.folder changes
      "command": "npm run watch",
      // file extensions to watch for changes in the linkWatch.folder
      "extensions": "js,png,svg,gif,jpeg,css"
    }
  }
}

Why use yalc?

See the FAQ.

Publishing a plugin

Note: If you're writing a plugin that is only useful for yourself or your company, you might want to develop the plugin directly in the Studio (saves you from having to publish at all, and has improved hot-reload dev experience).

If the plugin is shared across multiple "private" studios: register an organization on npm and make sure your module is prefixed with the organization scope, eg @your-company/plugin-name.

Also; you cannot easily remove modules/versions from npm once published. Take a good look at your package.json to see that the fields in there makes sense to you, and make sure there are no "secrets" (authorization tokens, API keys or similar) in the plugin directory - anything not listed in .npmignore will be part of the published module.

When you're ready to publish, run npm publish (or yarn publish if you prefer). The prepublishOnly task should kick in and compile the source files, then verify the built output to ensure it looks good.

If you have not published any modules to npm before, you will be asked to create a user first.

FAQ

Q: Do I have to use this for developing Sanity plugins?

A: Absolutely not! Make sure your Sanity plugin is ES6-compatible. This package was created to make it easier to set up the build toolchain and prevent common mistakes.

If you know what you're doing and don't like any magic, roll your own thing! :)

Q: Why use yalc?

npm link & yarn link unfortunately can easily break the rules of hooks due to the way packages are resolved using symlinks.

Yalc bypass this problem as it more closely resembles installing a dependency as normal.

Q: Do I have to use yalc?

A: No!

Feel free to use any variation of npm link or yarn link alongside npm run watch for testing, but beware that if you get errors from React along the lines of

Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

you probably have to revert to using yalc, or use npm pack + and install the resulting tzg-file.

Q: Why use Parcel?

Parcel is recommended for building plugins as it provides sensible defaults for building a React library that output CommonJS and ESM files.

Parcel can in most cases be configured through package.json alone, and can often simply be dropped in to existing packages without further customization . @sanity/plugin-kit verify-package ensures Sanity Studio 3 compliant configs exist.

Parcel also has very speedy production builds, which is a big plus when testing plugins using watch-mode.

A: Sanity Studio V3 uses Vite as the default bundler,

Q: Can I use another build tool or change parcel configuration?

A: Yes!

Feel free to make any changes to parcel library configuration as is needed. @sanity/plugin-sdk verify output is only recommendations for defaults that has been tested to work in Sanity Studio. Your plugin may have other needs.

You are also free to not use parcel at all; simply change your package.json build script, and disable any verification-steps you don't care for with sanityPlugin.verifyPackage.

CLI Help

$ npx @sanity/plugin-kit --help 

Usage
  $ plugin-kit [--help] [--debug] <command> [<args>]

  These are common commands used in various situations:

    init            Create a new Sanity plugin
    verify-package  Verify that a Sanity plugin follows plugin-kit conventions
    splat           Inject plugin-kit complatible package config into an existing plugin directory
    link-watch      Recompile plugin automatically on changes and push to yalc
    version         Show the version of ${cliName} currently installed

  Options
    --silent      Do not print info and warning messages
    --verbose     Log everything. This option conflicts with --silent
    --debug       Print stack trace on errors
    --version     Output the version number
    --help        Output usage information

  Examples
    # Init a new plugin
    $ plugin-kit init

    # Verify that a Sanity plugin follows plugin-kit conventions
    $ plugin-kit verify-package

Configuration reference

Provide a sanityPlugin config in package.json (defaults shown):

{
  "sanityPlugin": {
    "linkWatch": {
      "folder": "lib",
      "command": "npm run watch",
      "extensions": "js,png,svg,gif,jpeg,css"
    }
  },
  "verifyPackage": {
    "packageName": true,
    "module": true,
    "tsconfig": true,
    "tsc": true,
    "dependencies": true,
    "rollupConfig": true,
    "babelConfig": true,
    "sanityV2Json": true,
    "eslintImports": true,
    "scripts": true,
    "parcel": true,
    "nodeEngine": true
  }
}

License

MIT © Espen Hovlandsdal and Sanity.io

Development

Test in another package

In one shell, run

npm link
npm run watch

In the package where you want to test plugin kit, run:

npm link @sanity/plugin-kit

Now you can run commands:

npx @sanity/plugin-kit verify-package

or use them in package.json scripts:

"verify": "plugin-kit verify-package"

Integration tests

npm run test

Run a single test-file

npm run test -- test/verify-package.test.ts

Update snapshots for a test

npm run test -- test/verify-package.test.ts --snapshot
Comments
Releases(v3.1.2)
Owner
Sanity
Sanity.io a platform for structured content that comes with an open source editor that you can customize with React.js.
Sanity
The backend for our Airbnb App, built using Sanity.io.

AirBnb Sanity.io Backend This repository is to support my tutorial on how to build an AirBnb Clone with strucutred content using Sanity.io and Next.js

Ania Kubow 73 Dec 28, 2022
Airbnb-sanity - 🧳 Chrome extension to hide Airbnb listings you don't like

Airbnb Sanity ?? Coming soon to the Chrome Webstore! Chrome extension to hide Airbnb listings you don't like. Tired of looking through the same Airbnb

Johannes Rieke 4 Apr 4, 2022
Rede social baseada no ShareMe para compartilhamento e download de fotos em React,Sanity.io e TailWindCSS

Photophan ?? Descrição phothopan é uma rede social para o compartilhamento e download de imagens,foi desenvolvida utilizando React(Frontend),Sanity(Ba

Bruno Ariel 2 Jan 8, 2022
This is the repo for the Medium2 project with Next.js, Sanity CMS, React and Tailwind CSS

Next.js + Tailwind CSS Example This example shows how to use Tailwind CSS (v3.0) with Next.js. It follows the steps outlined in the official Tailwind

null 1 Jan 22, 2022
Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill).

@sanity/client Javascript client for Sanity. Works in node.js and modern browsers (older browsers needs a Promise polyfill). Requirements Sanity Clien

Sanity 23 Nov 29, 2022
Medium-Clone with Next.JS, Typescript, Tailwindcss, and Sanity!!

Medium Clone ?? Overview /pages ✔️ pages/index.tsx = Homepage and list all Blogs ✔️ pages/post/[slug].tsx = Details Blog /pages/api ✔️ pages/api/creat

argikurnia 23 Nov 16, 2022
A multi-tag input for sanity studio.

sanity-plugin-tags A multi-tag input for sanity studio. Fully featured with autocomplete capabilities, live updates, predefined tag options, style and

P Christopher Bowers 7 Jul 22, 2022
Monorepo for open source libraries used by nrkno-sanity

NRK.no Sanity libraries NRK.no Sanity libraries contains an assortment of plugins and libraries used by NRK.no to extend Sanity Studio and apps using

Norsk rikskringkasting (NRK) 10 Nov 30, 2022
Stablo is a minimal blog website template built with Next.js, TailwindCSS & Sanity CMS

Stablo Blog Template - Next.js & Sanity CMS Stablo is a JAMStack Starter template built with Next.js, Tailwind CSS & Sanity CMS by Web3Templates. Clic

Web3Templates 159 Dec 30, 2022
My personal website – Built using Next.js, TypeScript, MDX, Sanity.io and Tailwind

kenaqshal.com Framework: Next.js Database: PlanetScale ORM: Prisma Authentication: NextAuth.js Deployment: Vercel CMS: Sanity Styling: Tailwind CSS Ov

Ken Aqshal Bramasta 6 Nov 24, 2022
A minimal e-commerce store using Gatsby, SANITY, Stripe, Use-Shopping-Cart and Netlify

?? Gatsby Starter Stripemart Like a supermarket but for Stripe. No ongoing monthly costs. Perfect for artists, creators and independent builders doing

Eric Howey 26 Nov 14, 2022
Student reviews for OMS courses. Built with NextJS and Typescript. Backed by Sanity CMS. Deployed on Vercel.

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

OMS Tech 27 Dec 3, 2022
Generate social preview images in your Next.js API from Sanity webhooks

sanity-next-social-image-generator Automatically generate social share images using Sanity webhooks, and your Next.js API! Requirements A Next.js appl

Jordan McRae 9 Sep 4, 2022
nextjs + sanity studio v3 = 💖

Sanity.io and Next.js [main image placeholder] Sanity.io is the platform for structured content. With Sanity.io you can use the open-source, single pa

Sanity 100 Dec 26, 2022
NextJS E-commerce starter kit with Sanity.io and Stripe API 🛍

?? Next.js Sanity E-commerce Starter Kit A Next.js E-commerce app with Sanity.io and Stripe API. Built with TailwindCSS framework & SASS CSS extension

Lougie Quisel 11 Dec 29, 2022
A Drag-and-Drop library for all JavaScript frameworks implementing an enhanced transformation mechanism to manipulate DOM elements

JavaScript Project to Manipulate DOM Elements DFlex A Drag-and-Drop library for all JavaScript frameworks implementing an enhanced transformation mech

DFlex 1.5k Jan 8, 2023
A web component for progressively-enhanced auto-expanding textareas

Elastic Textarea A web component for progressively-enhanced auto-expanding textareas. This web component progressively enhances the native textarea: a

Cloud Four 29 Jan 9, 2023
An enhanced VSCode extension for the Move programming language.

Move Analyzer Plus Provides language support for the Move programming language. Install on the VSCode Extension Marketplace: Move Analyzer Plus on the

The Moving Company 10 Aug 12, 2022
Enhanced interval features for Node.js, such as promisified interval and human readable time parsing.

Interval-next Interval-next is a package that extends Javascript's built-in setInterval() capabilities. You have a plain and promisified interval meth

Snowy 5 Jul 28, 2022