Skip to content

Commit

Permalink
refactor(turbopack): Use ResolvedVc<T> for struct fields in `turbop…
Browse files Browse the repository at this point in the history
…ack-css` (#73300)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
  • Loading branch information
kdy1 authored Nov 30, 2024
1 parent e06bb4b commit 4509546
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions turbopack/crates/turbopack-css/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use swc_core::{
};
use tracing::Instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, ResolvedVc, ValueToString, Vc};
use turbo_tasks::{FxIndexMap, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbo_tasks_fs::{FileContent, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
Expand Down Expand Up @@ -113,7 +113,7 @@ impl StyleSheetLike<'_, '_> {

/// Multiple [ModuleReference]s
#[turbo_tasks::value(transparent)]
pub struct UnresolvedUrlReferences(pub Vec<(String, Vc<UrlAssetReference>)>);
pub struct UnresolvedUrlReferences(pub Vec<(String, ResolvedVc<UrlAssetReference>)>);

#[turbo_tasks::value(shared, serialization = "none", eq = "manual", cell = "new")]
pub enum ParseCssResult {
Expand Down Expand Up @@ -247,7 +247,7 @@ pub async fn finalize_css(
let mut url_map = HashMap::new();

for (src, reference) in (*url_references.await?).iter() {
let resolved = resolve_url_reference(*reference, chunking_context).await?;
let resolved = resolve_url_reference(**reference, chunking_context).await?;
if let Some(v) = resolved.as_ref().cloned() {
url_map.insert(RcStr::from(src.as_str()), v);
}
Expand Down Expand Up @@ -315,7 +315,7 @@ pub async fn parse_css(
process_content(
**file_content,
string.into_owned(),
fs_path,
fs_path.to_resolved().await?,
ident_str,
source,
origin,
Expand All @@ -335,7 +335,7 @@ pub async fn parse_css(
async fn process_content(
content_vc: Vc<FileContent>,
code: String,
fs_path_vc: Vc<FileSystemPath>,
fs_path_vc: ResolvedVc<FileSystemPath>,
filename: &str,
source: Vc<Box<dyn Source>>,
origin: Vc<Box<dyn ResolveOrigin>>,
Expand Down Expand Up @@ -399,23 +399,33 @@ async fn process_content(
}
}

for err in warnings.read().unwrap().iter() {
// We need to collect here because we need to avoid holding the lock while calling
// `.await` in the loop.
let warngins = warnings.read().unwrap().iter().cloned().collect::<Vec<_>>();
for err in warngins.iter() {
match err.kind {
lightningcss::error::ParserError::UnexpectedToken(_)
| lightningcss::error::ParserError::UnexpectedImportRule
| lightningcss::error::ParserError::SelectorError(..)
| lightningcss::error::ParserError::EndOfInput => {
let source = err.loc.as_ref().map(|loc| {
let pos = SourcePos {
line: loc.line as _,
column: loc.column as _,
};
IssueSource::from_line_col(source, pos, pos)
});
let source = match &err.loc {
Some(loc) => {
let pos = SourcePos {
line: loc.line as _,
column: loc.column as _,
};
Some(
IssueSource::from_line_col(source, pos, pos)
.to_resolved()
.await?,
)
}
None => None,
};

ParsingIssue {
file: fs_path_vc,
msg: Vc::cell(err.to_string().into()),
msg: ResolvedVc::cell(err.to_string().into()),
source,
}
.cell()
Expand All @@ -432,16 +442,23 @@ async fn process_content(
stylesheet_into_static(&ss, without_warnings(config.clone()))
}
Err(e) => {
let source = e.loc.as_ref().map(|loc| {
let pos = SourcePos {
line: loc.line as _,
column: loc.column as _,
};
IssueSource::from_line_col(source, pos, pos)
});
let source = match &e.loc {
Some(loc) => {
let pos = SourcePos {
line: loc.line as _,
column: loc.column as _,
};
Some(
IssueSource::from_line_col(source, pos, pos)
.to_resolved()
.await?,
)
}
None => None,
};
ParsingIssue {
file: fs_path_vc,
msg: Vc::cell(e.to_string().into()),
msg: ResolvedVc::cell(e.to_string().into()),
source,
}
.cell()
Expand All @@ -457,6 +474,12 @@ async fn process_content(
let (references, url_references) =
analyze_references(&mut stylesheet, source, origin, import_context)?;

let url_references = url_references
.into_iter()
.map(|(k, v)| async move { Ok((k, v.to_resolved().await?)) })
.try_join()
.await?;

Ok(ParseCssResult::Ok {
code: content_vc.to_resolved().await?,
stylesheet,
Expand Down Expand Up @@ -485,12 +508,14 @@ enum CssError {
}

impl CssError {
fn report(self, file: Vc<FileSystemPath>) {
fn report(self, file: ResolvedVc<FileSystemPath>) {
match self {
CssError::LightningCssSelectorInModuleNotPure { selector } => {
ParsingIssue {
file,
msg: Vc::cell(format!("{CSS_MODULE_ERROR}, (lightningcss, {selector})").into()),
msg: ResolvedVc::cell(
format!("{CSS_MODULE_ERROR}, (lightningcss, {selector})").into(),
),
source: None,
}
.cell()
Expand Down Expand Up @@ -646,16 +671,16 @@ impl GenerateSourceMap for ParseCssResultSourceMap {

#[turbo_tasks::value]
struct ParsingIssue {
msg: Vc<RcStr>,
file: Vc<FileSystemPath>,
source: Option<Vc<IssueSource>>,
msg: ResolvedVc<RcStr>,
file: ResolvedVc<FileSystemPath>,
source: Option<ResolvedVc<IssueSource>>,
}

#[turbo_tasks::value_impl]
impl Issue for ParsingIssue {
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
self.file
*self.file
}

#[turbo_tasks::function]
Expand All @@ -670,7 +695,7 @@ impl Issue for ParsingIssue {

#[turbo_tasks::function]
fn source(&self) -> Vc<OptionIssueSource> {
Vc::cell(self.source.map(|s| s.resolve_source_map(self.file)))
Vc::cell(self.source.map(|s| s.resolve_source_map(*self.file)))
}

#[turbo_tasks::function]
Expand Down

0 comments on commit 4509546

Please sign in to comment.