diff --git a/crates/mako/src/ast_2/file.rs b/crates/mako/src/ast_2/file.rs index 6950c5043..7bdd1661c 100644 --- a/crates/mako/src/ast_2/file.rs +++ b/crates/mako/src/ast_2/file.rs @@ -37,7 +37,6 @@ pub struct File { pub is_css_modules: bool, pub is_virtual: bool, pub is_entry: bool, - pub is_ignore: bool, pub pathname: PathBuf, pub search: String, pub params: Vec<(String, String)>, @@ -54,7 +53,6 @@ impl Default for File { is_css_modules: false, is_virtual: false, is_entry: false, - is_ignore: false, pathname: PathBuf::new(), search: "".to_string(), params: vec![], @@ -127,12 +125,6 @@ impl File { file } - pub fn new_ignore_with_content(path: String, content: Content, context: Arc) -> Self { - let mut file = File::with_content(path, content, context); - file.is_ignore = true; - file - } - pub fn set_content(&mut self, content: Content) { self.content = Some(content); } diff --git a/crates/mako/src/build.rs b/crates/mako/src/build.rs index f6582f810..b9bcf2558 100644 --- a/crates/mako/src/build.rs +++ b/crates/mako/src/build.rs @@ -84,29 +84,23 @@ impl Compiler { let path = dep.resolver_resource.get_resolved_path(); let dep_module_id = ModuleId::new(path.clone()); if !module_graph.has_module(&dep_module_id) { - let (is_external, is_ignore) = match dep.resolver_resource { - ResolverResource::Resolved(_) => (false, false), - ResolverResource::External(_) => (true, false), - ResolverResource::Ignored(_) => (false, true), - }; - let module = if is_external { - Self::create_external_module(&dep.resolver_resource, self.context.clone()) - } else { - Self::create_empty_module(&dep_module_id) + let module = match dep.resolver_resource { + ResolverResource::Resolved(_) => { + count += 1; + + let file = File::new(path.clone(), self.context.clone()); + build_with_pool(file, Some(dep.resolver_resource.clone())); + Self::create_empty_module(&dep_module_id) + } + ResolverResource::External(_) => Self::create_external_module( + &dep.resolver_resource, + self.context.clone(), + ), + ResolverResource::Ignored(_) => { + Self::create_ignored_module(&path, self.context.clone()) + } }; - if !is_external { - count += 1; - let file = if !is_ignore { - File::new(path.clone(), self.context.clone()) - } else { - File::new_ignore_with_content( - path.clone(), - Content::Js("".to_string()), - self.context.clone(), - ) - }; - build_with_pool(file, Some(dep.resolver_resource.clone())); - } + // 拿到依赖之后需要直接添加 module 到 module_graph 里,不能等依赖 build 完再添加 // 是因为由于是异步处理各个模块,后者会导致大量重复任务的 build_module 任务(3 倍左右) module_ids.insert(module.id.clone()); @@ -191,6 +185,26 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject( Ok(Module::new(module_id, false, Some(info))) } + fn create_ignored_module(path: &str, context: Arc) -> Module { + let module_id = ModuleId::new(path.to_owned()); + let mut module = Module::new(module_id, false, None); + let file = File::with_content( + path.to_owned(), + Content::Js("".to_string()), + context.clone(), + ); + + let ast = Parse::parse(&file, context.clone()).unwrap(); + let info = ModuleInfo { + file, + ast, + ..Default::default() + }; + + module.add_info(Some(info)); + module + } + pub fn create_empty_module(module_id: &ModuleId) -> Module { Module::new(module_id.clone(), false, None) } diff --git a/crates/mako/src/load.rs b/crates/mako/src/load.rs index a07dfeb85..1ba995270 100644 --- a/crates/mako/src/load.rs +++ b/crates/mako/src/load.rs @@ -29,8 +29,6 @@ enum LoadError { ToSvgrError { path: String, reason: String }, #[error("Compile md error: {path:?}, reason: {reason:?}")] CompileMdError { path: String, reason: String }, - #[error("Ignored file without content: {path:?}")] - IgnoredFileNoContent { path: String }, } lazy_static! { @@ -54,16 +52,6 @@ impl Load { mako_core::mako_profile_function!(file.path.to_string_lossy()); debug!("load: {:?}", file); - if file.is_ignore { - if let Some(content) = &file.content { - return Ok(content.clone()); - } else { - return Err(anyhow!(LoadError::IgnoredFileNoContent { - path: file.path.to_string_lossy().to_string() - })); - } - } - // plugin first let content: Option = context .plugin_driver