最初のネイティブプラグイン

このページ

このガイドでは、ネイティブプラグインをゼロから構築する方法を説明します。ネイティブプラグインは、React管理ページ、Portable Textコンポーネント、ページフラグメントなど、ランタイムへの完全なアクセス権を持ち、Astroサイトと同じプロセスで実行されます。

サンドボックス化されたプラグインではなくネイティブプラグインが必要かどうかまだ決めていない場合は、まずプラグインフォーマットの選択をお読みください。ネイティブは、React管理ページ、Portable Textレンダリングコンポーネント、またはページフラグメントを必要とするプラグインのフォーマットです。

1つまたは2つのファイルに2つの部分

サンドボックス化されたプラグインと同様に、ネイティブプラグインには2つの部分があります:

  1. ディスクリプタファクトリformat: "native"と管理関連のエントリーポイントを含むPluginDescriptorを返します。ビルド時にastro.config.mjsによってインポートされます。
  2. createPlugin(options)関数 — ランタイム側。definePlugin({ id, version, capabilities, hooks, routes, admin })の結果を返します。

サンドボックス化されたプラグインとは異なり、両方の部分は同じファイルに配置できます。なぜなら、異なる環境で実行されるのではなく、プラグイン全体がインプロセスで実行されるためです。パッケージの"."エクスポートは、ディスクリプタファクトリとcreatePlugin(またはdefault)関数の両方をエクスポートするファイルを指します:

my-native-plugin/
├── src/
│   ├── index.ts          # Descriptor factory + createPlugin
│   ├── admin.tsx         # React admin components (optional)
│   └── astro/            # Astro components for PT block rendering (optional)
│       └── index.ts
├── package.json
└── tsconfig.json

パッケージのセットアップ

次のpackage.jsonは、ネイティブプラグインに必要なエントリーポイントとピア依存関係を宣言します:

{
	"name": "@my-org/plugin-analytics",
	"version": "0.1.0",
	"type": "module",
	"main": "dist/index.js",
	"exports": {
		".": {
			"types": "./dist/index.d.ts",
			"import": "./dist/index.js"
		},
		"./admin": {
			"types": "./dist/admin.d.ts",
			"import": "./dist/admin.js"
		}
	},
	"files": ["dist"],
	"peerDependencies": {
		"emdash": "*",
		"react": "^18.0.0"
	}
}

emdashreactをピア依存関係として保持することで、ホストサイトが実際のバージョンを提供し、重複を出荷しないようにします。

ディスクリプタとランタイムの記述

次のsrc/index.tsは、ディスクリプタファクトリとcreatePluginランタイムを1つのファイルで定義します:

import { definePlugin } from "emdash";
import type { PluginDescriptor } from "emdash";

export interface AnalyticsOptions {
	enabled?: boolean;
	maxEvents?: number;
}

export function analyticsPlugin(options: AnalyticsOptions = {}): PluginDescriptor {
	return {
		id: "analytics",
		version: "0.1.0",
		format: "native",
		entrypoint: "@my-org/plugin-analytics",
		options,
		adminEntry: "@my-org/plugin-analytics/admin",
		adminPages: [{ path: "/dashboard", label: "Dashboard", icon: "chart" }],
		adminWidgets: [{ id: "events-today", title: "Events Today", size: "third" }],
	};
}

