-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve rs plugin and js hooks (#855)
* feat: more js hooks * docs: update docs/config.md * chore: code style * chore: code style * docs: update * chore: on_generate_file > _on_generate_file * chore: code style
- Loading branch information
Showing
17 changed files
with
508 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::sync::{mpsc, Arc}; | ||
|
||
use crate::threadsafe_function; | ||
use crate::tsfn::{LoadResult, ReadMessage, TsFnHooks, WriteRequest}; | ||
|
||
pub struct JsPlugin { | ||
pub hooks: TsFnHooks, | ||
} | ||
use mako::compiler::Context; | ||
use mako::load::Content; | ||
use mako::plugin::{Plugin, PluginGenerateEndParams, PluginLoadParam}; | ||
use mako_core::anyhow::{anyhow, Result}; | ||
|
||
impl Plugin for JsPlugin { | ||
fn name(&self) -> &str { | ||
"js_plugin" | ||
} | ||
|
||
fn build_start(&self, _context: &Arc<Context>) -> Result<Option<()>> { | ||
if let Some(hook) = &self.hooks.build_start { | ||
let (tx, rx) = mpsc::channel::<napi::Result<()>>(); | ||
hook.call( | ||
ReadMessage { message: (), tx }, | ||
threadsafe_function::ThreadsafeFunctionCallMode::Blocking, | ||
); | ||
rx.recv() | ||
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?; | ||
} | ||
Ok(None) | ||
} | ||
|
||
fn load(&self, param: &PluginLoadParam, _context: &Arc<Context>) -> Result<Option<Content>> { | ||
if let Some(hook) = &self.hooks.load { | ||
let (tx, rx) = mpsc::channel::<napi::Result<Option<LoadResult>>>(); | ||
hook.call( | ||
ReadMessage { | ||
message: param.task.request.path.clone(), | ||
tx, | ||
}, | ||
threadsafe_function::ThreadsafeFunctionCallMode::Blocking, | ||
); | ||
let x = rx | ||
.recv() | ||
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?; | ||
if let Some(x) = x { | ||
match x.content_type.as_str() { | ||
"js" => return Ok(Some(Content::Js(x.content))), | ||
"css" => return Ok(Some(Content::Css(x.content))), | ||
_ => return Err(anyhow!("Unsupported content type: {}", x.content_type)), | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
fn generate_end( | ||
&self, | ||
param: &PluginGenerateEndParams, | ||
_context: &Arc<Context>, | ||
) -> Result<Option<()>> { | ||
if let Some(hook) = &self.hooks.generate_end { | ||
let (tx, rx) = mpsc::channel::<napi::Result<()>>(); | ||
hook.call( | ||
ReadMessage { | ||
message: param.clone(), | ||
tx, | ||
}, | ||
threadsafe_function::ThreadsafeFunctionCallMode::Blocking, | ||
); | ||
rx.recv() | ||
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?; | ||
} | ||
Ok(None) | ||
} | ||
|
||
fn before_write_fs(&self, path: &std::path::Path, content: &[u8]) -> Result<()> { | ||
if let Some(hook) = &self.hooks._on_generate_file { | ||
let (tx, rx) = mpsc::channel::<napi::Result<()>>(); | ||
hook.call( | ||
WriteRequest { | ||
path: path.to_path_buf(), | ||
content: content.to_vec(), | ||
tx, | ||
}, | ||
threadsafe_function::ThreadsafeFunctionCallMode::Blocking, | ||
); | ||
rx.recv() | ||
.unwrap_or_else(|e| panic!("recv error: {:?}", e.to_string()))?; | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.