Skip to content

Commit e866a42

Browse files
committed
fix(plugin-alloy): Update meta hooks to use context field
1 parent ed4d804 commit e866a42

File tree

5 files changed

+391
-379
lines changed

5 files changed

+391
-379
lines changed

packages/plugin-alloy/src/core/contexts/meta.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
1717
------------------------------------------------------------------- */
1818

19-
import {
20-
ComponentContext,
21-
createNamedContext,
22-
useContext
23-
} from "@alloy-js/core";
19+
import { UNSAFE_AlloyPluginContext } from "@powerlines/plugin-alloy/types/_internal";
2420
import type { StoragePreset } from "powerlines/types/fs";
21+
import { usePowerlines } from "./context";
2522

2623
export interface MetaItem {
2724
/**
@@ -45,30 +42,18 @@ export interface MetaItem {
4542
[key: string]: any;
4643
}
4744

48-
/**
49-
* The Powerlines meta context used to determine metadata of files generated during rendering.
50-
*/
51-
export const MetaContext: ComponentContext<Record<string, MetaItem>> =
52-
createNamedContext<Record<string, MetaItem>>("Meta");
53-
54-
/**
55-
* Hook to access the Powerlines Context.
56-
*
57-
* @returns The Context.
58-
*/
59-
export function useMetaContext(): Record<string, MetaItem> | undefined {
60-
return useContext(MetaContext);
61-
}
62-
6345
/**
6446
* Hook to safely access the render context's metadata.
6547
*
6648
* @returns The Powerlines context or undefined if not set.
6749
*/
6850
export function useMetaSafe(): Record<string, MetaItem> | undefined {
69-
const meta = useMetaContext();
51+
const context = usePowerlines();
7052

71-
return meta ?? undefined;
53+
return (
54+
(context as unknown as UNSAFE_AlloyPluginContext).$$internal.meta.alloy ??
55+
undefined
56+
);
7257
}
7358

7459
/**

packages/plugin-alloy/src/index.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import rollupPlugin from "@alloy-js/rollup-plugin";
2222
import { StormJSON } from "@stryke/json/storm-json";
2323
import { findFileExtension } from "@stryke/path/file-path-fns";
2424
import { Plugin } from "powerlines/types/plugin";
25-
import { MetaContext } from "./core/contexts/meta";
2625
import { UNSAFE_AlloyPluginContext } from "./types/_internal";
2726
import { AlloyPluginContext, AlloyPluginOptions } from "./types/plugin";
2827

@@ -99,22 +98,18 @@ export const plugin = <
9998
const context = this as unknown as UNSAFE_AlloyPluginContext;
10099
context.$$internal.meta.alloy ??= {};
101100

102-
this.render = async (child: Children) => {
103-
const output = await renderAsync(
104-
<MetaContext.Provider value={context.$$internal.meta.alloy}>
105-
{child}
106-
</MetaContext.Provider>
107-
);
101+
this.render = async (children: Children) => {
102+
const output = await renderAsync(children);
108103

109104
if (!Object.keys(output).length) {
110105
this.debug(
111-
"No output files were rendered by Alloy-js templates."
106+
"No output files were rendered by Alloy-js component templates."
112107
);
113108
} else {
114109
this.debug(
115110
`Processing ${
116111
Object.keys(output).length
117-
} rendered output files from Alloy-js templates.`
112+
} rendered output files from Alloy-js component templates.`
118113
);
119114

120115
await traverseOutput(output, {
@@ -128,7 +123,8 @@ export const plugin = <
128123
visitFile: file => {
129124
if ("contents" in file) {
130125
const metadata =
131-
context.$$internal.meta.alloy[file.path] ?? {};
126+
(this as unknown as UNSAFE_AlloyPluginContext).$$internal
127+
.meta.alloy[file.path] ?? {};
132128
if (metadata.kind === "builtin") {
133129
if (!metadata.id) {
134130
throw new Error(

packages/plugin-alloy/src/types/plugin.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,36 @@ export type AlloyPluginResolvedConfig = BabelPluginResolvedConfig & {
5757
export type AlloyPluginContext<
5858
TResolvedConfig extends AlloyPluginResolvedConfig = AlloyPluginResolvedConfig
5959
> = PluginContext<TResolvedConfig> & {
60-
render: (child: Children) => Promise<void>;
60+
/**
61+
* A function to render children components within the [Alloy](https://alloy-framework.github.io) context, and write any saved content to the file system.
62+
*
63+
* @remarks
64+
* If the {@link children} provided to this function use the {@link PowerlinesContext} (accessible using the {@link usePowerlines} hook), it is very important that they are wrapped by the {@link Output} component. This ensures that the Powerlines context is properly provided to all child components during rendering. Since [\@alloy-js/core](https://alloy-framework.github.io) uses symbols to resolve context identifiers, failing to use the {@link Output} component will likely lead to unexpected behavior or errors during the rendering process.
65+
*
66+
* @example
67+
* ```tsx
68+
* import alloy from "@powerlines/plugin-alloy";
69+
* import { Output } from "@powerlines/plugin-alloy/core/components/output";
70+
*
71+
* export const plugin = () => {
72+
* return [
73+
* alloy(),
74+
* {
75+
* name: "my-plugin",
76+
* async prepare() {
77+
* await this.render(
78+
* <Output context={this}>
79+
* ...
80+
* </Output>
81+
* );
82+
* }
83+
* }
84+
* ];
85+
* };
86+
* ```
87+
*
88+
* @param children - The children components to render.
89+
* @returns A promise that resolves when rendering is complete.
90+
*/
91+
render: (children: Children) => Promise<void>;
6192
};

0 commit comments

Comments
 (0)