-
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.
- Loading branch information
1 parent
7fe969d
commit 1abdb37
Showing
4 changed files
with
81 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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 {} | ||
|
||
impl Plugin for CentralChunkEnsure { | ||
fn name(&self) -> &str { | ||
"dev_ensure2" | ||
} | ||
fn runtime_plugins(&self, context: &Arc<Context>) -> anyhow::Result<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_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<_>>(); | ||
|
||
dbg!(&deps_chunks); | ||
|
||
chunk_async_map | ||
.insert(generate_module_id(&module.id.id, context), deps_chunks); | ||
}); | ||
} | ||
}); | ||
|
||
// 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.updateEnsuire2Map = function(newMaping) {{ | ||
map = newMaping; | ||
}} | ||
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]) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.