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 {
#[turbo_tasks::function]
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(),
)))
}
#[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]
async fn get_rsc_transitions(
self: Vc<Self>,
......@@ -417,24 +422,14 @@ impl AppProject {
.into_iter()
.collect(),
transition_rules: vec![
// Mark as client reference (and exclude from RSC chunking) the edge from the
// CSS Module to the actual CSS
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.
// CSS Modules should be in the client layer but, the JS CSS Module should not be
// wrapped in the CssClientReferenceModule
TransitionRule::new(
module_styles_rule_condition(),
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(
styles_rule_condition(),
ResolvedVc::upcast(self.css_client_reference_transition().to_resolved().await?),
......@@ -596,6 +591,24 @@ impl AppProject {
Ok(ModuleAssetContext::new(
TransitionOptions {
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()
}
.cell(),
......
use anyhow::{Context, Result};
use turbo_tasks::{ResolvedVc, Vc};
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;
#[turbo_tasks::value(shared)]
pub struct NextCssClientReferenceTransition {
client_transition: ResolvedVc<Box<dyn Transition>>,
client_transition: Option<ResolvedVc<Box<dyn Transition>>>,
}
#[turbo_tasks::value_impl]
impl NextCssClientReferenceTransition {
/// Create a transition that only add a marker `CssClientReferenceModule`.
#[turbo_tasks::function]
pub fn new(client_transition: ResolvedVc<Box<dyn Transition>>) -> Vc<Self> {
NextCssClientReferenceTransition { client_transition }.cell()
pub fn new_marker() -> Vc<Self> {
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 {
self: Vc<Self>,
original_source: Vc<Box<dyn Source>>,
source: Vc<Box<dyn Source>>,
rsc_module_asset_context: Vc<ModuleAssetContext>,
module_asset_context: Vc<ModuleAssetContext>,
reference_type: ReferenceType,
) -> Result<Vc<ProcessResult>> {
let module = self.await?.client_transition.process(
original_source,
source,
rsc_module_asset_context,
reference_type,
);
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,
source,
module_asset_context,
new_reference_type,
)
} else {
module_asset_context.process(source, new_reference_type)
};
let ProcessResult::Module(module) = *module.await? else {
return Ok(ProcessResult::Ignore.cell());
......
......@@ -59,7 +59,9 @@ pub async fn get_next_server_transforms_rules(
RuleCondition::ResourcePathEndsWith(".module.css".into()),
RuleCondition::ContentTypeStartsWith("text/css+module".into()),
]),
RuleCondition::ReferenceType(ReferenceType::Css(CssReferenceSubType::Internal)),
RuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::ModuleStyles,
)),
]),
vec![ModuleRuleEffect::Ignore],
),
......
......@@ -220,10 +220,11 @@ pub enum CssReferenceSubType {
/// Reference from ModuleCssAsset to an imported ModuleCssAsset for retrieving the composed
/// class name
Compose,
/// Used for generating the list of classes in a ModuleCssAsset
ModuleAnalyze,
/// Reference from ModuleCssAsset to the CssModuleAsset
ModuleStyles,
Internal,
/// Used for generating the list of classes in a ModuleCssAsset
InternalAnalyze,
Custom(u8),
#[default]
Undefined,
......@@ -421,7 +422,8 @@ impl ReferenceType {
self,
ReferenceType::Internal(_)
| ReferenceType::Css(CssReferenceSubType::Internal)
| ReferenceType::Css(CssReferenceSubType::InternalAnalyze)
| ReferenceType::Css(CssReferenceSubType::ModuleAnalyze)
| ReferenceType::Css(CssReferenceSubType::ModuleStyles)
| ReferenceType::Runtime
)
}
......
......@@ -88,7 +88,7 @@ impl Module for ModuleCssAsset {
.copied()
.chain(
match *self
.inner(ReferenceType::Css(CssReferenceSubType::Internal))
.inner(ReferenceType::Css(CssReferenceSubType::ModuleStyles))
.try_into_module()
.await?
{
......@@ -169,7 +169,7 @@ impl ModuleCssAsset {
#[turbo_tasks::function]
async fn classes(self: Vc<Self>) -> Result<Vc<ModuleCssClasses>> {
let inner = self
.inner(ReferenceType::Css(CssReferenceSubType::InternalAnalyze))
.inner(ReferenceType::Css(CssReferenceSubType::ModuleAnalyze))
.module();
let inner = Vc::try_resolve_sidecast::<Box<dyn ProcessCss>>(inner)
......
......@@ -567,7 +567,7 @@ impl ModuleOptions {
ModuleRule::new_internal(
RuleCondition::all(vec![
RuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::InternalAnalyze,
CssReferenceSubType::ModuleAnalyze,
)),
RuleCondition::Any(vec![
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