Skip to content

Commit

Permalink
Fix/lazy compilation (#1253)
Browse files Browse the repository at this point in the history
* fix: lazy compilation concurrency issue

* fix: lazy compilation concurrency issue

---------

Co-authored-by: brightwwu <[email protected]>
  • Loading branch information
wre232114 and brightwwu authored Apr 26, 2024
1 parent 5b75ec2 commit 492353f
Show file tree
Hide file tree
Showing 28 changed files with 245 additions and 255 deletions.
7 changes: 7 additions & 0 deletions .changeset/eight-walls-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@farmfe/runtime-plugin-hmr': patch
'@farmfe/runtime': patch
'@farmfe/core': patch
---

fix: lazy compilation concurrency issue
1 change: 1 addition & 0 deletions crates/compiler/examples/react_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn main() {
)],
|| {},
false,
true,
)
.unwrap();
}
12 changes: 9 additions & 3 deletions crates/compiler/src/build/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ pub fn get_timestamp_of_module(module_id: &ModuleId, root: &str) -> u128 {
module_id.resolved_path(root)
)
});
file_meta
.modified()
.unwrap()
let system_time = file_meta.modified();

if let Ok(system_time) = system_time {
if let Ok(dur) = system_time.duration_since(SystemTime::UNIX_EPOCH) {
return dur.as_nanos();
}
}

SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos()
Expand Down
23 changes: 7 additions & 16 deletions crates/compiler/src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Compiler {
paths: Vec<(String, UpdateType)>,
callback: F,
sync: bool,
generate_update_resource: bool,
) -> Result<UpdateResult>
where
F: FnOnce() + Send + Sync + 'static,
Expand Down Expand Up @@ -262,8 +263,10 @@ impl Compiler {
});
let (immutable_resources, mutable_resources) = if should_reload_page {
("window.location.reload()".to_string(), "{}".to_string())
} else {
} else if generate_update_resource {
render_and_generate_update_resource(&updated_module_ids, &diff_result, &self.context)?
} else {
("{}".to_string(), "{}".to_string())
};