export function createPlugin(options: AnalyticsOptions = {}) {
	const maxEvents = options.maxEvents ?? 100;

	return definePlugin({
		id: "analytics",
		version: "0.1.0",

		capabilities: ["network:request"],
		allowedHosts: ["api.analytics.example.com"],

		storage: {
			events: { indexes: ["type", "createdAt"] },
		},

		admin: {
			entry: "@my-org/plugin-analytics/admin",
			settingsSchema: {
				trackingId: { type: "string", label: "Tracking ID" },
				enabled: { type: "boolean", label: "Enabled", default: options.enabled ?? true },
			},
			pages: [{ path: "/dashboard", label: "Dashboard", icon: "chart" }],
			widgets: [{ id: "events-today", title: "Events Today", size: "third" }],
		},

		hooks: {
			"plugin:install": async (_event, ctx) => {
				ctx.log.info("Analytics plugin installed", { maxEvents });
			},

			"content:afterSave": async (event, ctx) => {
				const enabled = await ctx.kv.get<boolean>("settings:enabled");
				if (enabled === false) return;

				await ctx.storage.events.put(`evt_${Date.now()}`, {
					type: "content:save",
					contentId: event.content.id,
					createdAt: new Date().toISOString(),
				});
			},
		},

		routes: {
			stats: {
				handler: async (ctx) => {
					const today = new Date().toISOString().split("T")[0];
					const count = await ctx.storage.events.count({
						createdAt: { gte: today },
					});
					return { today: count };
				},
			},
		},
	});
}

export default createPlugin;

この構成の重要な詳細:

  • format: "native"は必須です。 "native"もデフォルトですが、すべてのディスクリプタで明示的に記述することで、フォーマットが簡単に識別できます。
  • entrypointはパッケージのメインエクスポートです。 EmDashはランタイムでそれをインポートし、デフォルトエクスポートを呼び出して解決されたプラグインを構築します。
  • optionsはディスクリプタからcreatePluginへ流れます。 プラグインを登録するときにユーザーが渡すもの(analyticsPlugin({ enabled: false }))は、ディスクリプタに保存され、createPluginに転送されます。サンドボックス化されたプラグインにはこの表面がありません — 代わりにKVから設定を読み取ります。
  • idversioncapabilitiesは2回表示されます。 ディスクリプタに1回、definePlugin()に1回。これらは一致する必要があります。ディスクリプタのコピーは、ビルド時にastro.config.mjsが見るものです。definePlugin()のコピーは、リクエスト時に実行されるものです。
  • ネイティブルートハンドラは単一の引数を取ります(ctx: RouteContext)で、ctx.inputctx.requestctx.requestMetaが通常のPluginContextプロパティとマージされます。これは、標準フォーマットの2引数形式の逆です。完全な表面については、APIルートを参照してください(その他すべては同じです)。

プラグインIDルール

idフィールドは/^[a-z][a-z0-9_-]*$/に一致する必要があります — 小文字で始まり、その後に文字、数字、ハイフン、またはアンダースコアが続きます。IDは、プラグインルートURLの単一のパスセグメントとして、およびプラグインストレージインデックス用に生成されたSQL識別子の一部として使用されるため、そのパターン外のものはランタイムで失敗します。次の値は、どのIDが受け入れられるかを示しています:

// Valid
"seo";
"audit-log";
"audit_log";
"plugin-forms";

// Invalid
"@my-org/plugin-forms";  // scoped form not allowed at runtime
"MyPlugin";              // no uppercase
"42-plugin";             // can't start with a digit
"my.plugin";             // no dots

スコープなしのidentrypointのスコープ付きnpmパッケージ名とペアリングします — パッケージ名とプラグインIDは別の関心事です。

バージョンフォーマット

セマンティックバージョニングを使用してください。次の値は、どのバージョン文字列が受け入れられるかを示しています:

version: "1.0.0";       // valid
version: "1.2.3-beta";  // valid (prerelease)
version: "1.0";         // invalid (missing patch)

プラグインの登録

サイトのastro.config.mjsで、ディスクリプタファクトリをインポートし、plugins: []配列に渡します — ネイティブプラグインは常にインプロセスで実行され、sandboxed: []には決して入りません:

import { defineConfig } from "astro/config";
import emdash from "emdash/astro";
import { analyticsPlugin } from "@my-org/plugin-analytics";

export default defineConfig({
	integrations: [
		emdash({
			plugins: [
				analyticsPlugin({ enabled: true, maxEvents: 500 }),
			],
		}),
	],
});

設定UI

