-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: move ensure runtime to entry #1660
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f8ff46c
feat: ✨ add ensure2 replace in dynamic replace
stormslowly 2d42631
feat: ✨ ensure 2 runtime
stormslowly 364f24e
Merge remote-tracking branch 'origin/master' into feat/ensure_pr
stormslowly ef7b57d
Merge remote-tracking branch 'origin/master' into feat/ensure_pr
stormslowly d1ef84a
feat: ✨ add experimental centralEnsure config
stormslowly 7fe969d
refactor: 🎨 add struct ReslvedReplaceInfo
stormslowly 1abdb37
feat: ✨ add central ensure plugin
stormslowly beae8ce
refactor: 🎨 extract module ensure map
stormslowly 8cef532
refactor: 🎨 add back chunk id in replace info
stormslowly 8609332
refactor: 🎨 fix case one source with different import type
stormslowly 61bedb0
refactor: 🎨 extract hmr runtime update code aspect
stormslowly 0f7e63a
release: @umijs/[email protected]
stormslowly 3c79e02
chore: [email protected]
stormslowly ae1b8b7
Merge remote-tracking branch 'origin/master' into feat/ensure_pr
stormslowly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,64 @@ | ||
use std::collections::BTreeMap; | ||
use std::sync::Arc; | ||
|
||
use crate::compiler::Context; | ||
use crate::generate::chunk::ChunkType; | ||
use crate::plugin::Plugin; | ||
|
||
pub struct Ensure2 {} | ||
|
||
impl Plugin for Ensure2 { | ||
fn name(&self) -> &str { | ||
"dev_ensure2" | ||
} | ||
fn runtime_plugins(&self, _context: &Arc<Context>) -> anyhow::Result<Vec<String>> { | ||
let cg = _context.chunk_graph.read().unwrap(); | ||
|
||
let mut chunk_async_map: BTreeMap<String, Vec<String>> = BTreeMap::new(); | ||
|
||
cg.get_chunks() | ||
.into_iter() | ||
.filter(|chunk| chunk.chunk_type == ChunkType::Async) | ||
.for_each(|chunk| { | ||
let chunk_ids = { | ||
[ | ||
cg.sync_dependencies_chunk(&chunk.id), | ||
vec![chunk.id.clone()], | ||
] | ||
.concat() | ||
.iter() | ||
.filter_map(|chunk_id| { | ||
// skip empty chunk because it will not be generated | ||
if cg.chunk(chunk_id).is_some_and(|c| !c.modules.is_empty()) { | ||
Some(chunk_id.id.clone()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>() | ||
}; | ||
chunk_async_map.insert(chunk.id.id.clone(), chunk_ids); | ||
}); | ||
stormslowly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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.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 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 |
---|---|---|
|
@@ -14,7 +14,6 @@ use crate::ast::utils::{is_dynamic_import, promise_all, require_ensure}; | |
use crate::ast::DUMMY_CTXT; | ||
use crate::compiler::Context; | ||
use crate::generate::chunk::ChunkId; | ||
use crate::module::generate_module_id; | ||
use crate::visitors::dep_replacer::DependenciesToReplace; | ||
|
||
pub struct DynamicImport<'a> { | ||
|
@@ -75,6 +74,7 @@ impl<'a> VisitMut for DynamicImport<'a> { | |
if call_expr.args.is_empty() { | ||
return; | ||
} | ||
|
||
if let ExprOrSpread { | ||
expr: box Expr::Lit(Lit::Str(ref mut source)), | ||
.. | ||
|
@@ -106,8 +106,9 @@ impl<'a> VisitMut for DynamicImport<'a> { | |
// it must be resolved, so unwrap is safe here. | ||
.unwrap(); | ||
|
||
let chunk_ids = { | ||
let chunk_id: ChunkId = resolved_info.0.as_str().into(); | ||
let resolved_source = &resolved_info.0; | ||
let _chunk_ids = { | ||
let chunk_id: ChunkId = resolved_source.clone().into(); | ||
let chunk_graph = &self.context.chunk_graph.read().unwrap(); | ||
let chunk = chunk_graph.chunk(&chunk_id); | ||
let chunk_ids = match chunk { | ||
|
@@ -142,35 +143,23 @@ impl<'a> VisitMut for DynamicImport<'a> { | |
// e.g. | ||
// Promise.all([ require.ensure("id") ]).then(require.bind(require, "id")) | ||
// Promise.all([ require.ensure("d1"), require.ensure("id)]).then(require.bind(require, "id")) | ||
*expr = { | ||
let to_ensure_elems = chunk_ids | ||
.iter() | ||
.map(|c_id| { | ||
Some(ExprOrSpread { | ||
spread: None, | ||
expr: Box::new(require_ensure(c_id.clone())), | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
let load_promise = promise_all(ExprOrSpread { | ||
spread: None, | ||
expr: ArrayLit { | ||
span: DUMMY_SP, | ||
elems: to_ensure_elems, | ||
} | ||
.into(), | ||
}); | ||
|
||
let require_module = generate_module_id(&resolved_info.1, &self.context); | ||
// version 2 | ||
// require.ensure2("id").then(require.bind(require,"id")) | ||
|
||
*expr = { | ||
// let load_promise = self.make_load_promise(&chunk_ids); | ||
let load_promise = self.make_load_promise_2(resolved_source); | ||
|
||
let lazy_require_call = | ||
member_expr!(DUMMY_CTXT, DUMMY_SP, __mako_require__.bind).as_call( | ||
DUMMY_SP, | ||
vec![ | ||
quote_ident!("__mako_require__").as_arg(), | ||
quote_str!(require_module.as_str()).as_arg(), | ||
quote_str!(resolved_source.clone()).as_arg(), | ||
], | ||
); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 优化新表达式的构建过程 在构建新的表达式时,可以考虑简化 |
||
let dr_call = member_expr!(DUMMY_CTXT, DUMMY_SP, __mako_require__.dr) | ||
.as_call( | ||
DUMMY_SP, | ||
|
@@ -187,6 +176,33 @@ impl<'a> VisitMut for DynamicImport<'a> { | |
} | ||
} | ||
|
||
impl DynamicImport<'_> { | ||
fn make_load_promise_2(&self, module_id: &str) -> Expr { | ||
member_expr!(DUMMY_CTXT, DUMMY_SP, __mako_require__.ensure2) | ||
.as_call(DUMMY_SP, vec![quote_str!(module_id).as_arg()]) | ||
} | ||
|
||
fn make_load_promise(&self, chunk_ids: &[String]) -> Expr { | ||
let to_ensure_elems = chunk_ids | ||
.iter() | ||
.map(|c_id| { | ||
Some(ExprOrSpread { | ||
spread: None, | ||
expr: Box::new(require_ensure(c_id.clone())), | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
promise_all(ExprOrSpread { | ||
spread: None, | ||
expr: ArrayLit { | ||
span: DUMMY_SP, | ||
elems: to_ensure_elems, | ||
} | ||
.into(), | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个 e2e? 看下 entry 被改成什么样了