-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for CSS client references (vercel/turborepo#5397)
### Description This PR changes CSS assets to support the CSS client references use case of Next.js, where .css assets in the RSC graph should be replaced with a client reference assets. CSS assets are now processed in two steps: 1. Processing the CSS source asset into either a Global or a Module CSS asset. 2. Inside of Global/Module assets, processing the CSS source asset into a CSS asset. By default, (2.) will create a leaf CSS asset. But in Next.js RSC, this step will be replaced with creating a CSS client reference instead. This client reference can then be detected when walking the RSC graph, and a client CSS chunk can be produced from there. ### Testing Instructions N/A
- Loading branch information
Showing
30 changed files
with
509 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use crate::asset::{Asset, AssetVc}; | ||
|
||
/// An [Asset] that should never be placed into a chunk, but whose references | ||
/// should still be followed. | ||
#[turbo_tasks::value_trait] | ||
pub trait PassthroughAsset: Asset {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use anyhow::{bail, Result}; | ||
use turbo_tasks::{primitives::StringVc, Value}; | ||
use turbopack_core::{ | ||
asset::{Asset, AssetContentVc, AssetVc}, | ||
chunk::{PassthroughAsset, PassthroughAssetVc}, | ||
context::{AssetContext, AssetContextVc}, | ||
ident::AssetIdentVc, | ||
reference::AssetReferencesVc, | ||
reference_type::{CssReferenceSubType, ReferenceType}, | ||
}; | ||
|
||
use crate::references::internal::InternalCssAssetReferenceVc; | ||
|
||
#[turbo_tasks::value] | ||
#[derive(Clone)] | ||
pub struct GlobalCssAsset { | ||
source: AssetVc, | ||
context: AssetContextVc, | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl GlobalCssAssetVc { | ||
/// Creates a new CSS asset. The CSS is treated as global CSS. | ||
#[turbo_tasks::function] | ||
pub fn new(source: AssetVc, context: AssetContextVc) -> Self { | ||
Self::cell(GlobalCssAsset { source, context }) | ||
} | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl GlobalCssAssetVc { | ||
#[turbo_tasks::function] | ||
async fn inner(self) -> Result<AssetVc> { | ||
let this = self.await?; | ||
// The underlying CSS is processed through an internal CSS reference. | ||
// This can then be picked up by other rules to treat CSS assets in | ||
// a special way. For instance, in the Next App Router implementation, | ||
// RSC CSS assets will be added to the client references manifest. | ||
Ok(this.context.process( | ||
this.source, | ||
Value::new(ReferenceType::Css(CssReferenceSubType::Internal)), | ||
)) | ||
} | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl Asset for GlobalCssAsset { | ||
#[turbo_tasks::function] | ||
fn ident(&self) -> AssetIdentVc { | ||
self.source.ident().with_modifier(modifier()) | ||
} | ||
|
||
#[turbo_tasks::function] | ||
fn content(&self) -> Result<AssetContentVc> { | ||
bail!("CSS global asset has no contents") | ||
} | ||
|
||
#[turbo_tasks::function] | ||
fn references(self_vc: GlobalCssAssetVc) -> AssetReferencesVc { | ||
AssetReferencesVc::cell(vec![ | ||
InternalCssAssetReferenceVc::new(self_vc.inner()).into() | ||
]) | ||
} | ||
} | ||
|
||
#[turbo_tasks::function] | ||
fn modifier() -> StringVc { | ||
StringVc::cell("global css".to_string()) | ||
} | ||
|
||
/// A GlobalAsset is a transparent wrapper around an actual CSS asset. | ||
#[turbo_tasks::value_impl] | ||
impl PassthroughAsset for GlobalCssAsset {} |
Oops, something went wrong.