-
-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add freeze_module hooks #2092
Open
ErKeLost
wants to merge
13
commits into
farm-fe:v2-dev
Choose a base branch
from
ErKeLost:chore/freeze_module_hooks
base: v2-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fa050bf
feat: add js plugin freeze_modules hooks
ErKeLost 1368186
chore: remove unless code
ErKeLost a37e329
chore: update code
ErKeLost 1d227da
Merge branch 'v2-dev' into chore/freeze_module_hooks
ErKeLost cdea468
chore: remove format
ErKeLost 6e502fe
Merge branch 'chore/freeze_module_hooks' of https://github.com/ErKeLo…
ErKeLost 8601ab1
chore: update code
ErKeLost 9bdcca0
chore: update types
ErKeLost 7f9c74d
chore: update code
ErKeLost fa154c8
Merge branch 'v2-dev' into chore/freeze_module_hooks
ErKeLost 3015ce9
chore: update code
ErKeLost b6c650e
chore: update code
ErKeLost b070d85
chore: update code
ErKeLost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
258 changes: 258 additions & 0 deletions
258
crates/node/src/plugin_adapters/js_plugin_adapter/hooks/freeze_module.rs
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,258 @@ | ||
use std::sync::Arc; | ||
|
||
use farmfe_core::{ | ||
config::config_regex::ConfigRegex, | ||
context::CompilationContext, | ||
error::{CompilationError, Result}, | ||
module::{ModuleId, ModuleMetaData, ModuleType}, | ||
plugin::PluginFreezeModuleHookParam, | ||
serde::{Deserialize, Serialize}, | ||
swc_common::comments::SingleThreadedComments, | ||
swc_ecma_ast::EsVersion, | ||
swc_ecma_parser::{EsSyntax, Syntax}, | ||
}; | ||
use farmfe_toolkit::{ | ||
css::{codegen_css_stylesheet, parse_css_stylesheet, ParseCssModuleResult}, | ||
html::{codegen_html_document, parse_html_document}, | ||
script::{codegen_module, parse_module, CodeGenCommentsConfig, ParseScriptModuleResult}, | ||
}; | ||
use napi::{bindgen_prelude::FromNapiValue, NapiRaw}; | ||
|
||
use crate::{ | ||
new_js_plugin_hook, | ||
plugin_adapters::js_plugin_adapter::thread_safe_js_plugin_hook::ThreadSafeJsPluginHook, | ||
}; | ||
|
||
#[napi(object)] | ||
pub struct JsPluginFreezeModuleHookFilters { | ||
pub module_types: Vec<String>, | ||
pub resolved_paths: Vec<String>, | ||
} | ||
|
||
pub struct PluginFreezeModuleHookFilters { | ||
pub module_types: Vec<ModuleType>, | ||
pub resolved_paths: Vec<ConfigRegex>, | ||
} | ||
|
||
impl From<JsPluginFreezeModuleHookFilters> for PluginFreezeModuleHookFilters { | ||
fn from(value: JsPluginFreezeModuleHookFilters) -> Self { | ||
Self { | ||
module_types: value.module_types.into_iter().map(|ty| ty.into()).collect(), | ||
resolved_paths: value | ||
.resolved_paths | ||
.into_iter() | ||
.map(|p| ConfigRegex::new(&p)) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
pub struct JsPluginFreezeModuleHook { | ||
tsfn: ThreadSafeJsPluginHook, | ||
filters: PluginFreezeModuleHookFilters, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(crate = "farmfe_core::serde", rename_all = "camelCase")] | ||
pub struct PluginFreezeModuleHookResult { | ||
content: String, | ||
source_map: Option<String>, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(crate = "farmfe_core::serde", rename_all = "camelCase")] | ||
struct CompatiblePluginFreezeModuleHookParams { | ||
module_id: ModuleId, | ||
module_type: ModuleType, | ||
content: String, | ||
} | ||
|
||
fn format_module_metadata_to_code( | ||
param: &mut PluginFreezeModuleHookParam, | ||
context: &Arc<CompilationContext>, | ||
) -> Result<Option<String>> { | ||
let source_map_enabled = !context.config.sourcemap.is_false(); | ||
|
||
Ok(match &mut *param.module.meta { | ||
ModuleMetaData::Script(ref mut script_module_meta_data) => { | ||
let cm = context.meta.get_module_source_map(¶m.module.id); | ||
let mut src_map = vec![]; | ||
let comments = std::mem::take(&mut script_module_meta_data.comments); | ||
let single_threaded_comments = SingleThreadedComments::from(comments); | ||
|
||
let code = codegen_module( | ||
&script_module_meta_data.ast, | ||
EsVersion::latest(), | ||
cm.clone(), | ||
if source_map_enabled { | ||
Some(&mut src_map) | ||
} else { | ||
None | ||
}, | ||
false, | ||
Some(CodeGenCommentsConfig { | ||
comments: &single_threaded_comments, | ||
config: &context.config.comments, | ||
}), | ||
) | ||
.map_err(|err| CompilationError::GenericError(err.to_string()))?; | ||
// write back the comments | ||
script_module_meta_data.comments = single_threaded_comments.into(); | ||
// append source map | ||
if source_map_enabled { | ||
let map = cm.build_source_map(&src_map); | ||
let mut src_map = vec![]; | ||
map.to_writer(&mut src_map).map_err(|err| { | ||
CompilationError::GenericError(format!("failed to write source map: {err:?}")) | ||
})?; | ||
param | ||
.module | ||
.source_map_chain | ||
.push(Arc::new(String::from_utf8(src_map).unwrap())); | ||
} | ||
|
||
Some(String::from_utf8_lossy(&code).to_string()) | ||
} | ||
ModuleMetaData::Css(ref mut css_module_meta_data) => { | ||
let cm = context.meta.get_module_source_map(¶m.module.id); | ||
let (code, map) = codegen_css_stylesheet( | ||
&css_module_meta_data.ast, | ||
false, | ||
if source_map_enabled { | ||
Some(cm.clone()) | ||
} else { | ||
None | ||
}, | ||
); | ||
|
||
if let Some(map) = map { | ||
param.module.source_map_chain.push(Arc::new(map)); | ||
} | ||
|
||
Some(code) | ||
} | ||
ModuleMetaData::Html(ref mut html_module_meta_data) => { | ||
Some(codegen_html_document(&html_module_meta_data.ast, false)) | ||
} | ||
ModuleMetaData::Custom(_) => None, | ||
}) | ||
} | ||
|
||
fn convert_code_to_metadata( | ||
params: &mut PluginFreezeModuleHookParam, | ||
code: Arc<String>, | ||
source_map: Option<String>, | ||
context: &Arc<CompilationContext>, | ||
) -> Result<()> { | ||
params.module.content = code.clone(); | ||
|
||
if let Some(source_map) = source_map { | ||
params.module.source_map_chain.push(Arc::new(source_map)); | ||
} | ||
|
||
let filename = params.module.id.to_string(); | ||
|
||
match &mut *params.module.meta { | ||
ModuleMetaData::Script(ref mut script_module_meta_data) => { | ||
let ParseScriptModuleResult { | ||
ast, | ||
comments, | ||
source_map, | ||
} = parse_module( | ||
¶ms.module.id, | ||
code, | ||
match params.module.module_type { | ||
ModuleType::Js | ModuleType::Ts => Syntax::Es(Default::default()), | ||
ModuleType::Jsx | ModuleType::Tsx => Syntax::Es(EsSyntax { | ||
jsx: true, | ||
..Default::default() | ||
}), | ||
_ => Syntax::Es(Default::default()), | ||
}, | ||
Default::default(), | ||
)?; | ||
|
||
context | ||
.meta | ||
.set_module_source_map(¶ms.module.id, source_map); | ||
|
||
script_module_meta_data.ast = ast; | ||
script_module_meta_data.comments = comments.into() | ||
} | ||
ModuleMetaData::Css(ref mut css_module_meta_data) => { | ||
let ParseCssModuleResult { | ||
ast, | ||
comments, | ||
source_map, | ||
} = parse_css_stylesheet(&filename, code)?; | ||
|
||
context | ||
.meta | ||
.set_module_source_map(¶ms.module.id, source_map); | ||
|
||
css_module_meta_data.ast = ast; | ||
css_module_meta_data.comments = comments.into(); | ||
} | ||
ModuleMetaData::Html(ref mut html_module_meta_data) => { | ||
let v = parse_html_document(&filename, code)?; | ||
|
||
html_module_meta_data.ast = v; | ||
} | ||
ModuleMetaData::Custom(_) => { | ||
return Ok(()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
impl JsPluginFreezeModuleHook { | ||
new_js_plugin_hook!( | ||
PluginFreezeModuleHookFilters, | ||
JsPluginFreezeModuleHookFilters, | ||
CompatiblePluginFreezeModuleHookParams, | ||
PluginFreezeModuleHookResult | ||
); | ||
|
||
pub fn call( | ||
&self, | ||
param: &mut PluginFreezeModuleHookParam, | ||
ctx: Arc<CompilationContext>, | ||
) -> Result<Option<()>> { | ||
if self | ||
.filters | ||
.module_types | ||
.contains(¶m.module.module_type) | ||
|| self | ||
.filters | ||
.resolved_paths | ||
.iter() | ||
.any(|m| m.is_match(param.module.id.to_string().as_str())) | ||
{ | ||
let Some(result) = format_module_metadata_to_code(param, &ctx)? else { | ||
return Ok(None); | ||
}; | ||
|
||
let Some(result) = self | ||
.tsfn | ||
.call::<CompatiblePluginFreezeModuleHookParams, PluginFreezeModuleHookResult>( | ||
CompatiblePluginFreezeModuleHookParams { | ||
module_id: param.module.id.clone(), | ||
module_type: param.module.module_type.clone(), | ||
content: result, | ||
}, | ||
ctx.clone(), | ||
None, | ||
)? | ||
else { | ||
return Ok(None); | ||
}; | ||
|
||
convert_code_to_metadata(param, Arc::new(result.content), result.source_map, &ctx)?; | ||
|
||
return Ok(None); | ||
} | ||
|
||
Ok(None) | ||
} | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can extract this method as a util which can be shared between different hook