|
| 1 | +# Plugin Development |
| 2 | + |
| 3 | +STX provides a comprehensive plugin system that allows you to extend the framework's functionality. This guide covers how to create, distribute, and maintain STX plugins. |
| 4 | + |
| 5 | +## Plugin Architecture |
| 6 | + |
| 7 | +### Basic Plugin Structure |
| 8 | + |
| 9 | +```typescript |
| 10 | +export interface MyPluginOptions { |
| 11 | + enabled?: boolean |
| 12 | + apiKey?: string |
| 13 | + customSettings?: Record<string, any> |
| 14 | +} |
| 15 | + |
| 16 | +export class MyPlugin { |
| 17 | + constructor(private options: MyPluginOptions = {}) {} |
| 18 | + |
| 19 | + install(app: STXApp) { |
| 20 | + // Plugin initialization logic |
| 21 | + app.provide('myPlugin', this) |
| 22 | + app.config.globalProperties.$myPlugin = this |
| 23 | + } |
| 24 | + |
| 25 | + async initialize() { |
| 26 | + // Setup logic |
| 27 | + } |
| 28 | + |
| 29 | + destroy() { |
| 30 | + // Cleanup logic |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export default MyPlugin |
| 35 | +``` |
| 36 | + |
| 37 | +### Plugin Registration |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { createApp } from '@stx/core' |
| 41 | +import MyPlugin from './plugins/my-plugin' |
| 42 | + |
| 43 | +const app = createApp() |
| 44 | + |
| 45 | +app.use(MyPlugin, { |
| 46 | + enabled: true, |
| 47 | + apiKey: process.env.MY_PLUGIN_API_KEY |
| 48 | +}) |
| 49 | +``` |
| 50 | + |
| 51 | +## Plugin Types |
| 52 | + |
| 53 | +### Compiler Plugins |
| 54 | + |
| 55 | +```typescript |
| 56 | +export function createTransformerPlugin(options: TransformerOptions) { |
| 57 | + return { |
| 58 | + name: 'custom-transformer', |
| 59 | + transform(code: string, id: string) { |
| 60 | + if (id.endsWith('.stx')) { |
| 61 | + return code.replace(options.transformPattern, options.replacement) |
| 62 | + } |
| 63 | + return null |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Runtime Plugins |
| 70 | + |
| 71 | +```typescript |
| 72 | +export class StateManagerPlugin { |
| 73 | + private store = new Map() |
| 74 | + |
| 75 | + install(app: STXApp) { |
| 76 | + app.provide('state', { |
| 77 | + get: (key: string) => this.store.get(key), |
| 78 | + set: (key: string, value: any) => this.store.set(key, value), |
| 79 | + delete: (key: string) => this.store.delete(key) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Plugin Distribution |
| 86 | + |
| 87 | +### NPM Package Structure |
| 88 | + |
| 89 | +```json |
| 90 | +{ |
| 91 | + "name": "@stx/my-plugin", |
| 92 | + "version": "1.0.0", |
| 93 | + "main": "dist/index.js", |
| 94 | + "types": "dist/index.d.ts", |
| 95 | + "files": ["dist"], |
| 96 | + "peerDependencies": { |
| 97 | + "@stx/core": "^2.0.0" |
| 98 | + }, |
| 99 | + "keywords": ["stx", "plugin", "my-plugin"] |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### TypeScript Definitions |
| 104 | + |
| 105 | +```typescript |
| 106 | +declare module '@stx/core' { |
| 107 | + interface ComponentCustomProperties { |
| 108 | + $myPlugin: MyPlugin |
| 109 | + } |
| 110 | + |
| 111 | + interface GlobalComponents { |
| 112 | + MyComponent: typeof MyComponent |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Testing Plugins |
| 118 | + |
| 119 | +### Unit Testing |
| 120 | + |
| 121 | +```typescript |
| 122 | +import { test, expect } from 'bun:test' |
| 123 | +import { createApp } from '@stx/core' |
| 124 | +import MyPlugin from '../src' |
| 125 | + |
| 126 | +test('plugin installs correctly', () => { |
| 127 | + const app = createApp() |
| 128 | + |
| 129 | + expect(() => { |
| 130 | + app.use(MyPlugin, { enabled: true }) |
| 131 | + }).not.toThrow() |
| 132 | + |
| 133 | + expect(app.config.globalProperties.$myPlugin).toBeDefined() |
| 134 | +}) |
| 135 | +``` |
| 136 | + |
| 137 | +## Plugin Ecosystem |
| 138 | + |
| 139 | +### Official Plugins |
| 140 | + |
| 141 | +- `@stx/router` - Client-side routing |
| 142 | +- `@stx/state` - State management |
| 143 | +- `@stx/i18n` - Internationalization |
| 144 | +- `@stx/analytics` - Analytics integration |
| 145 | +- `@stx/auth` - Authentication |
| 146 | + |
| 147 | +### Community Guidelines |
| 148 | + |
| 149 | +Guidelines for community plugin development: |
| 150 | + |
| 151 | +1. Follow semantic versioning |
| 152 | +2. Include comprehensive tests |
| 153 | +3. Provide TypeScript definitions |
| 154 | +4. Document API and usage examples |
| 155 | +5. Follow STX coding standards |
| 156 | + |
| 157 | +## Related Resources |
| 158 | + |
| 159 | +- [Plugin API Reference](/api/plugins) - Complete plugin API documentation |
| 160 | +- [Component Development](/guide/components) - Building components for plugins |
| 161 | +- [State Management](/guide/state) - Integrating with STX state |
| 162 | +- [Configuration Guide](/guide/config) - Plugin configuration patterns |
0 commit comments