ネイティブプラグインは、自動生成される設定フォームにadmin.settingsSchemaを使用できます。これが最も簡単な方法です:

admin: {
	settingsSchema: {
		apiKey: { type: "secret", label: "API Key" },
		enabled: { type: "boolean", label: "Enabled", default: true },
		maxItems: { type: "number", label: "Max items", min: 1, max: 1000, default: 100 },
	},
},

フィールドタイプ: stringnumberbooleanselectsecreturlemail。それぞれはlabeldescriptiondefaultを受け入れ、さらにmin/max/optionsなどのタイプ固有の追加項目も受け入れます。設定は、サンドボックス化されたプラグインが使用するのと同じプラグインごとのKVストアに保存されます — どこからでもctx.kv.get<T>("settings:<key>")で読み取ります。

settingsSchemaが提供するよりも豊富な設定UIについては、カスタムReactページを出荷してください — React管理ページとウィジェットを参照してください。

完全な例 — 監査ログプラグイン

次のプラグインは、すべてのコンテンツの作成、更新、削除をインデックス化されたストレージに記録し、最近のアクティビティルートを公開します:

import { definePlugin } from "emdash";
import type { PluginDescriptor } from "emdash";

interface AuditEntry {
	timestamp: string;
	action: "create" | "update" | "delete";
	collection: string;
	resourceId: string;
	userId?: string;
}

export function auditLogPlugin(): PluginDescriptor {
	return {
		id: "audit-log",
		version: "0.1.0",
		format: "native",
		entrypoint: "@emdash-cms/plugin-audit-log",
	};
}

export function createPlugin() {
	return definePlugin({
		id: "audit-log",
		version: "0.1.0",

		storage: {
			entries: {
				indexes: [
					"timestamp",
					"action",
					"collection",
					["collection", "timestamp"],
					["action", "timestamp"],
				],
			},
		},

		admin: {
			settingsSchema: {
				retentionDays: {
					type: "number",
					label: "Retention (days)",
					description: "Days to keep entries. 0 = forever.",
					default: 90,
					min: 0,
					max: 365,
				},
			},
			pages: [{ path: "/history", label: "Audit History", icon: "history" }],
			widgets: [{ id: "recent-activity", title: "Recent Activity", size: "half" }],
		},

		hooks: {
			"content:afterSave": {
				priority: 200,
				handler: async (event, ctx) => {
					const entry: AuditEntry = {
						timestamp: new Date().toISOString(),
						action: event.isNew ? "create" : "update",
						collection: event.collection,
						resourceId: event.content.id as string,
					};
					await ctx.storage.entries.put(`${Date.now()}-${event.content.id}`, entry);
				},
			},

			"content:afterDelete": {
				priority: 200,
				handler: async (event, ctx) => {
					await ctx.storage.entries.put(`${Date.now()}-${event.id}`, {
						timestamp: new Date().toISOString(),
						action: "delete",
						collection: event.collection,
						resourceId: event.id,
					});
				},
			},
		},

		routes: {
			recent: {
				handler: async (ctx) => {
					const result = await ctx.storage.entries.query({
						orderBy: { timestamp: "desc" },
						limit: 10,
					});
					return {
						entries: result.items.map((item) => ({
							id: item.id,
							...(item.data as AuditEntry),
						})),
					};
				},
			},
		},
	});
}

export default createPlugin;

テスト

プラグインが登録された最小限のAstroサイトを作成して、ネイティブプラグインをテストします:

  1. EmDashがインストールされたテストサイトを作成します。
  2. astro.config.mjsでプラグインを登録し、ローカルソースパスから直接インポートします。
  3. 開発サーバーを実行し、コンテンツを作成、更新、または削除してフックをトリガーします。
  4. ctx.log出力のコンソールを確認し、APIルート経由でストレージを検証します。

ユニットテストの場合は、PluginContextインターフェースをモックし、フックハンドラを直接呼び出します。

次のステップ