-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 🎨 分离 replacer 和 css import 删除逻辑
- Loading branch information
1 parent
b302c7f
commit cdb1cbf
Showing
4 changed files
with
87 additions
and
48 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
79 changes: 79 additions & 0 deletions
79
crates/mako/src/transformers/transform_css_import_remover.rs
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,79 @@ | ||
use std::sync::Arc; | ||
|
||
use mako_core::swc_common::{Mark, DUMMY_SP}; | ||
use mako_core::swc_ecma_ast::{Expr, ExprOrSpread, Lit}; | ||
use mako_core::swc_ecma_utils::{member_expr, quote_str, ExprFactory}; | ||
use mako_core::swc_ecma_visit::{VisitMut, VisitMutWith}; | ||
|
||
use crate::compiler::Context; | ||
use crate::module::ModuleId; | ||
use crate::plugins::javascript::{is_commonjs_require, is_dynamic_import}; | ||
use crate::task::parse_path; | ||
use crate::transformers::transform_virtual_css_modules::is_css_path; | ||
|
||
pub struct CssImportRemover<'a> { | ||
pub module_id: &'a ModuleId, | ||
pub context: &'a Arc<Context>, | ||
pub unresolved_mark: Mark, | ||
} | ||
|
||
impl VisitMut for CssImportRemover<'_> { | ||
fn visit_mut_expr(&mut self, expr: &mut Expr) { | ||
if let Expr::Call(call_expr) = 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; | ||
} | ||
if let ExprOrSpread { | ||
expr: box Expr::Lit(Lit::Str(ref mut source)), | ||
.. | ||
} = &mut call_expr.args[0] | ||
{ | ||
let source_string = source.value.clone().to_string(); | ||
|
||
let file_request = parse_path(&source_string).unwrap(); | ||
let is_source_replaceable = is_css_path(&file_request.path) | ||
&& (file_request.query.is_empty() || file_request.has_query("modules")); | ||
|
||
if is_source_replaceable { | ||
// remove `require('./xxx.css');` | ||
if is_commonjs_require_flag { | ||
*expr = Expr::Lit(quote_str!("").into()); | ||
return; | ||
} else { | ||
// `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.get_chunk_for_module(&dep_module_id.clone()); | ||
|
||
if let Some(chunk) = chunk { | ||
let chunk_id = chunk.id.id.clone(); | ||
// `import('./xxx.css')` => `__mako_require__.ensure('./xxx.css')` | ||
*expr = member_expr!(DUMMY_SP, __mako_require__.ensure) | ||
.as_call(DUMMY_SP, vec![quote_str!(chunk_id).as_arg()]); | ||
return; | ||
} else { | ||
*expr = Expr::Lit(quote_str!("").into()); | ||
return; | ||
} | ||
} else { | ||
*expr = Expr::Lit(quote_str!("").into()); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
expr.visit_mut_children_with(self); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests {} |
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