Skip to content

Commit

Permalink
feat: ✨ 区分 ssu firstbuild 和 updating 两者状态
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Nov 13, 2024
1 parent 20f751c commit e316d9f
Showing 1 changed file with 117 additions and 31 deletions.
148 changes: 117 additions & 31 deletions crates/mako/src/plugins/ssu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::config::{
use crate::generate::chunk::ChunkType;
use crate::generate::chunk_pot::util::{hash_hashmap, hash_vec};
use crate::generate::generate_chunks::{ChunkFile, ChunkFileType};
use crate::generate::transform::transform_modules;
use crate::module::ModuleId;
use crate::plugin::{NextBuildParam, Plugin, PluginLoadParam};
use crate::resolve::ResolverResource;

Expand Down Expand Up @@ -64,9 +66,17 @@ impl CacheState {
}
}

#[derive(Debug, Copy, Clone, Default)]

Check warning on line 69 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L69

Added line #L69 was not covered by tests
enum SSUScanStage {
#[default]
FirstBuild,

Check warning on line 72 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L72

Added line #L72 was not covered by tests
Updating,
}

pub struct SUPlus {
scanning: Arc<Mutex<bool>>,
enabled: Arc<Mutex<bool>>,
stage: Arc<Mutex<SSUScanStage>>,
cache_valid: Arc<Mutex<bool>>,
will_full_rebuild: Arc<Mutex<bool>>,
dependence_node_module_files: DashSet<File>,
cached_state: Arc<Mutex<CacheState>>,
current_state: Arc<Mutex<CacheState>>,
Expand All @@ -89,13 +99,15 @@ impl From<bool> for CodeType {
}

const SSU_ENTRY_PREFIX: &str = "virtual:ssu:entry:node_modules:";
const SSU_MOCK_CSS_FILE: &str = "virtual:C:/node_modules/css/css.css";
const SSU_MOCK_CSS_FILE: &str = "virtual:C:/node_modules/_mako_css/css.css";
const SSU_MOCK_JS_FILE: &str = "virtual:C:/node_modules/_mako_js/js.js";

impl SUPlus {
pub fn new() -> Self {
SUPlus {
scanning: Arc::new(Mutex::new(true)),
enabled: Arc::new(Mutex::new(true)),
stage: Arc::new(Mutex::new(Default::default())),
cache_valid: Arc::new(Mutex::new(true)),
will_full_rebuild: Arc::new(Mutex::new(false)),

Check warning on line 110 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L108-L110

Added lines #L108 - L110 were not covered by tests
dependence_node_module_files: Default::default(),
cached_state: Default::default(),
current_state: Default::default(),
Expand Down Expand Up @@ -127,25 +139,29 @@ impl SUPlus {
alias_hash.wrapping_add(external_hash)
}

fn start_scan(&self) {
let mut s = self.scanning.lock().unwrap();
*s = true;
fn in_building_stage(&self) {
let mut s = self.stage.lock().unwrap();
*s = SSUScanStage::FirstBuild;

Check warning on line 144 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L142-L144

Added lines #L142 - L144 were not covered by tests
}

fn stop_scan(&self) {
let mut s = self.scanning.lock().unwrap();
*s = false;
fn in_updating_stage(&self) {
let mut s = self.stage.lock().unwrap();
*s = SSUScanStage::Updating;

Check warning on line 149 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L147-L149

Added lines #L147 - L149 were not covered by tests
}

fn enable_cache(&self) {
let mut e = self.enabled.lock().unwrap();
let mut e = self.cache_valid.lock().unwrap();

Check warning on line 153 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L153

Added line #L153 was not covered by tests
*e = true;
}

fn disable_cache(&self) {
let mut e = self.enabled.lock().unwrap();
let mut e = self.cache_valid.lock().unwrap();

Check warning on line 158 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L158

Added line #L158 was not covered by tests
*e = false;
}

fn will_full_rebuild(&self) -> bool {
*self.will_full_rebuild.lock().unwrap()
}

Check warning on line 164 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L162-L164

Added lines #L162 - L164 were not covered by tests
}

impl Plugin for SUPlus {
Expand Down Expand Up @@ -227,14 +243,22 @@ impl Plugin for SUPlus {
let port = context.config.dev_server.as_ref().unwrap().port.to_string();
let host = &context.config.dev_server.as_ref().unwrap().host;
let host = if host == "0.0.0.0" { "127.0.0.1" } else { host };
let hmr_runtime = include_str!("../runtime/runtime_hmr_entry.js")
.to_string()
.replace("__PORT__", &port)
.replace("__HOST__", host);
let hmr_runtime = if context.config.hmr.is_some() {
include_str!("../runtime/runtime_hmr_entry.js")

Check warning on line 247 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L246-L247

Added lines #L246 - L247 were not covered by tests
.to_string()
.replace("__PORT__", &port)
.replace("__HOST__", host)
} else {
"".to_string()

Check warning on line 252 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L249-L252

Added lines #L249 - L252 were not covered by tests
};

let content = format!(
r#"
require("{SSU_MOCK_CSS_FILE}");
try{{
// it will throw due to the node_module chunk is not loaded yet
require("{SSU_MOCK_JS_FILE}");
}}catch(e){{}};
let patch = require._su_patch();
console.log(patch);
{}
Expand Down Expand Up @@ -263,6 +287,14 @@ module.export = Promise.all(
if param.file.path.starts_with(SSU_MOCK_CSS_FILE) {
return Ok(Some(Content::Css("._mako_mock_css { }".to_string())));
}

if param.file.path.starts_with(SSU_MOCK_JS_FILE) {
return Ok(Some(Content::Js(JsContent {

Check warning on line 292 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L291-L292

Added lines #L291 - L292 were not covered by tests
is_jsx: false,
content: "console.log('_mako_ssu_placeholder')".to_string(),

Check warning on line 294 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L294

Added line #L294 was not covered by tests
})));
}

Ok(None)
}

Expand All @@ -284,7 +316,7 @@ module.export = Promise.all(
.to_string()
);

match (from, to) {
let should_transform = match (from, to) {

Check warning on line 319 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L319

Added line #L319 was not covered by tests
(CodeType::SourceCode, CodeType::Dependency) => {
if let ResolverResource::Resolved(resolved) = &next_build_param.resource {
self.dependence_node_module_files
Expand All @@ -304,14 +336,38 @@ module.export = Promise.all(
v.as_str().unwrap_or("0.0.0").to_string()
});

self.current_state
.lock()
.unwrap()
.cached_boundaries
.insert(path_name, version);
let mut ssu_state = self.current_state.lock().unwrap();

Check warning on line 339 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L339

Added line #L339 was not covered by tests

let stage = self.stage.lock().unwrap();

Check warning on line 341 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L341

Added line #L341 was not covered by tests

let scanning = *self.scanning.lock().unwrap();
!scanning
match *stage {

Check warning on line 343 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L343

Added line #L343 was not covered by tests
SSUScanStage::FirstBuild => {
ssu_state.cached_boundaries.insert(path_name, version);
false

Check warning on line 346 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L345-L346

Added lines #L345 - L346 were not covered by tests
}
SSUScanStage::Updating => {
let mut cache_valid = self.cache_valid.lock().unwrap();

Check warning on line 349 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L349

Added line #L349 was not covered by tests

if *cache_valid {

Check warning on line 351 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L351

Added line #L351 was not covered by tests
// cache hit
if let Some(cached_version) =
ssu_state.cached_boundaries.get(&path_name)
&& *cached_version == version

Check warning on line 355 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L353-L355

Added lines #L353 - L355 were not covered by tests
{
false

Check warning on line 357 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L357

Added line #L357 was not covered by tests
} else {
ssu_state.cached_boundaries.insert(path_name, version);
*cache_valid = false;
*self.will_full_rebuild.lock().unwrap() = true;

Check warning on line 361 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L359-L361

Added lines #L359 - L361 were not covered by tests

true

Check warning on line 363 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L363

Added line #L363 was not covered by tests
}
} else {
ssu_state.cached_boundaries.insert(path_name, version);
true

Check warning on line 367 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L366-L367

Added lines #L366 - L367 were not covered by tests
}
}

Check warning on line 369 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L369

Added line #L369 was not covered by tests
}
} else {
true
}
Expand All @@ -337,7 +393,37 @@ module.export = Promise.all(
true
}
_ => true,
};

debug!(

Check warning on line 398 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L398

Added line #L398 was not covered by tests
"{} -> {} {should_transform}",
next_build_param.current_module.id,
next_build_param
.next_file
.pathname
.to_string_lossy()
.to_string()
);

should_transform
}

Check warning on line 409 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L408-L409

Added lines #L408 - L409 were not covered by tests

fn after_update(&self, compiler: &Compiler) -> Result<()> {
if self.will_full_rebuild() {
let files = self

Check warning on line 413 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L411-L413

Added lines #L411 - L413 were not covered by tests
.dependence_node_module_files
.iter()
.map(|f| f.clone())

Check warning on line 416 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L416

Added line #L416 was not covered by tests
.collect::<Vec<File>>();

debug!("start to build after update");
let mut modules = compiler.build(files.clone())?;

Check warning on line 420 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L419-L420

Added lines #L419 - L420 were not covered by tests

modules.extend(files.into_iter().map(|f| ModuleId::from(f.path)));

Check warning on line 422 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L422

Added line #L422 was not covered by tests

transform_modules(modules.into_iter().collect(), &compiler.context)?

Check warning on line 424 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L424

Added line #L424 was not covered by tests
}
Ok(())

Check warning on line 426 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L426

Added line #L426 was not covered by tests
}

fn after_build(&self, _context: &Arc<Context>, compiler: &Compiler) -> Result<()> {
Expand All @@ -360,6 +446,7 @@ module.export = Promise.all(

if cache_valid {
self.enable_cache();
self.in_updating_stage();

Check warning on line 449 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L449

Added line #L449 was not covered by tests
return Ok(());
}

Expand All @@ -371,12 +458,10 @@ module.export = Promise.all(
.map(|f| f.clone())
.collect::<Vec<File>>();

self.stop_scan();

debug!("start to build dep");
compiler.build(files)?;

self.start_scan();
self.in_updating_stage();

Check warning on line 464 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L464

Added line #L464 was not covered by tests

#[cfg(debug_assertions)]
{
Expand All @@ -397,7 +482,8 @@ module.export = Promise.all(
chunk_files: &[ChunkFile],
context: &Arc<Context>,
) -> Result<()> {
if *self.enabled.lock().unwrap() {
if *self.cache_valid.lock().unwrap() {
debug!("cache valid skip generate chunk files");

Check warning on line 486 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L485-L486

Added lines #L485 - L486 were not covered by tests
return Ok(());
}

Expand Down Expand Up @@ -470,7 +556,7 @@ module.export = Promise.all(
}

fn runtime_plugins(&self, _context: &Arc<Context>) -> Result<Vec<String>> {
if *self.enabled.lock().unwrap() {
if *self.cache_valid.lock().unwrap() {

Check warning on line 559 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L559

Added line #L559 was not covered by tests
let cache = self.cached_state.lock().unwrap();

let code = format!(
Expand Down Expand Up @@ -499,7 +585,7 @@ requireModule._su_patch = function(){{
.into_iter()
.filter(|c| c.chunk_type == ChunkType::Sync)
.for_each(|c| {
println!("chunk: {}", c.filename());
debug!("chunk: {}", c.filename());

Check warning on line 588 in crates/mako/src/plugins/ssu.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/ssu.rs#L588

Added line #L588 was not covered by tests
});

Ok(vec![r#"
Expand Down

0 comments on commit e316d9f

Please sign in to comment.