Skip to content

Latest commit

 

History

History
92 lines (74 loc) · 4.19 KB

plugin.md

File metadata and controls

92 lines (74 loc) · 4.19 KB

plugin

During the gro dev and gro build tasks, Gro uses Plugins to support custom usecases outside of the normal build pipeline.

Where it's going: the lifecycle stays. On the runtime, the Node tools plugins wrap run as subprocesses under the run's kernel rules until each is replaced.

In this early implementation of plugins in Gro, plugins run serially, in the order they are returned from plugins in the gro.config.ts. Each step of Gro's build processes - gro dev for development and gro build for production - runs a method of each plugin, batched together as setup -> adapt -> teardown, with some behavioral inconsistencies:

  • gro dev passes plugins {dev: true, watch: true} (watch: false with --no-watch), and gro build passes {dev: false, watch: false}
  • adapt only runs during production aka gro build
  • teardown does not run for gro dev in the default watch mode, but it does run with gro dev --no-watch
  • there should probably be a finalization step that runs teardown on uncaught exceptions

The API needs to be improved for more advanced usecases, currently it offers little flexibility - we'll follow the Vite/SvelteKit APIs probably. (pre etc) Maybe let you map the array of each method batch. (is that possible with those?)

Gro's builtin plugins:

gen plugin docs are not yet written

Also see config.plugins in the config docs and usage in the default config. The default config detects which plugins are included by inspecting the current project.

The implementation is at src/lib/plugin.ts with more details.

export interface Plugin<TPluginContext extends PluginContext = PluginContext> {
	name: string;
	setup?: (ctx: TPluginContext) => void | Promise<void>;
	adapt?: (ctx: TPluginContext) => void | Promise<void>;
	teardown?: (ctx: TPluginContext) => void | Promise<void>;
}

export interface PluginContext<TArgs = object> extends TaskContext<TArgs> {
	dev: boolean;
	watch: boolean;
	host: PluginHost;
}

export interface PluginHost {
	readonly fs_paths: FsPaths;
	readonly env: Env;
	readonly spawn: Spawn;
	readonly import_module: ImportModule;
	readonly loader_path: string;
}

PluginContext extends the task context, so plugins get the same task_args, config, graph, logger, raw_rest_args, and invoke_task as tasks; see TaskContext in the task docs. Its host holds the handles gro dev or gro build opens for its plugins: the fs boundary, the environment, spawn, and the project loader, and the path of the Node loader of the Gro the task runs files with, for a plugin that runs one as gro run does, like gro_plugin_server. Gro's own plugins take what they reach from there at setup, adapt, or teardown rather than by importing a host module, so they and the default config import none when they load, nor a node: builtin, and loading the config, which every command does, reaches none through them. That's a convention, which a test of Gro's import closures checks, not a rule a runtime enforces: on Node a project's plugin or config can still import a host module itself. The handles are the task's own, not cut per plugin.

error handling

Plugin errors fail the build immediately, stopping the lifecycle:

  • setup() error → adapt and teardown skipped
  • adapt() error → teardown skipped
  • teardown() error → build fails

This surfaces errors quickly. A future improvement may add cleanup-on-error handling.