Skip to content

Commit

Permalink
chore: bundle adapting ast merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shulandmimi committed Nov 29, 2024
1 parent 932f789 commit fa15d99
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 150 deletions.
84 changes: 38 additions & 46 deletions crates/plugin_bundle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use farmfe_core::{
regex::Regex,
relative_path::RelativePath,
resource::{
meta_data::ResourcePotMetaData,
meta_data::{js::JsResourcePotMetaData, ResourcePotMetaData},
resource_pot::{ResourcePot, ResourcePotType},
ResourceType,
ResourceOrigin, ResourceType,
},
swc_ecma_ast::Module,
};
use farmfe_toolkit::constant::RUNTIME_SUFFIX;
use resource_pot_to_bundle::{
Expand All @@ -37,8 +38,8 @@ pub mod resource_pot_to_bundle;

#[derive(Default)]
pub struct FarmPluginBundle {
runtime_code: Mutex<Arc<String>>,
bundle_map: Mutex<HashMap<String, Bundle>>,
runtime_code: Mutex<Arc<Module>>,
bundle_map: Mutex<HashMap<String, Module>>,
resource_pot_id_resource_map: Mutex<HashMap<String, String>>,
}

Expand Down Expand Up @@ -137,9 +138,10 @@ impl Plugin for FarmPluginBundle {
resource_pots: &mut Vec<&mut ResourcePot>,
context: &Arc<CompilationContext>,
) -> farmfe_core::error::Result<Option<()>> {
if !self.runtime_code.lock().is_empty() {
if !self.runtime_code.lock().body.is_empty() {
return Ok(None);
}

let module_graph = context.module_graph.read();

resource_pots.sort_by_key(|item| item.id.clone());
Expand All @@ -164,32 +166,23 @@ impl Plugin for FarmPluginBundle {

shared_bundle.render()?;

let mut defer_minify = vec![];
for resource_pot in resource_pots.iter() {
if matches!(resource_pot.resource_pot_type, ResourcePotType::Runtime)
|| (context.config.output.target_env.is_library()
&& matches!(resource_pot.resource_pot_type, ResourcePotType::Js))
{
let resource_pot_id = resource_pot.id.clone();

let bundle = shared_bundle.codegen(&resource_pot_id)?;

defer_minify.push(resource_pot_id.clone());
let module = shared_bundle.codegen(&resource_pot_id)?;

if matches!(resource_pot.resource_pot_type, ResourcePotType::Runtime) {
*self.runtime_code.lock() = Arc::new(bundle.to_string());
*self.runtime_code.lock() = Arc::new(module);
} else {
self.bundle_map.lock().insert(resource_pot_id, bundle);
self.bundle_map.lock().insert(resource_pot_id, module);
}
}
}

// for resource_pot in resource_pots {
// if defer_minify.contains(&resource_pot.id) {
// resource_pot.defer_minify_as_resource_pot();
// }
// }

Ok(None)
}

Expand All @@ -202,42 +195,41 @@ impl Plugin for FarmPluginBundle {
Ok(None)
}

// fn render_resource_pot_modules(
// &self,
// resource_pot: &ResourcePot,
// _context: &Arc<CompilationContext>,
// _hook_context: &PluginHookContext,
// ) -> farmfe_core::error::Result<Option<ResourcePotMetaData>> {
// if matches!(resource_pot.resource_pot_type, ResourcePotType::Runtime) {
// return Ok(Some(ResourcePotMetaData {
// rendered_modules: HashMap::new(),
// rendered_content: self.runtime_code.lock().clone(),
// rendered_map_chain: vec![],
// custom_data: resource_pot.meta.custom_data.clone(),
// }));
// } else if let Some(bundle) = self.bundle_map.lock().get(&resource_pot.id) {
// return Ok(Some(ResourcePotMetaData {
// // TODO
// rendered_modules: HashMap::new(),
// rendered_content: Arc::new(bundle.to_string()),
// rendered_map_chain: vec![],
// custom_data: resource_pot.meta.custom_data.clone(),
// }));
// }

// Ok(None)
// }
fn render_update_resource_pot(
&self,
resource_pot: &ResourcePot,
_context: &Arc<CompilationContext>,
_hook_context: &PluginHookContext,
) -> farmfe_core::error::Result<Option<ResourcePotMetaData>> {
if matches!(resource_pot.resource_pot_type, ResourcePotType::Runtime) {
return Ok(Some(ResourcePotMetaData::Js(JsResourcePotMetaData {
ast: Arc::into_inner(self.runtime_code.lock().clone()).unwrap(),
comments: Default::default(),
external_modules: Default::default(),
rendered_modules: Default::default(),
})));
} else if let Some(bundle) = self.bundle_map.lock().remove(&resource_pot.id) {
return Ok(Some(ResourcePotMetaData::Js(JsResourcePotMetaData {
ast: bundle,
comments: Default::default(),
external_modules: Default::default(),
rendered_modules: Default::default(),
})));
}

Ok(None)
}

fn process_generated_resources(
&self,
resources: &mut PluginGenerateResourcesHookResult,
_context: &Arc<CompilationContext>,
) -> farmfe_core::error::Result<Option<()>> {
if let Some(resource_pot_id) = resources.resource.info.as_ref().map(|i| i.id.clone()) {
if let ResourceOrigin::ResourcePot(ref resource_pot_id) = resources.resource.origin {
self
.resource_pot_id_resource_map
.lock()
.insert(resource_pot_id, resources.resource.name.clone());
.insert(resource_pot_id.to_string(), resources.resource.name.clone());
}

Ok(None)
Expand All @@ -255,8 +247,8 @@ impl Plugin for FarmPluginBundle {
let mut map = HashMap::new();

for (name, resource) in param.resources_map.iter() {
if let Some(ref info) = resource.info {
map.insert(info.id.clone(), name.clone());
if let ResourceOrigin::ResourcePot(id) = &resource.origin {
map.insert(id.clone(), name.clone());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use farmfe_core::{
module::{module_graph::ModuleGraph, ModuleId, ModuleSystem},
plugin::ResolveKind,
resource::resource_pot::ResourcePotType,
swc_common::{comments::SingleThreadedComments, util::take::Take},
swc_common::{comments::SingleThreadedComments, util::take::Take, DUMMY_SP},
swc_ecma_ast::Module,
};
use farmfe_toolkit::{
script::{codegen_module, swc_try_with::try_with, CodeGenCommentsConfig},
Expand Down Expand Up @@ -1404,22 +1405,118 @@ impl<'a> BundleAnalyzer<'a> {
}

// step: 4 generate bundle code
pub fn codegen(
// pub fn codegen(
// &mut self,
// module_analyzer_manager: &mut ModuleAnalyzerManager,
// config: &Config,
// ) -> Result<Bundle> {
// let mut bundle = Bundle::new(BundleOptions {
// separator: Some('\n'),
// intro: None,
// trace_source_map_chain: Some(false),
// });

// for module_id in &self.ordered_modules {
// let module = self
// .module_graph
// .module(module_id)
// .unwrap_or_else(|| panic!("Module not found: {module_id:?}"));
// let module_analyzer = module_analyzer_manager.module_analyzer_mut_unchecked(module_id);

// let comments: SingleThreadedComments = module.meta.as_script().comments.clone().into();

// let sourcemap_enabled = self.context.config.sourcemap.enabled(module.immutable);

// try_with(
// module_analyzer.cm.clone(),
// &self.context.meta.script.globals,
// || {
// module_analyzer
// .ast
// .visit_mut_with(&mut fixer(Some(&comments)));
// },
// )?;

// let mut mappings = vec![];
// let code_bytes = codegen_module(
// &module_analyzer.ast,
// self.context.config.script.target,
// module_analyzer.cm.clone(),
// if sourcemap_enabled {
// Some(&mut mappings)
// } else {
// None
// },
// false,
// Some(CodeGenCommentsConfig {
// comments: &comments,
// config: &self.context.config.comments,
// }),
// )
// .map_err(|err| CompilationError::RenderScriptModuleError {
// id: module_analyzer.module_id.to_string(),
// source: Some(Box::new(err)),
// })?;

// let code = String::from_utf8(code_bytes).map_err(|err| {
// CompilationError::GenericError(format!(
// "failed to convert code bytes to string, origin error: {err}"
// ))
// })?;

// let mut source_map_chain = vec![];

// if sourcemap_enabled {
// let sourcemap = build_source_map(module_analyzer.cm.clone(), &mappings);
// let mut buf = vec![];
// sourcemap
// .to_writer(&mut buf)
// .map_err(|e| CompilationError::RenderScriptModuleError {
// id: module_id.to_string(),
// source: Some(Box::new(e)),
// })?;
// let map = Arc::new(String::from_utf8(buf).unwrap());

// source_map_chain.clone_from(&module.source_map_chain);
// source_map_chain.push(map);
// }

// let mut module = MagicString::new(
// &code,
// Some(MagicStringOptions {
// filename: Some(module_id.resolved_path_with_query(&self.context.config.root)),
// source_map_chain,
// ..Default::default()
// }),
// );

// if matches!(self.context.config.mode, Mode::Development) {
// // debug info
// module.prepend(&format!("// module_id: {}\n", module_id.to_string()));
// }

// bundle.add_source(module, None).unwrap();
// }

// // in browser, should avoid naming pollution
// if matches!(self.context.config.output.target_env, TargetEnv::Browser)
// && matches!(self.group.group_type, ResourcePotType::Runtime)
// {
// bundle.prepend(";((function(){");
// bundle.append("})());", None);
// };

// Ok(bundle)
// }

pub fn gen_ast(
&mut self,
module_analyzer_manager: &mut ModuleAnalyzerManager,
config: &Config,
) -> Result<Bundle> {
let mut bundle = Bundle::new(BundleOptions {
separator: Some('\n'),
intro: None,
trace_source_map_chain: Some(false),
});

let mut ordered_modules = self.ordered_modules.clone();

ordered_modules.sort_by_key(|v| module_analyzer_manager.is_commonjs(v));
) -> Result<Module> {
let mut bodys = vec![];

for module_id in &self.ordered_modules {
for module_id in self.ordered_modules.iter() {
let module = self
.module_graph
.module(module_id)
Expand All @@ -1428,87 +1525,26 @@ impl<'a> BundleAnalyzer<'a> {

let comments: SingleThreadedComments = module.meta.as_script().comments.clone().into();

let sourcemap_enabled = self.context.config.sourcemap.enabled(module.immutable);

try_with(
module_analyzer.cm.clone(),
&self.context.meta.script.globals,
|| {
module_analyzer
.ast
.visit_mut_with(&mut fixer(Some(&comments)));
},
)?;

let mut mappings = vec![];
let code_bytes = codegen_module(
&module_analyzer.ast,
self.context.config.script.target,
module_analyzer.cm.clone(),
if sourcemap_enabled {
Some(&mut mappings)
} else {
None
let ast = module_analyzer.ast.take();

bodys.extend(ast.body);
},
false,
Some(CodeGenCommentsConfig {
comments: &comments,
config: &self.context.config.comments,
}),
)
.map_err(|err| CompilationError::RenderScriptModuleError {
id: module_analyzer.module_id.to_string(),
source: Some(Box::new(err)),
})?;

let code = String::from_utf8(code_bytes).map_err(|err| {
CompilationError::GenericError(format!(
"failed to convert code bytes to string, origin error: {err}"
))
})?;

let mut source_map_chain = vec![];

if sourcemap_enabled {
let sourcemap = build_source_map(module_analyzer.cm.clone(), &mappings);
let mut buf = vec![];
sourcemap
.to_writer(&mut buf)
.map_err(|e| CompilationError::RenderScriptModuleError {
id: module_id.to_string(),
source: Some(Box::new(e)),
})?;
let map = Arc::new(String::from_utf8(buf).unwrap());

source_map_chain.clone_from(&module.source_map_chain);
source_map_chain.push(map);
}

let mut module = MagicString::new(
&code,
Some(MagicStringOptions {
filename: Some(module_id.resolved_path_with_query(&self.context.config.root)),
source_map_chain,
..Default::default()
}),
);

if matches!(self.context.config.mode, Mode::Development) {
// debug info
module.prepend(&format!("// module_id: {}\n", module_id.to_string()));
}

bundle.add_source(module, None).unwrap();
.unwrap();
}

// in browser, should avoid naming pollution
if matches!(self.context.config.output.target_env, TargetEnv::Browser)
&& matches!(self.group.group_type, ResourcePotType::Runtime)
{
bundle.prepend(";((function(){");
bundle.append("})());", None);
};

Ok(bundle)
Ok(Module {
span: DUMMY_SP,
body: bodys,
shebang: None,
})
}
}
Loading

0 comments on commit fa15d99

Please sign in to comment.