Skip to content

Commit

Permalink
refactor: 改用 get_dependency_module_by_source 获取依赖模块的 id
Browse files Browse the repository at this point in the history
  • Loading branch information
Jess committed Dec 6, 2023
1 parent 3766736 commit 8e1bef8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
3 changes: 2 additions & 1 deletion crates/mako/src/plugins/bundless_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub fn transform_modules(module_ids: Vec<ModuleId>, context: &Arc<Context>) -> R
}

pub fn transform_js_generate(
_id: &ModuleId,
module_id: &ModuleId,
context: &Arc<Context>,
ast: &mut Ast,
dep_map: &DependenciesToReplace,
Expand Down Expand Up @@ -227,6 +227,7 @@ pub fn transform_js_generate(
// ast.ast.visit_mut_with(&mut inject_helpers(unresolved_mark));

let mut dep_replacer = DepReplacer {
module_id,
to_replace: dep_map,
context,
unresolved_mark: ast.unresolved_mark,
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/plugins/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl MakoRuntime {
ignored: vec![],
},
async_deps: &Vec::<Dependency>::new(),
_id: &module_id,
module_id: &module_id,
context,
ast: &mut ast,
})?;
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ const require = window.require;
)
.unwrap();
transform_js_generate(TransformJsParam {
_id: &ModuleId::new("test".to_string()),
module_id: &ModuleId::new("test".to_string()),
context: &context,
ast: &mut ast,
dep_map: &DependenciesToReplace {
Expand Down
7 changes: 4 additions & 3 deletions crates/mako/src/transform_in_generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn transform_modules_in_thread(
};
if let ModuleAst::Script(ast) = ast {
let ret = transform_js_generate(TransformJsParam {
_id: &module.id,
module_id: &module.id,
context: &context,
ast,
dep_map: &deps_to_replace,
Expand Down Expand Up @@ -173,7 +173,7 @@ fn insert_swc_helper_replace(map: &mut HashMap<String, String>, context: &Arc<Co
}

pub struct TransformJsParam<'a> {
pub _id: &'a ModuleId,
pub module_id: &'a ModuleId,
pub context: &'a Arc<Context>,
pub ast: &'a mut Ast,
pub dep_map: &'a DependenciesToReplace,
Expand All @@ -185,7 +185,7 @@ pub struct TransformJsParam<'a> {
pub fn transform_js_generate(transform_js_param: TransformJsParam) -> Result<()> {
mako_core::mako_profile_function!();
let TransformJsParam {
_id,
module_id,
context,
ast,
dep_map,
Expand Down Expand Up @@ -246,6 +246,7 @@ pub fn transform_js_generate(transform_js_param: TransformJsParam) -> Result<()>
}

let mut dep_replacer = DepReplacer {
module_id,
to_replace: dep_map,
context,
unresolved_mark,
Expand Down
55 changes: 33 additions & 22 deletions crates/mako/src/transformers/transform_dep_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ use std::sync::Arc;

use mako_core::swc_common::{Mark, DUMMY_SP};
use mako_core::swc_ecma_ast::{
AssignOp, BlockStmt, CallExpr, Callee, Expr, ExprOrSpread, FnExpr, Function, Ident, ImportDecl,
Lit, NamedExport, NewExpr, Stmt, Str, ThrowStmt, VarDeclKind,
AssignOp, BlockStmt, Expr, ExprOrSpread, FnExpr, Function, Ident, ImportDecl, Lit, NamedExport,
NewExpr, Stmt, Str, ThrowStmt, VarDeclKind,
};
use mako_core::swc_ecma_utils::{member_expr, quote_ident, quote_str, ExprFactory};
use mako_core::swc_ecma_visit::{VisitMut, VisitMutWith};

use crate::build::parse_path;
use crate::compiler::Context;
use crate::module::Dependency;
use crate::module::{Dependency, ModuleId};
use crate::plugins::css::is_url_ignored;
use crate::plugins::javascript::{is_commonjs_require, is_dynamic_import};
use crate::transformers::transform_virtual_css_modules::is_css_path;

pub struct DepReplacer<'a> {
pub module_id: &'a ModuleId,
pub to_replace: &'a DependenciesToReplace,
pub context: &'a Arc<Context>,
pub unresolved_mark: Mark,
Expand Down Expand Up @@ -83,8 +84,8 @@ pub fn miss_throw_stmt<T: AsRef<str>>(source: T) -> Expr {
impl VisitMut for DepReplacer<'_> {
fn visit_mut_expr(&mut self, expr: &mut Expr) {
if let Expr::Call(call_expr) = expr {
if is_commonjs_require(call_expr, &self.unresolved_mark) || is_dynamic_import(call_expr)
{
let is_commonjs_require_flag = is_commonjs_require(call_expr, &self.unresolved_mark);
if is_commonjs_require_flag || is_dynamic_import(call_expr) {
if call_expr.args.is_empty() {
return;
}
Expand Down Expand Up @@ -112,29 +113,32 @@ impl VisitMut for DepReplacer<'_> {
&& (file_request.query.is_empty() || file_request.has_query("modules"))
{
// remove `require('./xxx.css');`
if is_commonjs_require(call_expr, &self.unresolved_mark) {
if is_commonjs_require_flag {
*expr = Expr::Lit(quote_str!("").into())
} else {
// `import('./xxx.css')` => ensure css chunk
let module_id = self.to_replace.resolved.get(&source_string);
if let Some(module_id) = module_id {
// `import('./xxx.css')` 中的 css 模块会被拆分到单独的 chunk, 这里需要改为加载 css chunk
let module_graph = self.context.module_graph.read().unwrap();
let dep_module_id = module_graph
.get_dependency_module_by_source(self.module_id, &source_string);

if let Some(dep_module_id) = dep_module_id {
let chunk_graph = self.context.chunk_graph.read().unwrap();
let chunk = chunk_graph.chunk(&module_id.clone().into());
let dep_module_id = dep_module_id.generate(self.context);
let chunk = chunk_graph.chunk(&dep_module_id.clone().into());

if let Some(chunk) = chunk {
let chunk_id = chunk.id.generate(self.context);
*expr = Expr::Call(CallExpr {
span: DUMMY_SP,
type_args: None,
args: vec![ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(quote_str!(chunk_id).into())),
}],
callee: Callee::Expr(member_expr!(
// `import('./xxx.css')` => `__mako_require__.ensure('./xxx.css')`
*expr = member_expr!(DUMMY_SP, __mako_require__.ensure)
.as_call(
DUMMY_SP,
__mako_require__.ensure
)),
});
vec![ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(
quote_str!(chunk_id).into(),
)),
}],
);
} else {
*expr = Expr::Lit(quote_str!("").into())
}
Expand Down Expand Up @@ -234,7 +238,7 @@ mod tests {
use crate::assert_display_snapshot;
use crate::ast::build_js_ast;
use crate::compiler::Context;
use crate::module::{Dependency, ResolveType};
use crate::module::{Dependency, ModuleId, ResolveType};
use crate::test_helper::transform_ast_with;
use crate::transformers::test_helper::transform_js_code;
use crate::transformers::transform_dep_replacer::{DepReplacer, DependenciesToReplace};
Expand All @@ -255,9 +259,11 @@ mod tests {
};

let cloned = context.clone();
let module_id = ModuleId::new("index.jsx".to_string());
let mut visitor: Box<dyn VisitMut> = Box::new(chain!(
resolver(ast.unresolved_mark, ast.top_level_mark, false),
DepReplacer {
module_id: &module_id,
to_replace: &to_replace,
context: &cloned,
unresolved_mark: ast.unresolved_mark,
Expand Down Expand Up @@ -290,9 +296,11 @@ mod tests {
};

let cloned = context.clone();
let module_id = ModuleId::new("index.jsx".to_string());
let mut visitor: Box<dyn VisitMut> = Box::new(chain!(
resolver(ast.unresolved_mark, ast.top_level_mark, false),
DepReplacer {
module_id: &module_id,
to_replace: &to_replace,
context: &cloned,
unresolved_mark: ast.unresolved_mark,
Expand Down Expand Up @@ -334,9 +342,11 @@ mod tests {
};

let cloned = context.clone();
let module_id = ModuleId::new("index.jsx".to_string());
let mut visitor: Box<dyn VisitMut> = Box::new(chain!(
resolver(ast.unresolved_mark, ast.top_level_mark, false),
DepReplacer {
module_id: &module_id,
to_replace: &to_replace,
context: &cloned,
unresolved_mark: ast.unresolved_mark,
Expand Down Expand Up @@ -372,6 +382,7 @@ mod tests {
let unresolved_mark = Default::default();
let top_level_mark = Default::default();
let mut visitor = DepReplacer {
module_id: &ModuleId::new("index.jsx".into()),
to_replace: &DependenciesToReplace {
resolved: hashmap! {
"x".to_string() => "/x/index.js".to_string()
Expand Down

0 comments on commit 8e1bef8

Please sign in to comment.