Plugin API Reference

All contract types are defined in base/src/plugin.d.ts.

Plugin Definition

type PluginDefinition = {
  name: string;
  version: string;
  hooks: PluginHooks;
  load?: (ctx: PluginLoadContext) => void;
};

Hook Names

Defined as frozen constants in base/src/plugin.js:

ConstantValue
plugin.hooks.DIST_CLEAN"dist:clean"
plugin.hooks.ASSETS_COPY"assets:copy"
plugin.hooks.CONTENT_LOAD"content:load"
plugin.hooks.CONTENT_READY"content:ready"
plugin.hooks.PAGE_META"page:meta"

Base Context (createBaseContext())

Provided to every hook handler:

FieldTypeDescription
configConfigApiSite configuration with get(key) accessor
logLogApiLogging: info, warn, err, debug, step
file.read(path) => Promise<string>Read UTF-8 file
file.write(path, content) => Promise<void>Write UTF-8 file
file.exists(path) => Promise<boolean>Check file existence
directory.read(path) => Promise<string[]>List directory contents
directory.exists(path) => Promise<boolean>Check directory existence
directory.create(path) => Promise<void>Create directory recursively
path.combine(...paths) => stringJoin path segments
path.resolve(...paths) => stringResolve absolute path
path.name(path) => stringGet directory name

Extended Context (from PluginEngine)

FieldTypeAvailable In
pathsPluginPathsAll hooks
i18nI18nApiAll hooks
contentFilesContentFile[]content:load, content:ready
addContent(input) => voidcontent:load
pagesCollectionsByLangcontent:ready
footerPoliciesRecord<string, FooterPolicy[]>content:ready
contentIndexRecord<...>content:ready
frontMatterRecord<string, any>page:meta
derivedFrontMatterRecord<string, any>page:meta
langstringpage:meta
slugstringpage:meta
pageMetaRecord<string, any>page:meta
setPageMeta(meta) => voidpage:meta

PluginPaths

FieldExample
root./
src./src
dist./dist
tmp./tmp
content./src/content
layouts./src/layouts
components./src/components
templates./src/templates
assets./src/assets
siteConfig./src/site.json
i18nConfig./src/i18n.json

Schema Types (plugin.schema)

post, job-post, job-listing, not-found, page, home, contact, about, press, help, faq, collection, policy

Collection Types (plugin.collection)

tag, category, series

Minimal Plugin Skeleton

import { plugin } from "@shevky/base";

const hooks = {
  [plugin.hooks.ASSETS_COPY]: async function (ctx) {
    ctx.log.info("My plugin running!");
    // plugin work here
  },
};

export default {
  name: "my-plugin",
  version: "0.0.1",
  hooks,
};

Related