-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathbase.py
More file actions
270 lines (204 loc) Β· 8.09 KB
/
Copy pathbase.py
File metadata and controls
270 lines (204 loc) Β· 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""Base class for all plugins."""
from collections.abc import Callable, Sequence
from enum import Enum
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, ParamSpec, Protocol, TypedDict
from typing_extensions import Unpack
class HookOrder(str, Enum):
"""Dispatch bucket for a compiler ``enter_component`` / ``leave_component`` hook."""
PRE = "pre"
NORMAL = "normal"
POST = "post"
if TYPE_CHECKING:
from reflex.app import App, UnevaluatedPage
from reflex_base.components.component import BaseComponent
from reflex_base.plugins.compiler import ComponentAndChildren, PageContext
class CommonContext(TypedDict):
"""Common context for all plugins."""
P = ParamSpec("P")
class AddTaskProtocol(Protocol):
"""Protocol for adding a task to the pre-compile context."""
def __call__(
self,
task: Callable[P, list[tuple[str, str]] | tuple[str, str] | None],
/,
*args: P.args,
**kwargs: P.kwargs,
) -> None:
"""Add a task to the pre-compile context.
Args:
task: The task to add.
args: The arguments to pass to the task
kwargs: The keyword arguments to pass to the task
"""
class PreCompileContext(CommonContext):
"""Context for pre-compile hooks."""
add_save_task: AddTaskProtocol
add_modify_task: Callable[[str, Callable[[str], str]], None]
radix_themes_plugin: Any
unevaluated_pages: Sequence["UnevaluatedPage"]
class PostCompileContext(CommonContext):
"""Context for post-compile hooks."""
app: "App"
class PostBuildContext(CommonContext):
"""Context for post-build hooks."""
static_dir: Path
class Plugin:
"""Base class for all plugins."""
# Dispatch position for ``enter_component`` and ``leave_component`` hooks.
# Plugins run in ``PRE`` β ``NORMAL`` β ``POST`` order. Within a bucket,
# enter hooks fire in plugin-chain order while leave hooks fire in
# reverse plugin-chain order (mirroring an enter/leave stack).
_compiler_enter_component_order: ClassVar[HookOrder] = HookOrder.NORMAL
_compiler_leave_component_order: ClassVar[HookOrder] = HookOrder.NORMAL
def get_frontend_development_dependencies(
self, **context: Unpack[CommonContext]
) -> list[str] | set[str] | tuple[str, ...]:
"""Get the NPM packages required by the plugin for development.
Args:
context: The context for the plugin.
Returns:
A list of packages required by the plugin for development.
"""
return []
def get_frontend_dependencies(
self, **context: Unpack[CommonContext]
) -> list[str] | set[str] | tuple[str, ...]:
"""Get the NPM packages required by the plugin.
Args:
context: The context for the plugin.
Returns:
A list of packages required by the plugin.
"""
return []
def get_static_assets(
self, **context: Unpack[CommonContext]
) -> Sequence[tuple[Path, str | bytes]]:
"""Get the static assets required by the plugin.
Args:
context: The context for the plugin.
Returns:
A list of static assets required by the plugin.
"""
return []
def get_stylesheet_paths(self, **context: Unpack[CommonContext]) -> Sequence[str]:
"""Get the paths to the stylesheets required by the plugin relative to the styles directory.
Args:
context: The context for the plugin.
Returns:
A list of paths to the stylesheets required by the plugin.
"""
return []
def pre_compile(self, **context: Unpack[PreCompileContext]) -> None:
"""Called before the compilation of the plugin.
Args:
context: The context for the plugin.
"""
def post_compile(self, **context: Unpack[PostCompileContext]) -> None:
"""Called after the compilation of the plugin.
Runs at most once per app instance β even when the ASGI app is
constructed repeatedly, and even when another plugin's failed hook
forces a retry β so hooks may mutate the app (e.g.
``add_middleware``) without their own idempotency guard.
Args:
context: The context for the plugin.
"""
def post_build(self, **context: Unpack[PostBuildContext]) -> None:
"""Called after the production frontend build finishes.
Fires after ``npm run export`` so plugins can inspect or post-process
the Vite output (``context["static_dir"]``).
Args:
context: The context for the plugin.
"""
def provides_entry_client(self) -> bool:
"""Return whether this plugin emits its own ``entry.client.js``.
The framework calls this during ``setup_frontend`` to decide whether
to skip its default ``entry.client.js`` write. Plugins that register
a save task for the same path should return ``True`` so the framework
write doesn't race with (or overwrite) theirs.
Returns:
``True`` if the plugin owns ``entry.client.js`` for this build.
"""
return False
def update_env_json(
self, **context: Unpack[CommonContext]
) -> dict[str, Any] | None:
"""Return entries to merge into ``.web/env.json``.
The framework merges each plugin's contribution on top of the base
``env.json`` it writes during ``setup_frontend``. Later plugins
override earlier ones. Return ``None`` (the default) to contribute
nothing.
Args:
context: The context for the plugin.
Returns:
A mapping of env entries to add or override, or ``None``.
"""
return None
def eval_page(
self,
page_fn: Any,
/,
**kwargs: Any,
) -> "PageContext | None":
"""Evaluate a page-like object into a page context.
Args:
page_fn: The page-like object to evaluate.
kwargs: Additional compiler-specific context.
Returns:
A page context when the plugin can evaluate the page, otherwise ``None``.
"""
return None
def compile_page(
self,
page_ctx: "PageContext",
/,
**kwargs: Any,
) -> None:
"""Finalize a page context after its component tree has been traversed."""
return
def enter_component(
self,
comp: "BaseComponent",
/,
*,
page_context: "PageContext",
compile_context: Any,
in_prop_tree: bool = False,
) -> "BaseComponent | ComponentAndChildren | None":
"""Inspect or transform a component before visiting its descendants.
Args:
comp: The component being compiled.
page_context: The active page compilation state.
compile_context: The active compile-run state.
in_prop_tree: Whether the component is being visited through a prop subtree.
Returns:
An optional replacement component and/or structural children.
"""
return None
def leave_component(
self,
comp: "BaseComponent",
children: tuple["BaseComponent", ...],
/,
*,
page_context: "PageContext",
compile_context: Any,
in_prop_tree: bool = False,
) -> "BaseComponent | ComponentAndChildren | None":
"""Inspect or transform a component after visiting its descendants.
Args:
comp: The component being compiled.
children: The compiled structural children for the component.
page_context: The active page compilation state.
compile_context: The active compile-run state.
in_prop_tree: Whether the component is being visited through a prop subtree.
Returns:
An optional replacement component and/or structural children.
"""
return None
def __repr__(self):
"""Return a string representation of the plugin.
Returns:
A string representation of the plugin.
"""
return f"{self.__class__.__name__}()"