-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move ensure runtime to entry (#1660)
* feat: ✨ add ensure2 replace in dynamic replace * feat: ✨ ensure 2 runtime * feat: ✨ add experimental centralEnsure config * refactor: 🎨 add struct ReslvedReplaceInfo * feat: ✨ add central ensure plugin * refactor: 🎨 extract module ensure map * refactor: 🎨 add back chunk id in replace info * refactor: 🎨 fix case one source with different import type * refactor: 🎨 extract hmr runtime update code aspect * release: @umijs/[email protected] * chore: [email protected]
- Loading branch information
1 parent
dd22d96
commit 4d645ad
Showing
12 changed files
with
308 additions
and
123 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
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,90 @@ | ||
use std::collections::BTreeMap; | ||
use std::sync::Arc; | ||
|
||
use anyhow::anyhow; | ||
|
||
use crate::compiler::Context; | ||
use crate::module::generate_module_id; | ||
use crate::plugin::Plugin; | ||
|
||
pub struct CentralChunkEnsure {} | ||
|
||
pub fn module_ensure_map(context: &Arc<Context>) -> anyhow::Result<BTreeMap<String, Vec<String>>> { | ||
let mg = context | ||
.module_graph | ||
.read() | ||
.map_err(|e| anyhow!("Read_Module_Graph_error:\n{:?}", e))?; | ||
let cg = context | ||
.chunk_graph | ||
.read() | ||
.map_err(|e| anyhow!("Read_Chunk_Graph_error:\n{:?}", e))?; | ||
|
||
let mut chunk_async_map: BTreeMap<String, Vec<String>> = Default::default(); | ||
|
||
mg.modules().iter().for_each(|module| { | ||
let be_dynamic_imported = mg | ||
.get_dependents(&module.id) | ||
.iter() | ||
.any(|(_, dep)| dep.resolve_type.is_dynamic_esm()); | ||
|
||
if be_dynamic_imported { | ||
cg.get_async_chunk_for_module(&module.id) | ||
.iter() | ||
.for_each(|chunk| { | ||
let deps_chunks = cg | ||
.installable_descendants_chunk(&chunk.id) | ||
.iter() | ||
.map(|chunk_id| chunk_id.generate(context)) | ||
.collect::<Vec<_>>(); | ||
|
||
chunk_async_map.insert(generate_module_id(&module.id.id, context), deps_chunks); | ||
}); | ||
} | ||
}); | ||
|
||
Ok(chunk_async_map) | ||
} | ||
|
||
impl Plugin for CentralChunkEnsure { | ||
fn name(&self) -> &str { | ||
"dev_ensure2" | ||
} | ||
fn runtime_plugins(&self, context: &Arc<Context>) -> anyhow::Result<Vec<String>> { | ||
let chunk_async_map = module_ensure_map(context)?; | ||
|
||
// TODO: compress the map to reduce duplicated chunk ids | ||
let ensure_map = serde_json::to_string(&chunk_async_map)?; | ||
|
||
let runtime = format!( | ||
r#" | ||
(function(){{ | ||
let map = {ensure_map}; | ||
requireModule.updateEnsure2Map = function(newMapping) {{ | ||
map = newMapping; | ||
}} | ||
requireModule.ensure2 = function(chunkId){{ | ||
let toEnsure = map[chunkId]; | ||
if (toEnsure) {{ | ||
return Promise.all(toEnsure.map(function(c){{ return requireModule.ensure(c); }})) | ||
}}else{{ | ||
return Promise.resolve(); | ||
}} | ||
}}; | ||
}})(); | ||
"# | ||
); | ||
|
||
Ok(vec![runtime]) | ||
} | ||
|
||
fn hmr_runtime_updates(&self, _context: &Arc<Context>) -> anyhow::Result<Vec<String>> { | ||
let map = module_ensure_map(_context)?; | ||
|
||
let update_mapping = format!( | ||
"runtime.updateEnsure2Map({});", | ||
serde_json::to_string(&map)? | ||
); | ||
|
||
Ok(vec![update_mapping]) | ||
} | ||
} |
Oops, something went wrong.