scmJS docs

The plugin host

plugins.md is written for someone who has the npm package and none of this repository. This is the editor's half: where the plugin system lives and how a plugin gets from an address to a running activate.

File Holds
src/plugins/api.ts The public types: the only place the contract is written
src/plugins/host.ts Builds one plugin's API over the store; activation, deactivation, inspect and install
src/plugins/loader.ts Spec parsing, the manifest fetch, and the fetch / transpile / import pipeline
src/plugins/defaults.ts The plugins a fresh editor starts with, each pinned to a tag
src/plugins/registry.ts Browse Plugins: reading, searching and caching the registry indexes
src/plugins/widgets.ts api.ui.widgets: plain DOM in the editor's own class names, and the waiting kit
src/plugins/claims.ts The trigger-list claims the trigger editors read
src/plugins/images.ts Image loading, clipboard images, and drops and pastes in plugin dialogs
src/plugins/failures.ts The toast a failed activation raises
src/plugins/builtin.ts The vendored defaults in plugins/*/, compiled in
src/plugins/transpile.worker.ts TypeScript in a worker, for a .ts plugin loaded from source
src/atoms/pluginAtoms.ts The installed list, stored code copies, runtimes, contributions, and the requests the viewport serves
src/hooks/usePlugins.ts Activates the enabled plugins at startup and keeps the runtimes in step with the list
src/components/dialogs/PluginDialogs.tsx Manage Plugins, Browse, the Add / Update confirmation, a plugin dialog's frame
src/components/panels/PluginPanels.tsx The floating and docked frames a plugin's panel mounts into
src/components/ui/DialogSlots.tsx The footer row a plugin's dialog slot mounts into

The chrome reads the contributions: the menu bar merges plugin menu items into its model, the context menus and status bar add theirs, the right dock renders docked panels after Properties, and the hotkey hook checks plugin key combinations first. The viewport serves a plugin's map requests: picks, map tools and overlays.

A plugin's document.edit commits one history entry through the same path a brush stroke takes, and document.update commits through the settings and trigger commits. document.sections rewrites the file and reloads it, which drops the history. While a map is shared, every one of those commits also reaches the session. plugins-host.md and sync.md in .claude/internals/ have the details.

The loading pipeline#

  1. The spec is parsed into a base URL. builtin:<name> is a plugin compiled in from plugins/<name>/; a github: spec or a github.com URL resolves to raw.githubusercontent.com/owner/repo/<ref or HEAD>/<dir>/; any other URL is a manifest, an entry file (a manifest is made up from its name) or a directory holding plugin.json.
  2. The manifest is fetched and validated; only name is required.
  3. The file to import is the manifest's build when it has one (a committed bundle, one fetch, no transpile), else its entry.
  4. That file is fetched as text and, if it is TypeScript, transpiled in the worker. Fetching as text matters: raw.githubusercontent.com serves text/plain, which a browser refuses to import as a module.
  5. Relative imports are followed depth first, and each file becomes a blob: module URL with its specifiers rewritten. An extensionless specifier is tried as .ts, .tsx, .mts, .js, .mjs and then as that directory's index.*; ./x.js falls back to ./x.ts. Cycles and bare package names are errors naming the file.
  6. The module is imported and its default export (or a named activate) is called with the API. Whatever it returns is kept for deactivation.

Adding one#

Pressing Add in Manage Plugins installs nothing. The editor canonicalises the address, asks GitHub which commit the ref points at, and reads the plugin.json at that commit. No code is fetched, transpiled or imported before the user agrees. The confirmation opens only once a manifest came back; an address with nothing behind it is reported under the Add field instead.

The install has three ticks: Enable it now, Pin to this version (the stored spec becomes github:owner/repo@<sha>) and Load from a copy saved here (below). The Update button on a pinned row previews the branch and, when it holds a different commit, reopens the same dialog with the old commit marked to be replaced.

A listed plugin that is not running is still described: its manifest alone is fetched for the name, version, description and icon, and cached so the next visit shows them at once. Browse Plugins hands a chosen entry to the same Add path, so a registry decides what is listed and never what is trusted.

Loading from a copy#

Load from a copy saved here means "prefer the copy". With no copy yet, the load records every fetched file into browser storage (up to a size cap; a larger plugin stays remote with a console warning). With a copy, the load has no network path at all and fails on a file the copy lacks. Reload drops the copy and fetches again; turning the option off drops it too.

Defaults and vendoring#

The default plugins are ordinary plugins from their own repositories: Aftermath, Paint, Repair, scm-sim, scmjs.dev, scmscx.com, Stamp Library, Terrain from Image, TrigEdit, TrigScript and Walkability. All start on except TrigEdit, which is installed but off. Each is pinned in src/plugins/defaults.ts to a tag, never a branch, so a push to a plugin repository cannot change editors already in use, and any release can be rebuilt as it shipped. Moving a default forward is a commit that changes the tag there. A plugin's identity is its repository, whatever the version, so an older unpinned spec, the compiled-in copy and the pinned default are one plugin, not three.

scripts/vendor-plugins.mjs writes each default's source at its tag into the gitignored plugins/, where builtin.ts compiles it into the bundle. Every build does this, so a first visit loads no compiler and makes no third-party requests. The script runs from predev, prebuild and the desktop build, fetches only what is not already there at the pinned version, and uses GITHUB_TOKEN when set to stay off the anonymous rate limit. A directory it wrote carries a vendored.json; one you put there by hand is left alone.

npm run vendor:plugins             # bring plugins/ up to date
npm run vendor:plugins -- --force  # fetch again even where the spec matches
npm run vendor:plugins -- --clean  # remove the directory
npm run vendor:plugins -- --list   # print the pinned specs
SCMJS_SKIP_VENDOR=1 npm run build  # skip it: that bundle fetches its defaults at startup

tests/vendor-plugins.test.ts pins the script's parsing and the rule that no default may be unpinned.

Plugin typings#

Plugin repositories compile against @scm-js/plugin-api, an npm package holding one index.d.ts bundled from src/plugins/api.ts.

npm run build:plugin-types      # → plugin-api/index.d.ts + package.json (gitignored)
npm run publish:plugin-types    # publish to npm and push to github.com/scm-js/plugin-api

The build refuses a bundle that still carries an import, since a file naming jotai or react is one a plugin repository cannot compile with alone. The version is the API's, not the editor's: the major is PLUGIN_API_VERSION, the minor moves when the declarations do, and a build that did not change the contract publishes nothing. The plugin-api job in the Build workflow runs it on every build. The push to scm-js/plugin-api uses the PLUGIN_API_KEY deploy key (without it the job reports and carries on), and the npm publish is behind the PUBLISH_PLUGIN_API variable, using npm's trusted publishing with provenance. tests/plugin-api-package.test.ts pins the version rule and the no-imports rule.

Each plugin repository calls the shared workflow plugin-ci.yml in scm-js/.github. It type-checks, tests, rebuilds and commits the plugin's dist/plugin.js on the main branch, and at a tag checks that the committed bundle is what the source builds to.

scmJS 0.18.0 · Generated from the repository. The source is under the MIT license, which does not cover what ATTRIBUTION.md lists. StarCraft and Brood War are trademarks of Blizzard Entertainment; this project ships none of their data.