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

fix: dynamic import css #756

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
44 changes: 38 additions & 6 deletions crates/mako/src/transformers/transform_dep_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ 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 All @@ -107,12 +108,36 @@ impl VisitMut for DepReplacer<'_> {
}
}

// remove `require('./xxx.css');`
let file_request = parse_path(&source_string).unwrap();
if is_css_path(&file_request.path)
&& (file_request.query.is_empty() || file_request.has_query("modules"))
{
*expr = Expr::Lit(quote_str!("").into())
// remove `require('./xxx.css');`
if is_commonjs_require_flag {
*expr = Expr::Lit(quote_str!("").into())
} 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.generate(self.context);
// `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()]);
} else {
*expr = Expr::Lit(quote_str!("").into())
}
} else {
*expr = Expr::Lit(quote_str!("").into())
}
}
}
}
}
Expand Down Expand Up @@ -205,7 +230,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 @@ -226,9 +251,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 @@ -261,9 +288,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 @@ -305,9 +334,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 @@ -343,6 +374,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