Skip to main content

bext_plugin_api/
transform.rs

1//! Source-code transform plugin trait for the build pipeline.
2//! Plugins can rewrite file contents at compile time (e.g. macro expansion,
3//! code injection) and run in priority order alongside built-in transforms.
4
5use crate::types::transform_priority;
6
7/// Context provided to transform plugins (read-only view of build state).
8#[derive(Debug, Clone)]
9pub struct TransformContext {
10    /// Current NODE_ENV value.
11    pub node_env: String,
12    /// NEXT_PUBLIC_ environment variables as (key, value) pairs.
13    pub env_vars: Vec<(String, String)>,
14}
15
16/// A plugin that hooks into the source-code transform pipeline.
17///
18/// **Compile-time only** — no WASM overhead on every file.
19///
20/// Built-in transforms run at priorities 100–700. Plugin transforms default
21/// to priority 1000 (after all built-ins). Lower priority runs first.
22pub trait TransformPlugin: Send + Sync {
23    /// Unique identifier (e.g., `"my-macro-expansion"`).
24    fn name(&self) -> &str;
25
26    /// Execution priority. Lower runs first. Default: 1000 (after built-ins).
27    fn priority(&self) -> u32 {
28        transform_priority::PLUGIN_DEFAULT
29    }
30
31    /// Return `true` if this plugin should process the file at `path`.
32    /// Called before `transform()` to skip unnecessary work.
33    fn matches(&self, path: &str, source: &str) -> bool;
34
35    /// Transform the source code.
36    ///
37    /// Returns `Some(new_source)` if modified, `None` if unchanged.
38    /// Receives the current source which may already be modified by
39    /// earlier transforms in the pipeline.
40    fn transform(&self, source: &str, path: &str, ctx: &TransformContext) -> Option<String>;
41}