-
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
Changes from 7 commits
f8ff46c
2d42631
364f24e
ef7b57d
d1ef84a
7fe969d
1abdb37
beae8ce
8cef532
8609332
61bedb0
0f7e63a
3c79e02
ae1b8b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
stormslowly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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; | ||
stormslowly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}} | ||
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]) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,14 +23,17 @@ | |
pub unresolved_mark: Mark, | ||
} | ||
|
||
type ResolvedModuleId = String; | ||
type ResolvedModulePath = String; | ||
#[derive(Debug, Clone)] | ||
pub struct ResolvedReplaceInfo { | ||
pub to_replace_source: String, | ||
pub resolved_module_id: ModuleId, | ||
} | ||
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 建议为新结构体 新添加的结构体 🧰 Tools🪛 GitHub Check: codecov/patch[warning] 26-26: crates/mako/src/visitors/dep_replacer.rs#L26 [warning] 28-29: crates/mako/src/visitors/dep_replacer.rs#L28-L29 |
||
|
||
#[derive(Debug, Clone)] | ||
pub struct DependenciesToReplace { | ||
// resolved stores the "source" maps to (generate_id, raw_module_id) | ||
// e.g. "react" => ("hashed_id", "/abs/to/react/index.js") | ||
pub resolved: HashMap<String, (ResolvedModuleId, ResolvedModulePath)>, | ||
pub resolved: HashMap<String, ResolvedReplaceInfo>, | ||
pub missing: HashMap<String, Dependency>, | ||
} | ||
|
||
|
@@ -124,8 +127,9 @@ | |
// css | ||
// TODO: add testcases for this | ||
let is_replaceable_css = | ||
if let Some((_, raw_id)) = self.to_replace.resolved.get(&source_string) { | ||
let (path, _search, query, _) = parse_path(raw_id).unwrap(); | ||
if let Some(replace_info) = self.to_replace.resolved.get(&source_string) { | ||
let (path, _search, query, _) = | ||
parse_path(&replace_info.resolved_module_id.id).unwrap(); | ||
Comment on lines
+131
to
+133
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 建议处理 在第 132 行,使用了 建议修改为: - let (path, _search, query, _) = parse_path(&replace_info.resolved_module_id.id).unwrap();
+ if let Some((path, _search, query, _)) = parse_path(&replace_info.resolved_module_id.id) {
+ // 现有逻辑
+ } else {
+ // 适当地处理错误,例如记录日志或返回错误
+ }
|
||
// when inline_css is enabled | ||
// css is parsed as js modules | ||
self.context.config.inline_css.is_none() | ||
|
@@ -194,7 +198,7 @@ | |
impl DepReplacer<'_> { | ||
fn replace_source(&mut self, source: &mut Str) { | ||
if let Some(replacement) = self.to_replace.resolved.get(&source.value.to_string()) { | ||
let module_id = replacement.0.clone(); | ||
let module_id = replacement.to_replace_source.clone(); | ||
let span = source.span; | ||
*source = Str::from(module_id); | ||
source.span = span; | ||
|
@@ -247,7 +251,7 @@ | |
use swc_core::common::GLOBALS; | ||
use swc_core::ecma::visit::VisitMutWith; | ||
|
||
use super::{DepReplacer, DependenciesToReplace, ResolvedModuleId, ResolvedModulePath}; | ||
use super::{DepReplacer, DependenciesToReplace, ResolvedReplaceInfo}; | ||
use crate::ast::tests::TestUtils; | ||
use crate::module::{Dependency, ImportType, ModuleId, ResolveType}; | ||
|
||
|
@@ -339,17 +343,13 @@ | |
); | ||
} | ||
|
||
fn build_resolved( | ||
key: &str, | ||
module_id: &str, | ||
) -> HashMap<String, (ResolvedModuleId, ResolvedModulePath)> { | ||
fn build_resolved(key: &str, module_id: &str) -> HashMap<String, ResolvedReplaceInfo> { | ||
hashmap! { | ||
key.to_string() => | ||
( | ||
|
||
module_id.to_string(), | ||
"".to_string() | ||
) | ||
ResolvedReplaceInfo { | ||
to_replace_source: module_id.into(), | ||
resolved_module_id: "".into(), | ||
} | ||
stormslowly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
|
@@ -367,7 +367,7 @@ | |
|
||
fn run( | ||
js_code: &str, | ||
resolved: HashMap<String, (ResolvedModuleId, ResolvedModulePath)>, | ||
resolved: HashMap<String, ResolvedReplaceInfo>, | ||
missing: HashMap<String, Dependency>, | ||
) -> String { | ||
let mut test_utils = TestUtils::gen_js_ast(js_code); | ||
|
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 被改成什么样了