Commit 7eb53237 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

Still add marker module for CSS modules

parent 8818b373
...@@ -374,11 +374,16 @@ impl AppProject { ...@@ -374,11 +374,16 @@ impl AppProject {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn css_client_reference_transition(self: Vc<Self>) -> Vc<Box<dyn Transition>> { pub fn css_client_reference_transition(self: Vc<Self>) -> Vc<Box<dyn Transition>> {
Vc::upcast(NextCssClientReferenceTransition::new(Vc::upcast( Vc::upcast(NextCssClientReferenceTransition::new_client(Vc::upcast(
self.client_transition(), self.client_transition(),
))) )))
} }
#[turbo_tasks::function]
pub fn css_client_reference_marker_transition(self: Vc<Self>) -> Vc<Box<dyn Transition>> {
Vc::upcast(NextCssClientReferenceTransition::new_marker())
}
#[turbo_tasks::function] #[turbo_tasks::function]
async fn get_rsc_transitions( async fn get_rsc_transitions(
self: Vc<Self>, self: Vc<Self>,
...@@ -417,24 +422,14 @@ impl AppProject { ...@@ -417,24 +422,14 @@ impl AppProject {
.into_iter() .into_iter()
.collect(), .collect(),
transition_rules: vec![ transition_rules: vec![
// Mark as client reference (and exclude from RSC chunking) the edge from the // CSS Modules should be in the client layer but, the JS CSS Module should not be
// CSS Module to the actual CSS // wrapped in the CssClientReferenceModule
TransitionRule::new_internal(
RuleCondition::all(vec![
RuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::Internal,
)),
module_styles_rule_condition(),
]),
ResolvedVc::upcast(self.css_client_reference_transition().to_resolved().await?),
),
// Don't wrap in marker module but change context, this is used to determine
// the list of CSS module classes.
TransitionRule::new( TransitionRule::new(
module_styles_rule_condition(), module_styles_rule_condition(),
ResolvedVc::upcast(self.client_transition().to_resolved().await?), ResolvedVc::upcast(self.client_transition().to_resolved().await?),
), ),
// Mark as client reference all regular CSS imports // Mark all regular CSS imports as client reference (transition and wrap
// CssClientReferenceModule)
TransitionRule::new( TransitionRule::new(
styles_rule_condition(), styles_rule_condition(),
ResolvedVc::upcast(self.css_client_reference_transition().to_resolved().await?), ResolvedVc::upcast(self.css_client_reference_transition().to_resolved().await?),
...@@ -596,6 +591,24 @@ impl AppProject { ...@@ -596,6 +591,24 @@ impl AppProject {
Ok(ModuleAssetContext::new( Ok(ModuleAssetContext::new(
TransitionOptions { TransitionOptions {
named_transitions: transitions, named_transitions: transitions,
transition_rules: vec![
// The edge from the JS CSS Module to the actual CSS styles module should be
// wrapped with a CssClientReferenceModule to mark it as a client reference
// (and exclude it from RSC chunking).
TransitionRule::new_internal(
RuleCondition::all(vec![
RuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::ModuleStyles,
)),
module_styles_rule_condition(),
]),
ResolvedVc::upcast(
self.css_client_reference_marker_transition()
.to_resolved()
.await?,
),
),
],
..Default::default() ..Default::default()
} }
.cell(), .cell(),
......
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbopack::{ModuleAssetContext, css::chunk::CssChunkPlaceable, transition::Transition}; use turbopack::{ModuleAssetContext, css::chunk::CssChunkPlaceable, transition::Transition};
use turbopack_core::{context::ProcessResult, reference_type::ReferenceType, source::Source}; use turbopack_core::{
context::{AssetContext, ProcessResult},
reference_type::{CssReferenceSubType, ReferenceType},
source::Source,
};
use crate::next_client_reference::css_client_reference::css_client_reference_module::CssClientReferenceModule; use crate::next_client_reference::css_client_reference::css_client_reference_module::CssClientReferenceModule;
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct NextCssClientReferenceTransition { pub struct NextCssClientReferenceTransition {
client_transition: ResolvedVc<Box<dyn Transition>>, client_transition: Option<ResolvedVc<Box<dyn Transition>>>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl NextCssClientReferenceTransition { impl NextCssClientReferenceTransition {
/// Create a transition that only add a marker `CssClientReferenceModule`.
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new(client_transition: ResolvedVc<Box<dyn Transition>>) -> Vc<Self> { pub fn new_marker() -> Vc<Self> {
NextCssClientReferenceTransition { client_transition }.cell() NextCssClientReferenceTransition {
client_transition: None,
}
.cell()
}
/// Create a transition that applies `client_transiton` and adds a marker
/// `CssClientReferenceModule`.
#[turbo_tasks::function]
pub fn new_client(client_transition: ResolvedVc<Box<dyn Transition>>) -> Vc<Self> {
NextCssClientReferenceTransition {
client_transition: Some(client_transition),
}
.cell()
} }
} }
...@@ -25,15 +43,27 @@ impl Transition for NextCssClientReferenceTransition { ...@@ -25,15 +43,27 @@ impl Transition for NextCssClientReferenceTransition {
self: Vc<Self>, self: Vc<Self>,
original_source: Vc<Box<dyn Source>>, original_source: Vc<Box<dyn Source>>,
source: Vc<Box<dyn Source>>, source: Vc<Box<dyn Source>>,
rsc_module_asset_context: Vc<ModuleAssetContext>, module_asset_context: Vc<ModuleAssetContext>,
reference_type: ReferenceType, reference_type: ReferenceType,
) -> Result<Vc<ProcessResult>> { ) -> Result<Vc<ProcessResult>> {
let module = self.await?.client_transition.process( let new_reference_type = match reference_type {
ReferenceType::Css(CssReferenceSubType::ModuleStyles) => {
// Prevent an infinite loop since this transition matches on the
// InternalModuleStyles reference type
ReferenceType::Css(CssReferenceSubType::Internal)
}
ty => ty,
};
let module = if let Some(client_transition) = self.await?.client_transition {
client_transition.process(
original_source, original_source,
source, source,
rsc_module_asset_context, module_asset_context,
reference_type, new_reference_type,
); )
} else {
module_asset_context.process(source, new_reference_type)
};
let ProcessResult::Module(module) = *module.await? else { let ProcessResult::Module(module) = *module.await? else {
return Ok(ProcessResult::Ignore.cell()); return Ok(ProcessResult::Ignore.cell());
......
...@@ -59,7 +59,9 @@ pub async fn get_next_server_transforms_rules( ...@@ -59,7 +59,9 @@ pub async fn get_next_server_transforms_rules(
RuleCondition::ResourcePathEndsWith(".module.css".into()), RuleCondition::ResourcePathEndsWith(".module.css".into()),
RuleCondition::ContentTypeStartsWith("text/css+module".into()), RuleCondition::ContentTypeStartsWith("text/css+module".into()),
]), ]),
RuleCondition::ReferenceType(ReferenceType::Css(CssReferenceSubType::Internal)), RuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::ModuleStyles,
)),
]), ]),
vec![ModuleRuleEffect::Ignore], vec![ModuleRuleEffect::Ignore],
), ),
......
...@@ -220,10 +220,11 @@ pub enum CssReferenceSubType { ...@@ -220,10 +220,11 @@ pub enum CssReferenceSubType {
/// Reference from ModuleCssAsset to an imported ModuleCssAsset for retrieving the composed /// Reference from ModuleCssAsset to an imported ModuleCssAsset for retrieving the composed
/// class name /// class name
Compose, Compose,
/// Used for generating the list of classes in a ModuleCssAsset
ModuleAnalyze,
/// Reference from ModuleCssAsset to the CssModuleAsset /// Reference from ModuleCssAsset to the CssModuleAsset
ModuleStyles,
Internal, Internal,
/// Used for generating the list of classes in a ModuleCssAsset
InternalAnalyze,
Custom(u8), Custom(u8),
#[default] #[default]
Undefined, Undefined,
...@@ -421,7 +422,8 @@ impl ReferenceType { ...@@ -421,7 +422,8 @@ impl ReferenceType {
self, self,
ReferenceType::Internal(_) ReferenceType::Internal(_)
| ReferenceType::Css(CssReferenceSubType::Internal) | ReferenceType::Css(CssReferenceSubType::Internal)
| ReferenceType::Css(CssReferenceSubType::InternalAnalyze) | ReferenceType::Css(CssReferenceSubType::ModuleAnalyze)
| ReferenceType::Css(CssReferenceSubType::ModuleStyles)
| ReferenceType::Runtime | ReferenceType::Runtime
) )
} }
......
...@@ -88,7 +88,7 @@ impl Module for ModuleCssAsset { ...@@ -88,7 +88,7 @@ impl Module for ModuleCssAsset {
.copied() .copied()
.chain( .chain(
match *self match *self
.inner(ReferenceType::Css(CssReferenceSubType::Internal)) .inner(ReferenceType::Css(CssReferenceSubType::ModuleStyles))
.try_into_module() .try_into_module()
.await? .await?
{ {
...@@ -169,7 +169,7 @@ impl ModuleCssAsset { ...@@ -169,7 +169,7 @@ impl ModuleCssAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn classes(self: Vc<Self>) -> Result<Vc<ModuleCssClasses>> { async fn classes(self: Vc<Self>) -> Result<Vc<ModuleCssClasses>> {
let inner = self let inner = self
.inner(ReferenceType::Css(CssReferenceSubType::InternalAnalyze)) .inner(ReferenceType::Css(CssReferenceSubType::ModuleAnalyze))
.module(); .module();
let inner = Vc::try_resolve_sidecast::<Box<dyn ProcessCss>>(inner) let inner = Vc::try_resolve_sidecast::<Box<dyn ProcessCss>>(inner)
......
...@@ -567,7 +567,7 @@ impl ModuleOptions { ...@@ -567,7 +567,7 @@ impl ModuleOptions {
ModuleRule::new_internal( ModuleRule::new_internal(
RuleCondition::all(vec![ RuleCondition::all(vec![
RuleCondition::ReferenceType(ReferenceType::Css( RuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::InternalAnalyze, CssReferenceSubType::ModuleAnalyze,
)), )),
RuleCondition::Any(vec![ RuleCondition::Any(vec![
RuleCondition::ResourcePathEndsWith(".module.css".to_string()), RuleCondition::ResourcePathEndsWith(".module.css".to_string()),
......
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