// find the boundaries.
Expand Down Expand Up @@ -573,25 +576,13 @@ impl Compiler {
let resource_pot_map = self.context.resource_pot_map.read();
let resources_map = self.context.resources_map.lock();
let module_graph = self.context.module_graph.read();
let html_entries_ids = module_graph
.entries
.clone()
.into_iter()
.filter_map(|(m, _)| {
let module = module_graph.module(&m).unwrap();
if matches!(module.module_type, ModuleType::Html) {
Some(m)
} else {
None
}
})
.collect::<Vec<_>>();

let mut dynamic_resources = HashMap::new();

for html_entry_id in html_entries_ids {
for entry_id in module_graph.entries.keys() {
dynamic_resources.extend(get_dynamic_resources_map(
&module_group_graph,
&html_entry_id,
entry_id,
&resource_pot_map,
&resources_map,
));
Expand Down
13 changes: 9 additions & 4 deletions crates/compiler/tests/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn update_without_dependencies_change() {
.to_string_lossy()
.to_string();
let result = compiler
.update(vec![(update_file, UpdateType::Updated)], || {}, true)
.update(vec![(update_file, UpdateType::Updated)], || {}, true, true)
.unwrap();

assert_eq!(result.added_module_ids.len(), 0);
Expand Down Expand Up @@ -165,6 +165,7 @@ fn update_without_dependencies_change_css() {
vec![(update_file.clone(), UpdateType::Updated)],
|| {},
true,
true,
)
.unwrap();

Expand All @@ -175,7 +176,7 @@ fn update_without_dependencies_change_css() {
asset_update_result_code(cwd.clone(), &result, Some("update1"));

let result = compiler
.update(vec![(update_file, UpdateType::Updated)], || {}, false)
.update(vec![(update_file, UpdateType::Updated)], || {}, false, true)
.unwrap();

assert_eq!(result.added_module_ids.len(), 0);
Expand Down Expand Up @@ -230,6 +231,7 @@ fn update_with_dependencies_change_css_modules() {
vec![(update_file.clone(), UpdateType::Updated)],
|| {},
true,
true,
)
.unwrap();
assert_eq!(result.added_module_ids.len(), 2,);
Expand Down Expand Up @@ -257,6 +259,7 @@ fn update_with_dependencies_change_css_modules() {
vec![(update_file_css.clone(), UpdateType::Updated)],
|| {},
true,
true,
)
.unwrap();
assert_eq!(
Expand All @@ -278,7 +281,7 @@ fn update_with_dependencies_change_css_modules() {
let mut original_ts_file = File::create(&update_file).unwrap();
original_ts_file.write_all(original_ts.as_bytes()).unwrap();
let result = compiler
.update(vec![(update_file, UpdateType::Updated)], || {}, false)
.update(vec![(update_file, UpdateType::Updated)], || {}, false, true)
.unwrap();

assert_eq!(result.added_module_ids.len(), 0);
Expand Down Expand Up @@ -316,7 +319,7 @@ fn update_css_and_css_raw() {
.to_string();

let result = compiler
.update(vec![(update_file, UpdateType::Updated)], || {}, true)
.update(vec![(update_file, UpdateType::Updated)], || {}, true, true)
.unwrap();

assert_eq!(
Expand Down Expand Up @@ -359,6 +362,7 @@ fn update_lazy_compilation() {
vec![(update_module_id.clone(), UpdateType::Updated)],
|| {},
true,
true,
)
.unwrap();

Expand Down Expand Up @@ -395,6 +399,7 @@ fn update_lazy_compilation_node() {
vec![(update_module_id.clone(), UpdateType::Updated)],
|| {},
true,
true,
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/cache/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use crate::config::Mode;

const FARM_CACHE_VERSION: &str = "0.4.0";
const FARM_CACHE_VERSION: &str = "0.4.1";
const FARM_CACHE_MANIFEST_FILE: &str = "farm-cache.json";

// TODO make CacheStore a trait and implement DiskCacheStore or RemoteCacheStore or more.
Expand Down
13 changes: 13 additions & 0 deletions crates/core/src/config/custom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::sync::Arc;

use crate::context::CompilationContext;

const CUSTOM_CONFIG_RUNTIME_ISOLATE: &str = "runtime.isolate";

pub fn get_config_runtime_isolate(context: &Arc<CompilationContext>) -> bool {
if let Some(val) = context.config.custom.get(CUSTOM_CONFIG_RUNTIME_ISOLATE) {
val == "true"
} else {
false
}
}
1 change: 1 addition & 0 deletions crates/core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub const FARM_MODULE_EXPORT: &str = "exports";
pub mod bool_or_obj;
pub mod comments;
pub mod config_regex;
pub mod custom;
pub mod html;
pub mod minify;
pub mod partial_bundling;
Expand Down
2 changes: 2 additions & 0 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ impl JsCompiler {
paths: Vec<String>,
callback: JsFunction,
sync: bool,
generate_update_resource: bool,
) -> napi::Result<JsObject> {
let context = self.compiler.context().clone();
let thread_safe_callback: ThreadsafeFunction<(), ErrorStrategy::Fatal> =
Expand All @@ -289,6 +290,7 @@ impl JsCompiler {
thread_safe_callback.call((), ThreadsafeFunctionCallMode::Blocking);
},
sync,
generate_update_resource
)
.map_err(|e| napi::Error::new(Status::GenericFailure, format!("{}", e)))
{
Expand Down
132 changes: 51 additions & 81 deletions crates/plugin_html/src/resources_injector.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::{collections::HashMap, sync::Arc};

use farmfe_core::{
config::{Mode, FARM_MODULE_SYSTEM},
config::{custom::get_config_runtime_isolate, Mode, FARM_MODULE_SYSTEM},
context::CompilationContext,
module::ModuleId,
resource::{self, Resource, ResourceType},
serde_json,
resource::{Resource, ResourceType},
swc_html_ast::{Child, Document, Element},
};
use farmfe_toolkit::{
Expand Down Expand Up @@ -75,38 +74,28 @@ impl ResourcesInjector {

// Support isolate runtime resource (https://github.com/farm-fe/farm/issues/434)
fn inject_runtime_resources(&mut self, element: &mut Element) {
let inline_farm_entry_script = self
.options
.context
.config
.custom
.get("disabledInlineScript");

match inline_farm_entry_script {
Some(value) if value == "true" => {
let resource = create_farm_runtime_output_resource(
self.runtime_code.clone().into_bytes(),
FARM_RUNTIME_INJECT_RESOURCE,
&self.options.context,
);
let script_element = create_element(
"script",
None,
vec![
(FARM_ENTRY, "true"),
("src", &format!("/{}", resource.name)),
],
);
element.children.push(Child::Element(script_element));
self.additional_inject_resources.push(resource.clone());
}
_ => {
element.children.push(Child::Element(create_element(
"script",
Some(&self.runtime_code),
vec![(FARM_ENTRY, "true")],
)));
}
if get_config_runtime_isolate(&self.options.context) {
let resource = create_farm_runtime_output_resource(
self.runtime_code.clone().into_bytes(),
FARM_RUNTIME_INJECT_RESOURCE,
&self.options.context,
);
let script_element = create_element(
"script",
None,
vec![
(FARM_ENTRY, "true"),
("src", &format!("/{}", resource.name)),
],
);
element.children.push(Child::Element(script_element));
self.additional_inject_resources.push(resource.clone());
} else {
element.children.push(Child::Element(create_element(
"script",
Some(&self.runtime_code),
vec![(FARM_ENTRY, "true")],
)));
}
}

Expand Down Expand Up @@ -140,37 +129,28 @@ impl ResourcesInjector {
r#"{}.{}.setDynamicModuleResourcesMap({});"#,
self.farm_global_this, FARM_MODULE_SYSTEM, dynamic_resources_code
);
let inline_farm_entry_script = self
.options
.context
.config
.custom
.get("disabledInlineScript");

match inline_farm_entry_script {
Some(value) if value == "true" => {
let resource = create_farm_runtime_output_resource(
finalize_code.into_bytes(),
"dynamic_resources_map",
&self.options.context,
);
element.children.push(Child::Element(create_element(
"script",
None,
vec![
("FARM_ENTRY", "true"),
("src", &format!("/{}", resource.name)),
],
)));
self.additional_inject_resources.push(resource);
}
_ => {
element.children.push(Child::Element(create_element(
"script",
Some(&finalize_code),
vec![(FARM_ENTRY, "true")],
)));
}
if get_config_runtime_isolate(&self.options.context) {
let resource = create_farm_runtime_output_resource(
finalize_code.into_bytes(),
"dynamic_resources_map",
&self.options.context,
);
element.children.push(Child::Element(create_element(
"script",
None,
vec![
("FARM_ENTRY", "true"),
("src", &format!("/{}", resource.name)),
],
)));
self.additional_inject_resources.push(resource);
} else {
element.children.push(Child::Element(create_element(
"script",
Some(&finalize_code),
vec![(FARM_ENTRY, "true")],
)));
}
}

Expand Down Expand Up @@ -319,22 +299,12 @@ impl VisitMut for ResourcesInjector {
self.inject_initial_loaded_resources(element);
self.inject_dynamic_resources_map(element);

let inline_farm_entry_script = self
.options
.context
.config
.custom
.get("disabledInlineScript");

match inline_farm_entry_script {
Some(value) if value == "true" => {
self.inject_resource_separate_file(element);
}
_ => {
self.inject_other_entry_file(element);
}
};
}
if get_config_runtime_isolate(&self.options.context) {
self.inject_resource_separate_file(element);
} else {
self.inject_other_entry_file(element);
}
};

element.visit_mut_children_with(self);
}
Expand Down
Loading

0 comments on commit 492353f

Please sign in to comment.