Unverified Commit 45095461 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

refactor(turbopack): Use `ResolvedVc<T>` for struct fields in `turbopack-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 #

-->
parent e06bb4b9
...@@ -19,7 +19,7 @@ use swc_core::{ ...@@ -19,7 +19,7 @@ use swc_core::{
}; };
use tracing::Instrument; use tracing::Instrument;
use turbo_rcstr::RcStr; 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 turbo_tasks_fs::{FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -113,7 +113,7 @@ impl StyleSheetLike<'_, '_> { ...@@ -113,7 +113,7 @@ impl StyleSheetLike<'_, '_> {
/// Multiple [ModuleReference]s /// Multiple [ModuleReference]s
#[turbo_tasks::value(transparent)] #[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")] #[turbo_tasks::value(shared, serialization = "none", eq = "manual", cell = "new")]
pub enum ParseCssResult { pub enum ParseCssResult {
...@@ -247,7 +247,7 @@ pub async fn finalize_css( ...@@ -247,7 +247,7 @@ pub async fn finalize_css(
let mut url_map = HashMap::new(); let mut url_map = HashMap::new();
for (src, reference) in (*url_references.await?).iter() { 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() { if let Some(v) = resolved.as_ref().cloned() {
url_map.insert(RcStr::from(src.as_str()), v); url_map.insert(RcStr::from(src.as_str()), v);
} }
...@@ -315,7 +315,7 @@ pub async fn parse_css( ...@@ -315,7 +315,7 @@ pub async fn parse_css(
process_content( process_content(
**file_content, **file_content,
string.into_owned(), string.into_owned(),
fs_path, fs_path.to_resolved().await?,
ident_str, ident_str,
source, source,
origin, origin,
...@@ -335,7 +335,7 @@ pub async fn parse_css( ...@@ -335,7 +335,7 @@ pub async fn parse_css(
async fn process_content( async fn process_content(
content_vc: Vc<FileContent>, content_vc: Vc<FileContent>,
code: String, code: String,
fs_path_vc: Vc<FileSystemPath>, fs_path_vc: ResolvedVc<FileSystemPath>,
filename: &str, filename: &str,
source: Vc<Box<dyn Source>>, source: Vc<Box<dyn Source>>,
origin: Vc<Box<dyn ResolveOrigin>>, origin: Vc<Box<dyn ResolveOrigin>>,
...@@ -399,23 +399,33 @@ async fn process_content( ...@@ -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 { match err.kind {
lightningcss::error::ParserError::UnexpectedToken(_) lightningcss::error::ParserError::UnexpectedToken(_)
| lightningcss::error::ParserError::UnexpectedImportRule | lightningcss::error::ParserError::UnexpectedImportRule
| lightningcss::error::ParserError::SelectorError(..) | lightningcss::error::ParserError::SelectorError(..)
| lightningcss::error::ParserError::EndOfInput => { | lightningcss::error::ParserError::EndOfInput => {
let source = err.loc.as_ref().map(|loc| { let source = match &err.loc {
let pos = SourcePos { Some(loc) => {
line: loc.line as _, let pos = SourcePos {
column: loc.column as _, line: loc.line as _,
}; column: loc.column as _,
IssueSource::from_line_col(source, pos, pos) };
}); Some(
IssueSource::from_line_col(source, pos, pos)
.to_resolved()
.await?,
)
}
None => None,
};
ParsingIssue { ParsingIssue {
file: fs_path_vc, file: fs_path_vc,
msg: Vc::cell(err.to_string().into()), msg: ResolvedVc::cell(err.to_string().into()),
source, source,
} }
.cell() .cell()
...@@ -432,16 +442,23 @@ async fn process_content( ...@@ -432,16 +442,23 @@ async fn process_content(
stylesheet_into_static(&ss, without_warnings(config.clone())) stylesheet_into_static(&ss, without_warnings(config.clone()))
} }
Err(e) => { Err(e) => {
let source = e.loc.as_ref().map(|loc| { let source = match &e.loc {
let pos = SourcePos { Some(loc) => {
line: loc.line as _, let pos = SourcePos {
column: loc.column as _, line: loc.line as _,
}; column: loc.column as _,
IssueSource::from_line_col(source, pos, pos) };
}); Some(
IssueSource::from_line_col(source, pos, pos)
.to_resolved()
.await?,
)
}
None => None,
};
ParsingIssue { ParsingIssue {
file: fs_path_vc, file: fs_path_vc,
msg: Vc::cell(e.to_string().into()), msg: ResolvedVc::cell(e.to_string().into()),
source, source,
} }
.cell() .cell()
...@@ -457,6 +474,12 @@ async fn process_content( ...@@ -457,6 +474,12 @@ async fn process_content(
let (references, url_references) = let (references, url_references) =
analyze_references(&mut stylesheet, source, origin, import_context)?; 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 { Ok(ParseCssResult::Ok {
code: content_vc.to_resolved().await?, code: content_vc.to_resolved().await?,
stylesheet, stylesheet,
...@@ -485,12 +508,14 @@ enum CssError { ...@@ -485,12 +508,14 @@ enum CssError {
} }
impl CssError { impl CssError {
fn report(self, file: Vc<FileSystemPath>) { fn report(self, file: ResolvedVc<FileSystemPath>) {
match self { match self {
CssError::LightningCssSelectorInModuleNotPure { selector } => { CssError::LightningCssSelectorInModuleNotPure { selector } => {
ParsingIssue { ParsingIssue {
file, file,
msg: Vc::cell(format!("{CSS_MODULE_ERROR}, (lightningcss, {selector})").into()), msg: ResolvedVc::cell(
format!("{CSS_MODULE_ERROR}, (lightningcss, {selector})").into(),
),
source: None, source: None,
} }
.cell() .cell()
...@@ -646,16 +671,16 @@ impl GenerateSourceMap for ParseCssResultSourceMap { ...@@ -646,16 +671,16 @@ impl GenerateSourceMap for ParseCssResultSourceMap {
#[turbo_tasks::value] #[turbo_tasks::value]
struct ParsingIssue { struct ParsingIssue {
msg: Vc<RcStr>, msg: ResolvedVc<RcStr>,
file: Vc<FileSystemPath>, file: ResolvedVc<FileSystemPath>,
source: Option<Vc<IssueSource>>, source: Option<ResolvedVc<IssueSource>>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Issue for ParsingIssue { impl Issue for ParsingIssue {
#[turbo_tasks::function] #[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> { fn file_path(&self) -> Vc<FileSystemPath> {
self.file *self.file
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -670,7 +695,7 @@ impl Issue for ParsingIssue { ...@@ -670,7 +695,7 @@ impl Issue for ParsingIssue {
#[turbo_tasks::function] #[turbo_tasks::function]
fn source(&self) -> Vc<OptionIssueSource> { 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] #[turbo_tasks::function]
......
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