Skip to content

Commit

Permalink
refact: no need to mark ignored on File
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Mar 6, 2024
1 parent 97956f2 commit 2a69582
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
8 changes: 0 additions & 8 deletions crates/mako/src/ast_2/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>,
Expand All @@ -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![],
Expand Down Expand Up @@ -127,12 +125,6 @@ impl File {
file
}

pub fn new_ignore_with_content(path: String, content: Content, context: Arc<Context>) -> 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);
}
Expand Down
58 changes: 36 additions & 22 deletions crates/mako/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<Context>) -> 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)
}
Expand Down
12 changes: 0 additions & 12 deletions crates/mako/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand All @@ -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<Content> = context
.plugin_driver
Expand Down

0 comments on commit 2a69582

Please sign in to comment.