Unverified Commit 3cb77c84 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: unify ModuleGraph constructors (#93203)

We have options and variable length lists. Why do we need a separate
function for every combination?
parent 5d3232fb
...@@ -966,7 +966,7 @@ impl AppProject { ...@@ -966,7 +966,7 @@ impl AppProject {
graphs.push(graph); graphs.push(graph);
visited_modules = VisitedModules::concatenate(visited_modules, graph); visited_modules = VisitedModules::concatenate(visited_modules, graph);
let base = ModuleGraph::from_graphs(graphs.clone()); let base = ModuleGraph::from_graphs(graphs.clone(), None);
let additional_entries = endpoint.additional_entries(base.connect()); let additional_entries = endpoint.additional_entries(base.connect());
let additional_module_graph = SingleModuleGraph::new_with_entries_visited_intern( let additional_module_graph = SingleModuleGraph::new_with_entries_visited_intern(
additional_entries.owned().await?, additional_entries.owned().await?,
...@@ -991,20 +991,18 @@ impl AppProject { ...@@ -991,20 +991,18 @@ impl AppProject {
.await?; .await?;
let (full, binding_usage_info) = if remove_unused_imports { let (full, binding_usage_info) = if remove_unused_imports {
let full_with_unused_references = ModuleGraph::from_graphs(graphs.clone()); let full_with_unused_references =
ModuleGraph::from_graphs(graphs.clone(), None);
let binding_usage_info = compute_binding_usage_info( let binding_usage_info = compute_binding_usage_info(
full_with_unused_references, full_with_unused_references,
should_read_binding_usage, should_read_binding_usage,
); );
( (
ModuleGraph::from_graphs_without_unused_references( ModuleGraph::from_graphs(graphs, Some(binding_usage_info)),
graphs,
binding_usage_info,
),
Some(binding_usage_info), Some(binding_usage_info),
) )
} else { } else {
(ModuleGraph::from_graphs(graphs), None) (ModuleGraph::from_graphs(graphs, None), None)
}; };
Ok(BaseAndFullModuleGraph { Ok(BaseAndFullModuleGraph {
......
...@@ -753,11 +753,11 @@ impl PageEndpoint { ...@@ -753,11 +753,11 @@ impl PageEndpoint {
.await?; .await?;
let graph = if remove_unused_imports { let graph = if remove_unused_imports {
let graph = ModuleGraph::from_graphs(graphs.clone()); let graph = ModuleGraph::from_graphs(graphs.clone(), None);
let binding_usage_info = compute_binding_usage_info(graph, true); let binding_usage_info = compute_binding_usage_info(graph, true);
ModuleGraph::from_graphs_without_unused_references(graphs, binding_usage_info) ModuleGraph::from_graphs(graphs, Some(binding_usage_info))
} else { } else {
ModuleGraph::from_graphs(graphs) ModuleGraph::from_graphs(graphs, None)
}; };
Ok(graph.connect()) Ok(graph.connect())
......
...@@ -1452,11 +1452,14 @@ impl Project { ...@@ -1452,11 +1452,14 @@ impl Project {
) -> Result<Vc<ModuleGraph>> { ) -> Result<Vc<ModuleGraph>> {
Ok(if *self.per_page_module_graph().await? { Ok(if *self.per_page_module_graph().await? {
let is_production = self.next_mode().await?.is_production(); let is_production = self.next_mode().await?.is_production();
ModuleGraph::from_single_graph(SingleModuleGraph::new_with_entry( ModuleGraph::from_graphs(
ChunkGroupEntry::Entry(vec![entry]), vec![SingleModuleGraph::new_with_entry(
is_production, ChunkGroupEntry::Entry(vec![entry]),
is_production, is_production,
)) is_production,
)],
None,
)
.connect() .connect()
} else { } else {
*self.whole_app_module_graphs().await?.full *self.whole_app_module_graphs().await?.full
...@@ -1476,11 +1479,14 @@ impl Project { ...@@ -1476,11 +1479,14 @@ impl Project {
.copied() .copied()
.map(ResolvedVc::upcast) .map(ResolvedVc::upcast)
.collect(); .collect();
ModuleGraph::from_single_graph(SingleModuleGraph::new_with_entries( ModuleGraph::from_graphs(
ResolvedVc::cell(vec![ChunkGroupEntry::Entry(entries)]), vec![SingleModuleGraph::new_with_entries(
is_production, ResolvedVc::cell(vec![ChunkGroupEntry::Entry(entries)]),
is_production, is_production,
)) is_production,
)],
None,
)
.connect() .connect()
} else { } else {
*self.whole_app_module_graphs().await?.full *self.whole_app_module_graphs().await?.full
...@@ -2490,7 +2496,7 @@ async fn whole_app_module_graph_operation( ...@@ -2490,7 +2496,7 @@ async fn whole_app_module_graph_operation(
); );
let base_visited_modules = VisitedModules::from_graph(base_single_module_graph); let base_visited_modules = VisitedModules::from_graph(base_single_module_graph);
let base = ModuleGraph::from_single_graph(base_single_module_graph); let base = ModuleGraph::from_graphs(vec![base_single_module_graph], None);
let turbopack_remove_unused_imports = *project let turbopack_remove_unused_imports = *project
.next_config() .next_config()
...@@ -2501,10 +2507,7 @@ async fn whole_app_module_graph_operation( ...@@ -2501,10 +2507,7 @@ async fn whole_app_module_graph_operation(
// TODO suboptimal that we do compute_binding_usage_info twice (once for the base // TODO suboptimal that we do compute_binding_usage_info twice (once for the base
// graph and later for the full graph) // graph and later for the full graph)
let binding_usage_info = compute_binding_usage_info(base, true); let binding_usage_info = compute_binding_usage_info(base, true);
ModuleGraph::from_single_graph_without_unused_references( ModuleGraph::from_graphs(vec![base_single_module_graph], Some(binding_usage_info))
base_single_module_graph,
binding_usage_info,
)
} else { } else {
base base
}; };
...@@ -2540,14 +2543,14 @@ async fn whole_app_module_graph_operation( ...@@ -2540,14 +2543,14 @@ async fn whole_app_module_graph_operation(
let graphs = vec![base_single_module_graph, additional_module_graph]; let graphs = vec![base_single_module_graph, additional_module_graph];
let (full, binding_usage_info) = if turbopack_remove_unused_imports { let (full, binding_usage_info) = if turbopack_remove_unused_imports {
let full_with_unused_references = ModuleGraph::from_graphs(graphs.clone()); let full_with_unused_references = ModuleGraph::from_graphs(graphs.clone(), None);
let binding_usage_info = compute_binding_usage_info(full_with_unused_references, true); let binding_usage_info = compute_binding_usage_info(full_with_unused_references, true);
( (
ModuleGraph::from_graphs_without_unused_references(graphs, binding_usage_info), ModuleGraph::from_graphs(graphs, Some(binding_usage_info)),
Some(binding_usage_info), Some(binding_usage_info),
) )
} else { } else {
(ModuleGraph::from_graphs(graphs), None) (ModuleGraph::from_graphs(graphs, None), None)
}; };
Ok(BaseAndFullModuleGraph { Ok(BaseAndFullModuleGraph {
......
...@@ -774,11 +774,14 @@ async fn get_mock_stylesheet( ...@@ -774,11 +774,14 @@ async fn get_mock_stylesheet(
.module(); .module();
let entries = get_evaluate_entries(mocked_response_asset, asset_context, *node_backend, None); let entries = get_evaluate_entries(mocked_response_asset, asset_context, *node_backend, None);
let module_graph = ModuleGraph::from_single_graph(SingleModuleGraph::new_with_entries( let module_graph = ModuleGraph::from_graphs(
entries.graph_entries().to_resolved().await?, vec![SingleModuleGraph::new_with_entries(
false, entries.graph_entries().to_resolved().await?,
false, false,
)); false,
)],
None,
);
let module_graph = module_graph.connect(); let module_graph = module_graph.connect();
let root = mock_fs.root().owned().await?; let root = mock_fs.root().owned().await?;
......
...@@ -316,15 +316,14 @@ async fn build_internal( ...@@ -316,15 +316,14 @@ async fn build_internal(
false, false,
true, true,
); );
let mut module_graph = ModuleGraph::from_single_graph(single_graph); let mut module_graph = ModuleGraph::from_graphs(vec![single_graph], None);
let binding_usage = compute_binding_usage_info(module_graph, true); let binding_usage = compute_binding_usage_info(module_graph, true);
let unused_references = binding_usage let unused_references = binding_usage
.connect() .connect()
.unused_references() .unused_references()
.to_resolved() .to_resolved()
.await?; .await?;
module_graph = module_graph = ModuleGraph::from_graphs(vec![single_graph], Some(binding_usage));
ModuleGraph::from_single_graph_without_unused_references(single_graph, binding_usage);
let module_graph = module_graph.connect(); let module_graph = module_graph.connect();
let module_id_strategy = get_global_module_id_strategy(module_graph) let module_id_strategy = get_global_module_id_strategy(module_graph)
.to_resolved() .to_resolved()
......
...@@ -161,11 +161,14 @@ pub async fn create_web_entry_source( ...@@ -161,11 +161,14 @@ pub async fn create_web_entry_source(
.map(|&entry| ResolvedVc::upcast(entry)), .map(|&entry| ResolvedVc::upcast(entry)),
) )
.collect::<Vec<ResolvedVc<Box<dyn Module>>>>(); .collect::<Vec<ResolvedVc<Box<dyn Module>>>>();
let module_graph = ModuleGraph::from_single_graph(SingleModuleGraph::new_with_entries( let module_graph = ModuleGraph::from_graphs(
ResolvedVc::cell(vec![ChunkGroupEntry::Entry(all_modules)]), vec![SingleModuleGraph::new_with_entries(
false, ResolvedVc::cell(vec![ChunkGroupEntry::Entry(all_modules)]),
false, false,
)); false,
)],
None,
);
let module_graph = module_graph.connect().to_resolved().await?; let module_graph = module_graph.connect().to_resolved().await?;
let entries: Vec<_> = entries let entries: Vec<_> = entries
......
...@@ -698,56 +698,21 @@ pub struct ModuleGraph { ...@@ -698,56 +698,21 @@ pub struct ModuleGraph {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl ModuleGraph { impl ModuleGraph {
/// Analyze the module graph and potentially remove unused references (by determining the used
/// exports and removing unused imports).
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
pub async fn from_single_graph(graph: OperationVc<SingleModuleGraph>) -> Result<Vc<Self>> { pub async fn from_graphs(
let graph = Self::create(vec![graph], None)
.read_strongly_consistent()
.await?;
Ok(ReadRef::cell(graph))
}
#[turbo_tasks::function(operation)]
pub async fn from_graphs(graphs: Vec<OperationVc<SingleModuleGraph>>) -> Result<Vc<Self>> {
let graph = Self::create(graphs, None)
.read_strongly_consistent()
.await?;
Ok(ReadRef::cell(graph))
}
/// Analyze the module graph and remove unused references (by determining the used exports and
/// removing unused imports).
///
/// In particular, this removes ModuleReference-s that list only unused exports in the
/// `import_usage()`
#[turbo_tasks::function(operation)]
pub async fn from_single_graph_without_unused_references(
graph: OperationVc<SingleModuleGraph>,
binding_usage: OperationVc<BindingUsageInfo>,
) -> Result<Vc<Self>> {
let graph = Self::create(vec![graph], Some(binding_usage))
.read_strongly_consistent()
.await?;
Ok(ReadRef::cell(graph))
}
/// Analyze the module graph and remove unused references (by determining the used exports and
/// removing unused imports).
///
/// In particular, this removes ModuleReference-s that list only unused exports in the
/// `import_usage()`
#[turbo_tasks::function(operation)]
pub async fn from_graphs_without_unused_references(
graphs: Vec<OperationVc<SingleModuleGraph>>, graphs: Vec<OperationVc<SingleModuleGraph>>,
binding_usage: OperationVc<BindingUsageInfo>, binding_usage: Option<OperationVc<BindingUsageInfo>>,
) -> Result<Vc<Self>> { ) -> Result<Vc<Self>> {
let graph = Self::create(graphs, Some(binding_usage)) let graph = Self::from_graphs_inner(graphs, binding_usage)
.read_strongly_consistent() .read_strongly_consistent()
.await?; .await?;
Ok(ReadRef::cell(graph)) Ok(ReadRef::cell(graph))
} }
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
async fn create( async fn from_graphs_inner(
graphs: Vec<OperationVc<SingleModuleGraph>>, graphs: Vec<OperationVc<SingleModuleGraph>>,
binding_usage: Option<OperationVc<BindingUsageInfo>>, binding_usage: Option<OperationVc<BindingUsageInfo>>,
) -> Result<Vc<ModuleGraph>> { ) -> Result<Vc<ModuleGraph>> {
...@@ -2103,15 +2068,18 @@ pub mod tests { ...@@ -2103,15 +2068,18 @@ pub mod tests {
false, false,
); );
let module_graph = ModuleGraph::from_graphs(vec![ let module_graph = ModuleGraph::from_graphs(
parent_graph, vec![
SingleModuleGraph::new_with_entries_visited( parent_graph,
ResolvedVc::cell(vec![ChunkGroupEntry::Entry(vec![a_module])]), SingleModuleGraph::new_with_entries_visited(
VisitedModules::from_graph(parent_graph), ResolvedVc::cell(vec![ChunkGroupEntry::Entry(vec![a_module])]),
false, VisitedModules::from_graph(parent_graph),
false, false,
), false,
]) ),
],
None,
)
.connect(); .connect();
let child_graph = module_graph let child_graph = module_graph
.iter_graphs() .iter_graphs()
...@@ -2366,7 +2334,9 @@ pub mod tests { ...@@ -2366,7 +2334,9 @@ pub mod tests {
.await? .await?
.into_iter() .into_iter()
.collect(); .collect();
let module_graph = ModuleGraph::from_single_graph(graph).connect().await?; let module_graph = ModuleGraph::from_graphs(vec![graph], None)
.connect()
.await?;
Ok(SetupGraph { Ok(SetupGraph {
module_graph, module_graph,
......
...@@ -546,11 +546,14 @@ impl PostCssTransformedAsset { ...@@ -546,11 +546,14 @@ impl PostCssTransformedAsset {
.to_resolved() .to_resolved()
.await?; .await?;
let module_graph = ModuleGraph::from_single_graph(SingleModuleGraph::new_with_entries( let module_graph = ModuleGraph::from_graphs(
entries.graph_entries().to_resolved().await?, vec![SingleModuleGraph::new_with_entries(
false, entries.graph_entries().to_resolved().await?,
false, false,
)) false,
)],
None,
)
.connect() .connect()
.to_resolved() .to_resolved()
.await?; .await?;
......
...@@ -274,11 +274,14 @@ impl WebpackLoadersProcessedAsset { ...@@ -274,11 +274,14 @@ impl WebpackLoadersProcessedAsset {
.to_resolved() .to_resolved()
.await?; .await?;
let module_graph = ModuleGraph::from_single_graph(SingleModuleGraph::new_with_entries( let module_graph = ModuleGraph::from_graphs(
entries.graph_entries().to_resolved().await?, vec![SingleModuleGraph::new_with_entries(
false, entries.graph_entries().to_resolved().await?,
false, false,
)) false,
)],
None,
)
.connect() .connect()
.to_resolved() .to_resolved()
.await?; .await?;
...@@ -715,7 +718,7 @@ impl EvaluateContext for WebpackLoaderContext { ...@@ -715,7 +718,7 @@ impl EvaluateContext for WebpackLoaderContext {
false, false,
false, false,
); );
let import_module_graph = ModuleGraph::from_single_graph(single_graph) let import_module_graph = ModuleGraph::from_graphs(vec![single_graph], None)
.connect() .connect()
.to_resolved() .to_resolved()
.await?; .await?;
......
...@@ -517,7 +517,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V ...@@ -517,7 +517,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V
false, false,
true, true,
); );
let mut module_graph = ModuleGraph::from_single_graph(single_graph); let mut module_graph = ModuleGraph::from_graphs(vec![single_graph], None);
let binding_usage = if options.remove_unused_imports || options.remove_unused_exports { let binding_usage = if options.remove_unused_imports || options.remove_unused_exports {
Some(compute_binding_usage_info( Some(compute_binding_usage_info(
...@@ -530,8 +530,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V ...@@ -530,8 +530,7 @@ async fn run_test_operation(prepared_test: ResolvedVc<PreparedTest>) -> Result<V
if options.remove_unused_imports if options.remove_unused_imports
&& let Some(binding_usage) = binding_usage && let Some(binding_usage) = binding_usage
{ {
module_graph = module_graph = ModuleGraph::from_graphs(vec![single_graph], Some(binding_usage));
ModuleGraph::from_single_graph_without_unused_references(single_graph, binding_usage);
} }
let module_graph = module_graph.connect(); let module_graph = module_graph.connect();
......
...@@ -460,7 +460,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> { ...@@ -460,7 +460,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
false, false,
true, true,
); );
let mut module_graph = ModuleGraph::from_single_graph(single_graph); let mut module_graph = ModuleGraph::from_graphs(vec![single_graph], None);
let binding_usage = if options.remove_unused_imports || options.remove_unused_exports { let binding_usage = if options.remove_unused_imports || options.remove_unused_exports {
Some(compute_binding_usage_info( Some(compute_binding_usage_info(
...@@ -473,8 +473,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> { ...@@ -473,8 +473,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
if options.remove_unused_imports if options.remove_unused_imports
&& let Some(binding_usage) = binding_usage && let Some(binding_usage) = binding_usage
{ {
module_graph = module_graph = ModuleGraph::from_graphs(vec![single_graph], Some(binding_usage));
ModuleGraph::from_single_graph_without_unused_references(single_graph, binding_usage);
} }
let module_graph = module_graph.connect(); let module_graph = module_graph.connect();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment