Writing a plugin
Clone an official template, follow its README, and copy from its worked example. Templates ship a working build and a local JBrowse instance to test against.
Plugins add new pluggable elements (views, tracks, displays, adapters, widgets, etc.) and can modify application behavior by watching state. See pluggable elements for the full list of element types you can register.
Official templates
| Template | Bundler |
|---|---|
| jbrowse-plugin-esbuild-template | esbuild |
| jbrowse-plugin-template | rollup |
They are otherwise the same: pnpm, vitest unit tests, and Puppeteer end-to-end tests against a nightly JBrowse build. Pick esbuild for faster builds; rollup is older and more widely referenced in existing examples.
Each template ships a custom view (src/HelloView) as its worked example.
For a custom track/display, scaffold from a template and then follow
Plotting features in a custom display. Every code block in that guide is
generated from the complete plugin in example-plugins/score-example/.
The templates pin @jbrowse/core 4.x, whose re-export list names neither
@jbrowse/display-kit nor @jbrowse/render-core, so the imports in that guide
do not resolve in a template as cloned. Until 5.0.0 ships, a v5 display plugin
installs the three packages, and @jbrowse/plugin-linear-genome-view for its
types, from npm's next tag (their latest is not v5):
pnpm add -D @jbrowse/core@next @jbrowse/display-kit@next @jbrowse/render-core@next @jbrowse/plugin-linear-genome-view@nextThen set mobx, mobx-react and @jbrowse/mobx-state-tree to the ranges
npm view @jbrowse/core@next dependencies prints. A plugin that keeps the
template's older ranges gets a second copy of each, and its typecheck fails with
errors that name neither package.
What's in a plugin
A plugin is a class extending Plugin. name is the only required member, and
JBrowse loads one plugin per name, so rename the plugin everywhere the template
README's "Renaming the plugin" section lists:
- the
namefield inpackage.json - the class and its
namefield insrc/index.ts - the bundle's global and output file:
globalNameandoutfileinesbuild.mjs, or the outputnameand file names inrollup.config.mjs - the plugin's
nameandurlinconfig.json, wherenameis the global minus itsJBrowsePluginprefix (Templatefor the template's global, JBrowsePluginTemplate)
The rest you implement as needed:
install()— registers pluggable elements against thepluginManager.configure()— runs afterwards, typically to set up mobx autoruns over application state.- The bundle,
install()andconfigure()also run in each RPC worker, wherepluginManager.rootModelis undefined and there is nowindowordocument. A bundle that reads the DOM at module scope fails the worker's plugin load, and every RPC call fails with it. Guard main-thread work onpluginManager.rootModel, as the no-build plugin's menu item does. version— what the plugin store shows beside the name.
On the main thread, a plugin loaded by url that throws fails alone: a throw from
install(), from a function given to addViewType or its siblings, or from
configure() leaves the plugin out, or partly registered (the elements it
created before the throw stay), and the session opens with a notice naming it.
In the RPC worker, a bundle that throws at module scope fails the worker's
plugin load, and every RPC call with it.
The element-specific guides walk through each type:
- Custom track and display types — Define track types (high-level identity) and display types (how a track renders in a given view)
- Plotting features in a custom display — Build a plugin that fetches features in a worker and declares what it draws as a mark list, which every backend and the SVG export draw
- GPU displays — Write a shape of your own when no shared one fits, and declare it as a mark the WebGPU, WebGL2 and Canvas2D backends all draw
- Add-track workflows — Custom UI in the Add track dialog for non-standard track types
- Custom adapters — Read data from custom file formats with feature, regions, or sequence adapters
- Custom connections — Add many tracks at once, or dynamically query a remote resource, with a connection type
- Custom view types — Add entirely new view panels such as DotplotView or CircularView
- Custom widgets — Add new drawer/panel UI components
- Drawer widgets — Launching sidebar or popup widgets in the embedded LGV
- Menus — Add items to the application menu bar, the track menu, and track context menus
- SVG export — How to implement renderSvg on a custom display type
- Text search adapters — Implement a custom backend for the search box
- Upgrading to v5 — What changed for plugin authors and embedders in JBrowse 2 v5.0.0
- What your plugin costs at load — A config that names your plugin downloads and evaluates all of it before anything draws — how to make that cost your registration rather than your whole bundle
A plugin can also add configuration slots, through one of two class members that differ only in where they put the slots:
configurationSchemanests them under the plugin's ownname, so a slot reads asconfiguration.MyPlugin.mySlot. This is the one to reach for, since the slots are in a namespace named for the plugin.rootConfigurationSchemais a function of the plugin manager whose result is spread into the root config, for a schema that has to be built against what is registered.
In TypeScript, session.configuration names only the root slots core and the
app declare, so read a plugin namespace through a config typed
AnyConfigurationModel, as plugin-linear-genome-view's sessionTrackLabels
does.
For plugins that don't need a build step (e.g. jexl callbacks or small behavior tweaks), see writing a no-build plugin.
See also
- Developer guide: Pluggable elements
- Developer guide: Writing a no-build plugin
- Developer guide: Testing a plugin
- Developer guide: Dependencies and re-exports