Skip to content

Turbopack: avoid single css chunks when there is only a single chunk item #77283

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

Merged
merged 4 commits into from
Mar 19, 2025
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
54 changes: 31 additions & 23 deletions turbopack/crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod source_map;
use std::fmt::Write;

use anyhow::{bail, Result};
use swc_core::common::pass::Either;
use turbo_rcstr::RcStr;
use turbo_tasks::{
FxIndexSet, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, ValueDefault, ValueToString,
Expand Down Expand Up @@ -218,19 +219,23 @@ impl OutputChunk for CssChunk {
.into_iter()
.flatten()
.collect();
let module_chunks = content
.chunk_items
.iter()
.chain(imports_chunk_items.iter())
.map(|item| {
Vc::upcast::<Box<dyn OutputAsset>>(SingleItemCssChunk::new(
*self.chunking_context,
**item,
))
.to_resolved()
})
.try_join()
.await?;
let module_chunks = if entries_chunk_items.len() > 1 {
content
.chunk_items
.iter()
.chain(imports_chunk_items.iter())
.map(|item| {
Vc::upcast::<Box<dyn OutputAsset>>(SingleItemCssChunk::new(
*self.chunking_context,
**item,
))
.to_resolved()
})
.try_join()
.await?
} else {
Vec::new()
};
Ok(OutputChunkRuntimeInfo {
included_ids: Some(ResolvedVc::cell(included_ids)),
module_chunks: Some(ResolvedVc::cell(module_chunks)),
Expand Down Expand Up @@ -310,21 +315,24 @@ impl OutputAsset for CssChunk {
let this = self.await?;
let content = this.content.await?;
let mut references = content.referenced_output_assets.owned().await?;
let single_item_chunks = content.chunk_items.len() > 1;
references.extend(
content
.chunk_items
.iter()
.map(|item| async {
Ok(item
.references()
.await?
.into_iter()
.copied()
.chain(std::iter::once(ResolvedVc::upcast(
SingleItemCssChunk::new(*this.chunking_context, **item)
.to_resolved()
.await?,
))))
let references = item.references().await?.into_iter().copied();
Ok(if single_item_chunks {
Either::Left(
references.chain(std::iter::once(ResolvedVc::upcast(
SingleItemCssChunk::new(*this.chunking_context, **item)
.to_resolved()
.await?,
))),
)
} else {
Either::Right(references)
})
})
.try_flat_join()
.await?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl OutputAsset for SingleItemCssChunk {
self.item
.asset_ident()
.with_modifier(single_item_modifier()),
".css".into(),
".single.css".into(),
)
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/b1abf_turbopack-tests_tests_snapshot_css_css-legacy-nesting_input_index_d1e6b0db.js",
{},
{"otherChunks":[{"path":"output/b1abf_turbopack-tests_tests_snapshot_css_css-legacy-nesting_input_style_da464693.css","included":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/css-legacy-nesting/input/style.css [test] (css)"],"moduleChunks":["output/b1abf_turbopack-tests_tests_snapshot_css_css-legacy-nesting_input_style_63c923ab.css"]},"output/b1abf_turbopack-tests_tests_snapshot_css_css-legacy-nesting_input_index_49d4edb8.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/css-legacy-nesting/input/index.js [test] (ecmascript)"]}
{"otherChunks":[{"path":"output/b1abf_turbopack-tests_tests_snapshot_css_css-legacy-nesting_input_style_da464693.css","included":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/css-legacy-nesting/input/style.css [test] (css)"]},"output/b1abf_turbopack-tests_tests_snapshot_css_css-legacy-nesting_input_index_49d4edb8.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/css-legacy-nesting/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading