You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ee733bb
Browse filesBrowse the repository at this point in the historyBrowse files
Merge pull request #38 from PureStackStudio/improve-plugin-architecture
Move integration-specific configuration into its owning plugins and add optional image optimization through @purestack/ts-images. Preserve existing default site output while making plugin configuration and asset lifecycle contracts explicit.
Copy file name to clipboardExpand all lines: frontend/purestack.studio/guides/extending/plugins.mdx
+86-4Lines changed: 86 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,49 @@ A plugin is a named object that extends how PureStack builds your site. Each of
29
29
30
30
## Add your first plugin
31
31
32
+
To use an installed plugin without writing TypeScript, list its package in
33
+
`siteConfig.json`. For example, install `@purestack/ts-images`, then enable image
34
+
optimization:
35
+
36
+
```json
37
+
{
38
+
"plugins": ["@purestack/ts-images#imagesPlugin"],
39
+
"extensions": {
40
+
"images": { "widths": [480, 960, 1440] }
41
+
}
42
+
}
43
+
```
44
+
45
+
Use `package#exportName` to select a named export, for example
46
+
`"@purestack/ts-images#imagesPlugin"`. Without a suffix, PureStack selects the
47
+
default export. Each selected export must be a plugin object or a no-argument
48
+
factory. Packages can expose multiple plugins; list each export separately:
49
+
50
+
```json
51
+
{
52
+
"plugins": [
53
+
"@purestack/ts-page-scripts#consentPlugin",
54
+
"@purestack/ts-page-scripts#analyticsPlugin"
55
+
]
56
+
}
57
+
```
58
+
59
+
The suffix is the JavaScript export name, not the plugin's `name`. Package
60
+
subpaths also work: `package/subpath#exportName`. PureStack loads selections for
61
+
builds, publishing, and development, and reloads them when this JSON file changes.
62
+
Each plugin owns its settings; listing `imagesPlugin` enables
63
+
its defaults even without `extensions.images`. Set `extensions.images.enabled`
64
+
to `false` to disable optimization. An explicit plugin instance in
65
+
`purestack.config.ts` replaces a JSON-selected plugin with the same name.
66
+
67
+
Both config formats use PureStack's own copy of a package when available so
68
+
shared registries stay together; other packages resolve from the site.
69
+
Plugin declarations execute installed JavaScript with the build process's
70
+
permissions, so treat changes to `plugins` as code changes even when a CMS edits
71
+
other site settings.
72
+
73
+
For local plugins and custom callbacks, use the TypeScript setup below.
74
+
32
75
Keep plugins in a folder of their own, beside the content folder:
33
76
34
77
```text
@@ -111,7 +154,23 @@ Metadata defaults compose through `headConfig({ site, pageInfo })` in plugin ord
111
154
112
155
Auth hints, consent, and analytics are independent plugins in `ts-page-scripts`. The analytics plugin contributes automatic GA4 consent services during configuration resolution, before any factory or lifecycle hook runs. Omitting it removes automatic registration and direct tracking scripts. Explicitly configured consent services remain available. Analytics never bypasses enabled consent when the consent runtime plugin is omitted.
113
156
114
-
To choose the standard integrations yourself, disable the automatic set and list the plugins you want. The factories are exported from `purestack`, `@purestack/ts-ssg`, and their individual packages:
157
+
To choose the standard integrations yourself, disable the automatic set and list the plugins you want in `siteConfig.json`:
158
+
159
+
```json
160
+
{
161
+
"defaultPlugins": false,
162
+
"plugins": [
163
+
"@purestack/ts-theme#themePlugin",
164
+
"@purestack/ts-components#componentsPlugin"
165
+
]
166
+
}
167
+
```
168
+
169
+
Omitting `defaultPlugins` keeps the automatic set enabled. An explicit
170
+
TypeScript/programmatic `defaultPlugins` value overrides the JSON flag.
171
+
Development reloads changes to either setting.
172
+
173
+
The same selection is available in `purestack.config.ts`. The factories are exported from `purestack`, `@purestack/ts-ssg`, and their individual packages:
@@ -132,10 +191,14 @@ The Pagefind plugin supplies a `search` provider. To use another engine, replace
132
191
133
192
## Configure a plugin
134
193
135
-
Plugins contribute configuration through `config.resolve({ config, input, file })` and `config.finalize({ config, input, file })`. Both are optional and synchronous. Each returns an object to merge into the config; do not mutate the inputs. All resolvers run first in plugin order, applying configuration overrides. Then all finalizers run in plugin order, deriving dependent settings from the completed overrides. Finalizers should derive settings rather than change resolver inputs. Both phases finish before highlighters, Markdown processors, styles, search providers, components, and templates initialize. The analytics plugin finalizes its automatic GA4 service here, so later site resolvers can enable analytics, change its ID or category, or provide a manual service. The existing `onConfigResolved` hook still runs after factories, so it can customize initialized components and templates. Publishing applies its output-directory and minification overrides after configuration composition.
194
+
`resolveSiteConfig(input)` returns `DefaultSiteConfig` for the untouched standard set. Calls supplying a `plugins` array return `SiteConfig`, even when automatic defaults remain enabled, because a supplied plugin can replace a standard owner by name.
195
+
196
+
Plugins contribute configuration through optional, synchronous `config.resolve`, `config.normalize`, and `config.finalize` functions, each receiving `{ config, input, file }` and returning an object to merge into config. Do not mutate the inputs. All resolvers run first in plugin order to supply defaults and overrides. All normalizers then validate and canonicalize the completed overrides. Finally, all finalizers derive dependent settings from the normalized config. Finalizers should not change the inputs used by other finalizers. Every phase finishes before highlighters, Markdown processors, styles, search providers, components, and templates initialize. Analytics derives its automatic GA4 consent service only after every normalizer finishes, regardless of plugin order. The existing `onConfigResolved` hook still runs after factories. Publishing applies its output-directory and minification overrides after configuration composition.
136
197
137
198
Custom site settings belong under `extensions` in `siteConfig.json`. File values merge with programmatic input, which takes precedence. The standard JSON schema allows plugin namespaces here while continuing to reject unknown top-level fields. Each plugin owns validation, defaults, and normalization for its namespace. Other namespaces are preserved.
138
199
200
+
`resolve` and `normalize` receive `SiteConfigDraft`: core fields are ready, while plugin settings can still be partial, including nested extension values. `finalize` receives normalized `SiteConfig` after all selected owners finish normalization. Factories run afterwards. Importing a plugin's setting types does not install its owner; select that plugin before depending on its normalized settings.
Raw `resolveSiteConfig(input)`resolves core settings and extension input. CLI and programmatic builds additionally compose their selected plugins' configuration contributions. Configuration contributions are synchronous and may run whenever a project configuration is resolved; keep file writes and other side effects in lifecycle hooks.
237
+
`resolveSiteConfig(input, { plugins, defaultPlugins })`composes the supplied plugin instances with the standard plugins by default. This synchronous helper does not apply JSON plugin selection (`plugins` or `defaultPlugins`) or load `purestack.config.ts`. Plugin-owned sections (`style`, `docLayout`, `pageToc`, `logo`, `auth`, `consent`, `analytics`, `pagefind`, and `preview`) are optional in `SiteConfig`: an omitted plugin does not supply defaults or validate its unused input. A replacement plugin reads its settings from `input`and `file`, then contributes normalized config. Analytics also resolves explicitly configured consent when its browser runtime is omitted, so enabled consent is never bypassed. Configuration contributions are synchronous and may run whenever a project configuration is resolved; keep file writes and other side effects in lifecycle hooks.
175
238
176
239
Builder style state is isolated across asynchronous operations, including concurrent sites. Its owned theme options remain linked to `context.config.style.theme`, so in-place config-hook changes and `themes.setOptions` update page colors, CSS, and generated favicons consistently. Head, metadata, template, Markdown, provider, and lifecycle errors identify their owning plugin and preserve the original cause. Markdown wrappers preserve unified presets, duplicate-plugin option merging, and callback transforms.
177
240
241
+
## Optimize images
242
+
243
+
Image optimization is optional. Install `@purestack/ts-images` alongside PureStack and add it to your existing plugin list:
Ordinary Markdown images and rendered HTML images receive responsive AVIF/WebP sources, JPEG/PNG fallbacks, dimensions, and loading defaults. Originals remain available for links and downloads. Remote images, SVGs, animations, existing `picture` markup, and images with `srcset` pass through. Use `data-image-preset="hero"` for eager priority images, `data-image-preset="screenshot"` for lossless output, or `data-image-optimize="false"` to keep an individual image unchanged. Set `sizes` for your actual image layout.
255
+
256
+
The plugin owns its `extensions.images` settings, persistent cache, and encoder limits. It uses the generic asset contribution: `assets(config)` returns a per-site processor with a unique namespace and `processPage(input)`. This runs after all `onPageDocument` hooks, before URL rewriting. `resolveSource(element, url)` resolves local inputs and registers dependencies; `dependOn(relPath)` registers additional inputs; `emit(fileName, bytes)` atomically writes an owned file and returns its URL. Use content-addressed names and await all writes. Core handles base paths, incremental invalidation, shared references, manifest persistence, and cleanup when pages or processors disappear. The builder contains no image format or encoder selection.
257
+
258
+
Asset processors run in order within each page, and different page requests can run them concurrently. Keep page-local state inside `processPage`; coordinate shared caches and queues within the processor instance. Await source resolution and emission before returning. Input methods then close, and detached work cannot emit files after page completion. Later processors see earlier DOM edits, while `resolveSource` continues to resolve original content files only. Combine transforms inside one processor when they need intermediate generated bytes. Ownership receipts and processor instances remain private to the builder.
259
+
178
260
## How the config loads
179
261
180
262
-**Where it lives.** The CLI looks for `purestack.config.ts` in the folder passed to `--content`. It must default-export `defineConfig({ plugins: [...] })`.
@@ -586,7 +668,7 @@ Every field except `name` is optional.
586
668
|`name`|`string`, unique among the site's plugins |
587
669
|`defaults`|`boolean`; makes component and template contributions overridable defaults without changing initialization order |
588
670
|`skins`| Skins by name, each `{ create(): ThemeSkinPair }`|
589
-
|`config`|`{ resolve?: SiteConfigResolver, finalize?: SiteConfigResolver}`; overrides, then derived settings, before factories |
671
+
|`config`|`{ resolve?: SiteConfigResolver, normalize?: SiteConfigResolver, finalize?: SiteConfigFinalizer }`; overrides, normalization, then derived settings, before factories |
0 commit comments