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 single css chunks with import context #7775

Merged
merged 6 commits into from
Mar 22, 2024
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
37 changes: 37 additions & 0 deletions crates/turbopack-core/src/reference_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ impl ImportContext {

Ok(ImportContext::new(layers, media, supports))
}

#[turbo_tasks::function]
pub fn modifier(&self) -> Result<Vc<String>> {
use std::fmt::Write;
let mut modifier = String::new();
if !self.layers.is_empty() {
for (i, layer) in self.layers.iter().enumerate() {
if i > 0 {
modifier.push(' ');
}
write!(modifier, "layer({})", layer)?
}
}
if !self.media.is_empty() {
if !modifier.is_empty() {
modifier.push(' ');
}
for (i, media) in self.media.iter().enumerate() {
if i > 0 {
modifier.push_str(" and ");
}
modifier.push_str(media);
}
}
if !self.supports.is_empty() {
if !modifier.is_empty() {
modifier.push(' ');
}
for (i, supports) in self.supports.iter().enumerate() {
if i > 0 {
modifier.push(' ');
}
write!(modifier, "supports({})", supports)?
}
}
Ok(Vc::cell(modifier))
}
}

#[turbo_tasks::value(serialization = "auto_for_input")]
Expand Down
19 changes: 14 additions & 5 deletions crates/turbopack-css/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ use crate::{
};

#[turbo_tasks::function]
fn modifier() -> Vc<String> {
Vc::cell("css".to_string())
fn modifier(use_swc_css: bool) -> Vc<String> {
if use_swc_css {
Vc::cell("swc css".to_string())
} else {
Vc::cell("css".to_string())
}
}

#[turbo_tasks::value]
Expand Down Expand Up @@ -107,10 +111,15 @@ impl ProcessCss for CssModuleAsset {
impl Module for CssModuleAsset {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
self.source
let mut ident = self
.source
.ident()
.with_modifier(modifier())
.with_layer(self.asset_context.layer())
.with_modifier(modifier(self.use_swc_css))
.with_layer(self.asset_context.layer());
if let Some(import_context) = self.import_context {
ident = ident.with_modifier(import_context.modifier())
}
ident
}

#[turbo_tasks::function]
Expand Down
53 changes: 29 additions & 24 deletions crates/turbopack-css/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,11 @@ impl CssChunk {
}

writeln!(body, "/* {} */", id)?;
let mut close: Vec<String> = vec![];
if let Some(import_context) = content.import_context {
let import_context = &*import_context.await?;
if !&import_context.layers.is_empty() {
writeln!(body, "@layer {} {{", import_context.layers.join("."))?;
close.push("}\n".to_owned());
}
if !&import_context.media.is_empty() {
writeln!(body, "@media {} {{", import_context.media.join(" and "))?;
close.push("}\n".to_owned());
}
if !&import_context.supports.is_empty() {
writeln!(
body,
"@supports {} {{",
import_context.supports.join(" and ")
)?;
close.push("}\n".to_owned());
}
}
let close = write_import_context(&mut body, content.import_context).await?;

body.push_source(&content.inner_code, content.source_map.map(Vc::upcast));
writeln!(body)?;

for line in &close {
body.push_source(&Rope::from(line.to_string()), None);
}
writeln!(body, "{close}")?;
writeln!(body)?;
}

Expand Down Expand Up @@ -142,6 +120,33 @@ impl CssChunk {
}
}

pub async fn write_import_context(
body: &mut impl std::io::Write,
import_context: Option<Vc<ImportContext>>,
) -> Result<String> {
let mut close = String::new();
if let Some(import_context) = import_context {
let import_context = &*import_context.await?;
if !&import_context.layers.is_empty() {
writeln!(body, "@layer {} {{", import_context.layers.join("."))?;
close.push_str("\n}");
}
if !&import_context.media.is_empty() {
writeln!(body, "@media {} {{", import_context.media.join(" and "))?;
close.push_str("\n}");
}
if !&import_context.supports.is_empty() {
writeln!(
body,
"@supports {} {{",
import_context.supports.join(" and ")
)?;
close.push_str("\n}");
}
}
Ok(close)
}

#[turbo_tasks::value]
pub struct CssChunkContent {
pub chunk_items: Vec<Vc<Box<dyn CssChunkItem>>>,
Expand Down
5 changes: 4 additions & 1 deletion crates/turbopack-css/src/chunk/single_item_chunk/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use turbopack_core::{
};

use super::source_map::SingleItemCssChunkSourceMapAsset;
use crate::chunk::CssChunkItem;
use crate::chunk::{write_import_context, CssChunkItem};

/// A CSS chunk that only contains a single item. This is used for selectively
/// loading CSS modules that are part of a larger chunk in development mode, and
Expand Down Expand Up @@ -54,7 +54,10 @@ impl SingleItemCssChunk {

writeln!(code, "/* {} */", id)?;
let content = this.item.content().await?;
let close = write_import_context(&mut code, content.import_context).await?;

code.push_source(&content.inner_code, content.source_map.map(Vc::upcast));
write!(code, "{close}")?;

if *this
.chunking_context
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.

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.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import url("./c.css") layer(foo) (orientation: landscape);
@import url("./c.css") layer(bar) (orientation: portrait);

.imported {
color: orange;
Expand Down

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.

This file was deleted.

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

Loading
Loading