Skip to content
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 14 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/mako/src/compiler.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个 e2e? 看下 entry 被改成什么样了

Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@
plugins.push(Arc::new(plugins::ssu::SUPlus::new()));
}

if args.watch && config.experimental.central_ensure {
plugins.push(Arc::new(plugins::central_ensure::CentralChunkEnsure {}));

Check warning on line 302 in crates/mako/src/compiler.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/compiler.rs#L302

Added line #L302 was not covered by tests
}

if let Some(minifish_config) = &config._minifish {
let inject = if let Some(inject) = &minifish_config.inject {
let mut map = HashMap::new();
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/config/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct ExperimentalConfig {
pub magic_comment: bool,
#[serde(deserialize_with = "deserialize_detect_loop")]
pub detect_circular_dependence: Option<DetectCircularDependence>,
pub central_ensure: bool,
}

#[derive(Deserialize, Serialize, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion crates/mako/src/config/mako.config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"detectCircularDependence": {
"ignores": ["node_modules"],
"graphviz": false
}
},
"centralEnsure": true
},
"useDefineForClassFields": true,
"emitDecoratorMetadata": false,
Expand Down
62 changes: 31 additions & 31 deletions crates/mako/src/generate/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use crate::visitors::async_module::{mark_async, AsyncModule};
use crate::visitors::common_js::common_js;
use crate::visitors::css_imports::CSSImports;
use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace};
use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace, ResolvedReplaceInfo};
use crate::visitors::dynamic_import::DynamicImport;
use crate::visitors::mako_require::MakoRequire;
use crate::visitors::meta_url_replacer::MetaUrlReplacer;
Expand Down Expand Up @@ -84,36 +84,27 @@
thread_pool::spawn(move || {
let module_graph = context.module_graph.read().unwrap();
let deps = module_graph.get_dependencies(&module_id);
let mut resolved_deps: HashMap<String, (String, String)> = deps
let mut resolved_deps: HashMap<String, ResolvedReplaceInfo> = deps
.into_iter()
.map(|(id, dep)| {
(
dep.source.clone(),
(
match &dep.resolve_type {
ResolveType::Worker(import_options) => {
let chunk_id = match import_options.get_chunk_name() {
Some(chunk_name) => {
generate_module_id(chunk_name, &context)
}
None => id.generate(&context),
};
let chunk_graph = context.chunk_graph.read().unwrap();
chunk_graph.chunk(&chunk_id.into()).unwrap().filename()
}
ResolveType::DynamicImport(import_options) => {
match import_options.get_chunk_name() {
Some(chunk_name) => {
generate_module_id(chunk_name, &context)
}
None => id.generate(&context),
}
}
_ => id.generate(&context),
},
id.id.clone(),
),
)
let to_replace_source = match &dep.resolve_type {
ResolveType::Worker(import_options) => {
let chunk_id = match import_options.get_chunk_name() {
Some(chunk_name) => generate_module_id(chunk_name, &context),
None => id.generate(&context),

Check warning on line 94 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L91-L94

Added lines #L91 - L94 were not covered by tests
};
let chunk_graph = context.chunk_graph.read().unwrap();
chunk_graph.chunk(&chunk_id.into()).unwrap().filename()
}

Check warning on line 98 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L96-L98

Added lines #L96 - L98 were not covered by tests
_ => id.generate(&context),
};

let resolve_replace_info = ResolvedReplaceInfo {
to_replace_source,
resolved_module_id: id.clone(),
};

(dep.source.clone(), resolve_replace_info)
})
.collect();
insert_swc_helper_replace(&mut resolved_deps, &context);
Expand Down Expand Up @@ -162,10 +153,19 @@
Ok(())
}

fn insert_swc_helper_replace(map: &mut HashMap<String, (String, String)>, context: &Arc<Context>) {
fn insert_swc_helper_replace(
map: &mut HashMap<String, ResolvedReplaceInfo>,
context: &Arc<Context>,
) {
SWC_HELPERS.into_iter().for_each(|h| {
let m_id: ModuleId = h.to_string().into();
map.insert(m_id.id.clone(), (m_id.generate(context), h.to_string()));
map.insert(
m_id.id.clone(),
ResolvedReplaceInfo {
to_replace_source: m_id.generate(context),
resolved_module_id: m_id,
},
);
});
}

Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod async_runtime;
pub mod bundless_compiler;
pub mod central_ensure;
pub mod context_module;
pub mod copy;
pub mod detect_circular_dependence;
Expand Down
12 changes: 9 additions & 3 deletions crates/mako/src/plugins/bundless_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use crate::config::Config;
use crate::module::{ModuleAst, ModuleId};
use crate::plugin::{Plugin, PluginTransformJsParam};
use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace};
use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace, ResolvedReplaceInfo};
use crate::visitors::dynamic_import::DynamicImport;

pub struct BundlessCompiler {
Expand Down Expand Up @@ -80,11 +80,17 @@
}
};

Ok((dep.source.clone(), (replacement.clone(), replacement)))
Ok((
dep.source.clone(),
ResolvedReplaceInfo {
to_replace_source: replacement,
resolved_module_id: id.clone(),

Check warning on line 87 in crates/mako/src/plugins/bundless_compiler.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/bundless_compiler.rs#L83-L87

Added lines #L83 - L87 were not covered by tests
},
))
})
.collect::<Result<Vec<_>>>();

let resolved_deps: HashMap<String, (String, String)> =
let resolved_deps: HashMap<String, ResolvedReplaceInfo> =
resolved_deps?.into_iter().collect();

drop(module_graph);
Expand Down
76 changes: 76 additions & 0 deletions crates/mako/src/plugins/central_ensure.rs
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 {

Check warning on line 13 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L13

Added line #L13 was not covered by tests
"dev_ensure2"
}
fn runtime_plugins(&self, context: &Arc<Context>) -> anyhow::Result<Vec<String>> {
let mg = context

Check warning on line 17 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L15-L17

Added lines #L15 - L17 were not covered by tests
.module_graph
.read()
.map_err(|e| anyhow!("Read_Module_Graph_error:\n{:?}", e))?;
let cg = context

Check warning on line 21 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L20-L21

Added lines #L20 - L21 were not covered by tests
.chunk_graph
.read()
.map_err(|e| anyhow!("Read_Chunk_Graph_error:\n{:?}", e))?;

Check warning on line 24 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L24

Added line #L24 was not covered by tests

let mut chunk_async_map: BTreeMap<String, Vec<String>> = Default::default();

Check warning on line 26 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L26

Added line #L26 was not covered by tests

mg.modules().iter().for_each(|module| {
let be_dynamic_imported = mg
.get_dependents(&module.id)

Check warning on line 30 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
.iter()
.any(|(_, dep)| dep.resolve_type.is_dynamic_esm());

Check warning on line 32 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L32

Added line #L32 was not covered by tests

if be_dynamic_imported {
cg.get_chunk_for_module(&module.id)

Check warning on line 35 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L34-L35

Added lines #L34 - L35 were not covered by tests
.iter()
.for_each(|chunk| {
let deps_chunks = cg
.installable_descendants_chunk(&chunk.id)

Check warning on line 39 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L37-L39

Added lines #L37 - L39 were not covered by tests
.iter()
.map(|chunk_id| chunk_id.generate(context))
.collect::<Vec<_>>();

Check warning on line 42 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L41-L42

Added lines #L41 - L42 were not covered by tests

dbg!(&deps_chunks);

Check warning on line 44 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L44

Added line #L44 was not covered by tests
stormslowly marked this conversation as resolved.
Show resolved Hide resolved

chunk_async_map
.insert(generate_module_id(&module.id.id, context), deps_chunks);
});

Check warning on line 48 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L46-L48

Added lines #L46 - L48 were not covered by tests
}
});

Check warning on line 50 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L50

Added line #L50 was not covered by tests

// TODO: compress the map to reduce duplicated chunk ids
let ensure_map = serde_json::to_string(&chunk_async_map)?;

Check warning on line 53 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L53

Added line #L53 was not covered by tests

let runtime = format!(

Check warning on line 55 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L55

Added line #L55 was not covered by tests
r#"
(function(){{
let map = {ensure_map};
requireModule.updateEnsuire2Map = function(newMaping) {{

Check warning on line 59 in crates/mako/src/plugins/central_ensure.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"Maping" should be "Mapping".
map = newMaping;

Check warning on line 60 in crates/mako/src/plugins/central_ensure.rs

View workflow job for this annotation

GitHub Actions / Spell Check

"Maping" should be "Mapping".
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])
}

Check warning on line 75 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
}
34 changes: 17 additions & 17 deletions crates/mako/src/visitors/dep_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
pub unresolved_mark: Mark,
}

type ResolvedModuleId = String;
type ResolvedModulePath = String;
#[derive(Debug, Clone)]

Check warning on line 26 in crates/mako/src/visitors/dep_replacer.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/visitors/dep_replacer.rs#L26

Added line #L26 was not covered by tests
pub struct ResolvedReplaceInfo {
pub to_replace_source: String,
pub resolved_module_id: ModuleId,

Check warning on line 29 in crates/mako/src/visitors/dep_replacer.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/visitors/dep_replacer.rs#L28-L29

Added lines #L28 - L29 were not covered by tests
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议为新结构体 ResolvedReplaceInfo 添加单元测试

新添加的结构体 ResolvedReplaceInfo 未被测试覆盖,建议添加相应的单元测试以确保其功能的正确性和稳定性。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 26-26: crates/mako/src/visitors/dep_replacer.rs#L26
Added line #L26 was not covered by tests


[warning] 28-29: crates/mako/src/visitors/dep_replacer.rs#L28-L29
Added lines #L28 - L29 were not covered by tests


#[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>,

Check warning on line 36 in crates/mako/src/visitors/dep_replacer.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/visitors/dep_replacer.rs#L36

Added line #L36 was not covered by tests
pub missing: HashMap<String, Dependency>,
}

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

建议处理 parse_path 的可能错误以避免 panic

在第 132 行,使用了 parse_path(&replace_info.resolved_module_id.id).unwrap();,如果 parse_path 返回 Noneunwrap() 会导致程序 panic。为了提高代码的健壮性,建议处理可能的错误。

建议修改为:

- 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 {
+     // 适当地处理错误,例如记录日志或返回错误
+ }

Committable suggestion skipped: line range outside the PR's diff.

// when inline_css is enabled
// css is parsed as js modules
self.context.config.inline_css.is_none()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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
}
}

Expand All @@ -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);
Expand Down
Loading
Loading